Permalink
Browse files

Add some primitive PropEr string tests

  • Loading branch information...
1 parent ad7b632 commit 5e268258b6fef26f7aa41e586ae7dd17274e3071 @yrashk yrashk committed Aug 27, 2011
Showing with 134 additions and 3 deletions.
  1. +2 −0 .gitignore
  2. +1 −1 Makefile
  3. +7 −2 rebar.config
  4. +124 −0 test/erlv8_tests.erl
View
2 .gitignore
@@ -3,3 +3,5 @@ ebin/*.beam
priv/*.so
c_src/*.o
.eunit
+deps/proper
+deps/proper_stdlib
View
2 Makefile
@@ -39,7 +39,7 @@ deps/zeromq2/src/.libs/libzmq.a: deps/zeromq2/.git/HEAD
dependencies: deps/v8/libv8.a deps/zeromq2/src/.libs/libzmq.a
test: compile
- @./rebar eunit
+ @./rebar eunit skip_deps=true
compile: dependencies
@EXTRA_CFLAGS= ./rebar compile
View
9 rebar.config
@@ -1,7 +1,12 @@
{lib_dirs, ["deps/eqc/ebin"]}.
-{erl_opts, [{i, ".."},{i,"deps"}]}.
+{erl_opts, [{i, ".."},{i,"deps"},{i,"../proper/include"},{i,"deps/proper/include"}]}.
{port_sources,["c_src/*.cc"]}.
{port_envs,
[{"DRV_LDFLAGS","deps/v8/libv8.a deps/zeromq2/src/.libs/libzmq.a -shared $ERL_LDFLAGS -lstdc++ -luuid"},
{"darwin", "DRV_LDFLAGS", "deps/v8/libv8.a deps/zeromq2/src/.libs/libzmq.a -bundle -flat_namespace -undefined suppress $ERL_LDFLAGS"},
- {"DRV_CFLAGS","-Ic_src -Ideps/v8/include -Ideps/zeromq2/include -g -Wall -fPIC $ERL_CFLAGS $EXTRA_CFLAGS"}]}.
+ {"DRV_CFLAGS","-Ic_src -Ideps/v8/include -Ideps/zeromq2/include -g -Wall -fPIC $ERL_CFLAGS $EXTRA_CFLAGS"}]}.
+{deps,
+ [
+ {proper, ".*", {git, "https://github.com/manopapad/proper.git", {branch, "master"}}},
+ {proper_stdlib, ".*", {git, "https://github.com/spawngrid/proper_stdlib.git", {branch, "master"}}}
+ ]}.
View
124 test/erlv8_tests.erl
@@ -0,0 +1,124 @@
+-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:binary_string(list(proper_stdgen:utf8_char()))]).
+
+%%% properties
+
+%% type conversion
+prop_to_js_string() ->
+ ?FORALL(String, js_string(),
+ begin
+ [{vm, VM}] = ets:lookup(?MODULE, vm),
+ Obj = erlv8_vm:taint(VM, String),
+ Obj =:= iolist_to_binary(String)
+ 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())}}
+ ]}].

0 comments on commit 5e26825

Please sign in to comment.