/
erlv8_tests.erl
132 lines (103 loc) · 3.46 KB
/
erlv8_tests.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
-module(erlv8_tests).
-include_lib("proper/include/proper.hrl").
-include_lib("eunit/include/eunit.hrl").
-behaviour(proper_statem).
-export([initial_state/0, command/1, precondition/2, postcondition/3,
next_state/3]).
-compile(export_all).
-record(state, {
vms = [],
passed = []
}).
%% helpers
start_vm() ->
{ok, VM} = erlv8_vm:start(),
VM.
exit_vm(VM) ->
erlv8_vm:stop(VM),
VM.
run_tests(VM) ->
ets:insert(?MODULE, {vm, VM}),
proper:module(?MODULE,
[{'on_output',
fun(Format, Data) ->
io:format(standard_error, Format, Data)
end},
{numtests, 100}]).
%% statem
initial_state() ->
#state{}.
command(#state{ vms = VMs, passed = Passed }) ->
frequency([
{1 - (length(VMs) + length(Passed)), {call, ?MODULE, start_vm, []}},
{length(Passed), {call, ?MODULE, exit_vm, [oneof(Passed)]}},
{length(VMs), {call, ?MODULE, run_tests, [oneof(VMs)]}}
]).
precondition(_State, _Call) ->
true.
postcondition(_S, {call, ?MODULE, run_tests, [_VM]}, []) ->
true;
postcondition(_S, {call, ?MODULE, run_tests, [_VM]}, _) ->
false;
postcondition(_S, _C, _R) ->
true.
next_state(#state{ vms = VMs } = State, V, {call, ?MODULE, start_vm, []}) ->
State#state{ vms = [V|VMs] };
next_state(#state{ passed = VMs } = State, _V, {call, ?MODULE, exit_vm, [VM]}) ->
State#state{ passed = VMs -- [VM] };
next_state(#state{ vms = VMs, passed = Passed } = State, _V, {call, ?MODULE, run_tests, [VM]}) ->
State#state{ vms = VMs -- [VM], passed = [VM|Passed] };
next_state(State, _V, _C) ->
State.
statem() ->
?FORALL(Cmds, commands(?MODULE),
?TRAPEXIT(
begin
{History,State,Result} = run_commands(?MODULE, Cmds),
[ erlv8_vm:stop(VM) || VM <- State#state.vms ],
?WHENFAIL(io:format("History: ~p\nState: ~p\nResult: ~p\n",
[History, State, Result]),
aggregate(command_names(Cmds), Result =:= ok))
end)).
%%% generators
js_string() ->
oneof([list(proper_stdgen:utf8_char()),
proper_stdgen:utf8_bin()]).
%%% properties
%% type conversion
prop_to_js_string() ->
?FORALL(String, js_string(),
begin
[{vm, VM}] = ets:lookup(?MODULE, vm),
Bin = unicode:characters_to_binary(String),
Obj = erlv8_vm:taint(VM, Bin),
case Obj =:= Bin of
true ->
ok;
false ->
io:format("->~p/~p~n", [Bin, Obj]),
ok
end,
Obj =:= Bin
end).
%% eunit
t_properties() ->
?assertEqual(true, proper:quickcheck(statem(),
[{'on_output',
fun(Format, Data) ->
io:format(standard_error, Format, Data)
end},
{numtests, 10}])).
erlv8_test_() ->
[{setup,
fun() ->
ets:new(?MODULE,[named_table, public]),
ok = application:start(erlv8)
end,
fun(_) ->
ets:delete(?MODULE),
application:stop(erlv8)
end,
[
{timeout, 30, {"PropEr tests", ?_test(t_properties())}}
]}].