Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shrinking indication parameter #232

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/proper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@
-type short_module_result() :: [mfa()] | error().
-type module_result() :: long_module_result() | short_module_result().
-type shrinking_result() :: {non_neg_integer(),imm_testcase()}.
-type shrinking_states() :: 'false' | 'true' | 'done'.

%%-----------------------------------------------------------------------------
%% State handling functions
Expand Down Expand Up @@ -1701,11 +1702,23 @@ finalize_input(Instance) ->
%% Shrinking functions
%%-----------------------------------------------------------------------------

-spec set_shrinking_param(shrinking_states()) -> ok.
set_shrinking_param(Val) ->
OldParams = erlang:get('$parameters'),
case OldParams of
undefined ->
erlang:put('$parameters', [{shrinking, Val}]);
_ ->
erlang:put('$parameters', [{shrinking, Val} | OldParams])
end,
ok.

-spec shrink(imm_testcase(), test(), fail_reason(), opts()) ->
{'ok',imm_testcase()} | error().
shrink(ImmTestCase, Test, Reason,
#opts{expect_fail = false, noshrink = false, max_shrinks = MaxShrinks,
output_fun = Print, nocolors = NoColors} = Opts) ->
set_shrinking_param(true), % needs to be set before printing
case NoColors of
true ->
Print("~nShrinking ", []);
Expand Down Expand Up @@ -1736,10 +1749,12 @@ shrink(ImmTestCase, Test, Reason,
end
catch
throw:non_boolean_result ->
set_shrinking_param(done),
Print("~n", []),
{error, non_boolean_result}
end;
shrink(ImmTestCase, _Test, _Reason, _Opts) ->
set_shrinking_param(done),
{ok, ImmTestCase}.

-spec fix_shrink(imm_testcase(), stripped_test(), fail_reason(),
Expand Down Expand Up @@ -2125,6 +2140,7 @@ report_shrinking(Shrinks, MinImmTestCase, MinActions, Print, NoColors) ->
true -> Print("(~b time(s))~n", [Shrinks]);
false -> Print("\033[01;34m(~b time(s))\033[00m~n", [Shrinks])
end,
set_shrinking_param(done),
print_imm_testcase(MinImmTestCase, "", Print),
execute_actions(MinActions).

Expand Down
20 changes: 20 additions & 0 deletions test/proper_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,26 @@ native_type_props_test_() ->
-record(untyped, {a, b = 12}).
-type untyped() :: #untyped{}.

shrinking_param_phases_test_() ->
?_test(begin
?assertMatch(
false,
proper:quickcheck(
?FORALL(_, 1, false),
[{numtests, 1},
{on_output,
fun(_,_) ->
S = proper_types:parameter(shrinking, false),
put({shrink_phase_mark, S}, true)
end}])),
?assertEqual(true, get({shrink_phase_mark, false})),
?assertEqual(true, get({shrink_phase_mark, true})),
?assertEqual(true, get({shrink_phase_mark, done})),
[erase({shrink_phase_mark, P}) || P <- [false, true, done]],
proper:clean_garbage(),
?assert(state_is_clean())
end).

true_props_test_() ->
[?_passes(?FORALL(X,integer(),X < X + 1)),
?_passes(?FORALL(A,atom(),list_to_atom(atom_to_list(A)) =:= A)),
Expand Down