Skip to content

Commit ca41234

Browse files
committed
Add shrinking indication parameter
Set's a `shrinking` parameter to `true` when property is being executed during a shrinking phase. Also sets it to `done` after the shrinking is done. Useful for adjusting generators during shrinking phase or for `on_output` printers.
1 parent 5b66abe commit ca41234

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/proper.erl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@
581581
-type short_module_result() :: [mfa()] | error().
582582
-type module_result() :: long_module_result() | short_module_result().
583583
-type shrinking_result() :: {non_neg_integer(),imm_testcase()}.
584+
-type shrinking_states() :: 'false' | 'true' | 'done'.
584585

585586
%%-----------------------------------------------------------------------------
586587
%% State handling functions
@@ -1701,11 +1702,23 @@ finalize_input(Instance) ->
17011702
%% Shrinking functions
17021703
%%-----------------------------------------------------------------------------
17031704

1705+
-spec set_shrinking_param(shrinking_states()) -> ok.
1706+
set_shrinking_param(Val) ->
1707+
OldParams = erlang:get('$parameters'),
1708+
case OldParams of
1709+
undefined ->
1710+
erlang:put('$parameters', [{shrinking, Val}]);
1711+
_ ->
1712+
erlang:put('$parameters', [{shrinking, Val} | OldParams])
1713+
end,
1714+
ok.
1715+
17041716
-spec shrink(imm_testcase(), test(), fail_reason(), opts()) ->
17051717
{'ok',imm_testcase()} | error().
17061718
shrink(ImmTestCase, Test, Reason,
17071719
#opts{expect_fail = false, noshrink = false, max_shrinks = MaxShrinks,
17081720
output_fun = Print, nocolors = NoColors} = Opts) ->
1721+
set_shrinking_param(true), % needs to be set before printing
17091722
case NoColors of
17101723
true ->
17111724
Print("~nShrinking ", []);
@@ -1736,10 +1749,12 @@ shrink(ImmTestCase, Test, Reason,
17361749
end
17371750
catch
17381751
throw:non_boolean_result ->
1752+
set_shrinking_param(done),
17391753
Print("~n", []),
17401754
{error, non_boolean_result}
17411755
end;
17421756
shrink(ImmTestCase, _Test, _Reason, _Opts) ->
1757+
set_shrinking_param(done),
17431758
{ok, ImmTestCase}.
17441759

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

test/proper_tests.erl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,26 @@ native_type_props_test_() ->
898898
-record(untyped, {a, b = 12}).
899899
-type untyped() :: #untyped{}.
900900

901+
shrinking_param_phases_test_() ->
902+
?_test(begin
903+
?assertMatch(
904+
false,
905+
proper:quickcheck(
906+
?FORALL(_, 1, false),
907+
[{numtests, 1},
908+
{on_output,
909+
fun(_,_) ->
910+
S = proper_types:parameter(shrinking, false),
911+
put({shrink_phase_mark, S}, true)
912+
end}])),
913+
?assertEqual(true, get({shrink_phase_mark, false})),
914+
?assertEqual(true, get({shrink_phase_mark, true})),
915+
?assertEqual(true, get({shrink_phase_mark, done})),
916+
[erase({shrink_phase_mark, P}) || P <- [false, true, done]],
917+
proper:clean_garbage(),
918+
?assert(state_is_clean())
919+
end).
920+
901921
true_props_test_() ->
902922
[?_passes(?FORALL(X,integer(),X < X + 1)),
903923
?_passes(?FORALL(A,atom(),list_to_atom(atom_to_list(A)) =:= A)),

0 commit comments

Comments
 (0)