This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
qc.erl
134 lines (109 loc) · 4.44 KB
/
qc.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
%%%-------------------------------------------------------------------
%%% Copyright (c) 2009-2012 Gemini Mobile Technologies, Inc. All rights reserved.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%
%%% File : qc.erl
%%% Purpose : Wrapper for QuickCheck and Proper
%%%-------------------------------------------------------------------
-module(qc).
-include("qc_impl.hrl").
-ifdef(QC).
-include_lib("eunit/include/eunit.hrl").
-export([module/2]).
-export([start/0]).
-export([silent/1]).
-export([write_counterexamples/1, write_counterexamples/2, write_counterexamples/3]).
-export([write_counterexample/3, write_counterexample/4]).
-export([eunit_module/1, eunit_module/2, eunit_module/3, eunit_module/4]).
-ifdef(PROPER).
%% @doc PropER has a different API than EQC
module(Options, Module) ->
proper:module(Module, Options).
%% @doc PropER doesn't have a server. Always return true.
start() ->
true.
-endif. %% -ifdef(PROPER).
-ifdef(EQC).
%% @doc Same API as EQC
module(Options, Module) ->
eqc:module(Options, Module).
%% @doc Starts (and possibly restarts) the QuickCheck server. If
%% another instance is not running, start the server and return the
%% server's process id. If another instance is already running,
%% return true. Otherwise, forcefully restart the server.
start() ->
try
eqc:start(false)
catch
exit:already_running ->
true;
_X:_Y ->
eqc:start(true)
end.
-endif. %% -ifdef(EQC).
%% @doc Disable QuickCheck's test output (i.e. the "dots")
silent(Prop) ->
on_output(silent_printer(), Prop).
silent_printer() ->
Filter = fun($.) -> false; (_) -> true end,
fun(Fmt, Args) -> io:format(lists:filter(Filter,Fmt), Args), ok end.
%% @doc Write failing counterexamples for specified Module
write_counterexamples(Module) ->
write_counterexamples(Module, ?QC:counterexamples()).
write_counterexamples(Module, CounterExamples) ->
write_counterexamples(Module, CounterExamples, calendar:local_time()).
-ifdef(PROPER).
write_counterexamples(Module, CounterExamples, LocalTime) ->
[ write_counterexample(Module, Prop, CE, LocalTime) || {{_Mod,Prop,_Arity}, CE} <- CounterExamples ].
-endif. %% -ifdef(PROPER).
-ifdef(EQC).
write_counterexamples(Module, CounterExamples, LocalTime) ->
[ write_counterexample(Module, Prop, CE, LocalTime) || {Prop, CE} <- CounterExamples ].
-endif. %% -ifdef(EQC).
write_counterexample(Module, Prop, CounterExample) ->
write_counterexample(Module, Prop, CounterExample, calendar:local_time()).
write_counterexample(Module, Prop, CounterExample, {{Year,Month,Day},{Hour,Minute,Second}}) ->
Fmt = "~s:~s-~4..0B~2..0B~2..0B-~2..0B~2..0B~2..0B.erl",
Args = [Module,Prop,Year,Month,Day,Hour,Minute,Second],
FileName = lists:flatten(io_lib:format(Fmt, Args)),
ok = file:write_file(FileName, io_lib:format("~p.", [CounterExample])),
FileName.
%% @doc Wrap module as an EUnit test fixture
eunit_module(Module) ->
eunit_module(Module, 3000).
eunit_module(Module, NumTests) ->
eunit_module(Module, NumTests, 60).
eunit_module(Module, NumTests, Timeout) ->
eunit_module(Module, NumTests, Timeout, fun() -> noop end).
eunit_module(Module, NumTests, Timeout, Teardown) ->
{setup, local, fun() -> eunit_setup(Module) end
, fun(Mod) -> Teardown(), eunit_teardown(Mod) end
, {timeout, Timeout, [fun() -> eunit_run(Module, NumTests) end]}
}.
eunit_setup(Module) ->
start(),
Module.
eunit_teardown(Module) ->
?assertEqual([], write_counterexamples(Module)).
-ifdef(PROPER).
eunit_run(Module, NumTests) ->
erlang:group_leader(whereis(user), self()),
module([{numtests,NumTests}, noshrink, {on_output,silent_printer()}], Module).
-endif. %% -ifdef(PROPER).
-ifdef(EQC).
eunit_run(Module, NumTests) ->
erlang:group_leader(whereis(user), self()),
module([{numtests,NumTests}, fun ?QC_GEN:noshrink/1, {on_output,silent_printer()}], Module).
-endif. %% -ifdef(EQC).
-endif. %% -ifdef(QC).