From 654a8a480e1f95ba410df7bc32cbfc2925dc1ca0 Mon Sep 17 00:00:00 2001 From: David Hull Date: Thu, 16 Mar 2017 15:59:02 +0000 Subject: [PATCH] Unit tests for pre-encoded JSON. --- src/jiffy.erl | 5 ++- test/jiffy_18_preencode_tests.erl | 53 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 test/jiffy_18_preencode_tests.erl diff --git a/src/jiffy.erl b/src/jiffy.erl index d42c22c..88c1925 100644 --- a/src/jiffy.erl +++ b/src/jiffy.erl @@ -16,7 +16,8 @@ | json_string() | json_number() | json_object() - | json_array(). + | json_array() + | json_preencoded(). -type json_array() :: [json_value()]. -type json_string() :: atom() | binary(). @@ -33,6 +34,8 @@ -endif. +-type json_preencoded() :: {json, Json::iodata()}. + -type jiffy_decode_result() :: json_value() | {has_trailer, json_value(), binary()}. diff --git a/test/jiffy_18_preencode_tests.erl b/test/jiffy_18_preencode_tests.erl new file mode 100644 index 0000000..5bc36bf --- /dev/null +++ b/test/jiffy_18_preencode_tests.erl @@ -0,0 +1,53 @@ +-module(jiffy_18_preencode_tests). + + +-include_lib("eunit/include/eunit.hrl"). +-include("jiffy_util.hrl"). + + +preencode_success_test_() -> + [gen(ok, Case) || Case <- cases(ok)]. + + +%% preencode_failure_test_() -> +%% [gen(error, Case) || Case <- cases(error)]. + + +gen(ok, {E1, J, E2}) -> + {msg("~p", [E1]), [ + {"Encode", ?_assertEqual(J, enc(E1))}, + {"Decode", ?_assertEqual(E2, dec(J))} + ]}. + +%% gen(error, E) -> +%% {msg("Error: ~p", [E]), [ +%% ?_assertThrow({error, _}, enc(E)) +%% ]}. + + +cases(ok) -> + TopTests = + lists:map( + fun (EJSON) -> + JSON = enc(EJSON), + {{json, JSON}, JSON, EJSON} + end, [ 123 + , <<"hello world">> + , true + , false + , {[ {<<"a">>, <<"apple">>}, {<<"b">>, <<"banana">>} ]} + ]), + EJSON = [ 1, <<"a">> ], + JSON = enc(EJSON), + BuriedTests = + [ { [ {json, JSON} ], <<"[[1,\"a\"]]">>, [ EJSON ]} + , { [ 1, {json, JSON}, 3 ], <<"[1,[1,\"a\"],3]">>, [ 1, EJSON, 3 ]} + , { [ {json, JSON}, {json, JSON} ], <<"[[1,\"a\"],[1,\"a\"]]">>, [ EJSON, EJSON ]} + , { {[ {<<"a">>, {json, JSON}} ]}, <<"{\"a\":[1,\"a\"]}">>, {[ {<<"a">>, EJSON} ]}} + ], + TopTests ++ BuriedTests. + +%% cases(error) -> +%% [ {json, true} +%% , {json, "true"} +%% ].