This repository was archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest
More file actions
executable file
·40 lines (34 loc) · 1.22 KB
/
test
File metadata and controls
executable file
·40 lines (34 loc) · 1.22 KB
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
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -pa deps/jsx/ebin -pa deps/jsonpointer/ebin -pa ebin
-mode(compile).
-include_lib("eunit/include/eunit.hrl").
% tests from https://github.com/json-patch/json-patch-tests
main(Args) ->
{ok, TestsBin} = file:read_file("./json-patch-tests/tests.json"),
{ok, SpecTestsBin} = file:read_file("./json-patch-tests/spec_tests.json"),
Tests = json:from_binary(TestsBin) ++ json:from_binary(SpecTestsBin),
eunit:test(lists:map(
fun(Test) -> do(Test) end,
% drop disabled tests
lists:dropwhile(fun(Map) ->
case maps:find(<<"disabled">>, Map) of
{ok, true} -> true;
error -> false
end
end, Tests)
), case Args of ["-v"] -> [verbose]; _ -> [] end).
do(Test) ->
try maps:get(<<"error">>, Test), ?_assertError(badarg, patch(Test))
catch error:bad_key -> ?_assertEqual(expected(Test), patch(Test))
end.
patch(Test) ->
Patch = maps:get(<<"patch">>, Test),
Doc = maps:get(<<"doc">>, Test),
json:patch(Patch, Doc).
% if there's no `expected` key in the test object i guess we are
% meant to check that the doc is unmodified?
expected(Test) ->
try maps:get(<<"expected">>, Test)
catch error:bad_key -> maps:get(<<"doc">>, Test)
end.