浏览代码

Unit tests for pre-encoded JSON.

pull/140/head
David Hull 8 年前
父节点
当前提交
654a8a480e
共有 2 个文件被更改,包括 57 次插入1 次删除
  1. +4
    -1
      src/jiffy.erl
  2. +53
    -0
      test/jiffy_18_preencode_tests.erl

+ 4
- 1
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()}.

+ 53
- 0
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"}
%% ].

正在加载...
取消
保存