This commit is contained in:
alisdair sullivan 2012-03-27 16:46:43 -07:00
commit bd6202e618
43 changed files with 1473 additions and 628 deletions

View file

@ -33,15 +33,17 @@
-type opts() :: list().
-spec to_term(Source::binary(), Opts::opts()) -> list({binary(), any()})
| list(any())
-type json_value() :: list({binary(), json_value()})
| list(json_value())
| true
| false
| null
| integer()
| float()
| binary().
-spec to_term(Source::binary(), Opts::opts()) -> json_value().
to_term(Source, Opts) when is_list(Opts) ->
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts)))(Source).
@ -109,35 +111,29 @@ format_key(Key, Opts) ->
basic_test_() ->
[
{"empty object", ?_assert(to_term(<<"{}">>, []) =:= [{}])},
{"simple object", ?_assert(to_term(<<"{\"key\": true}">>, []) =:= [{<<"key">>, true}])},
{"less simple object",
?_assert(to_term(<<"{\"a\": 1, \"b\": 2}">>, []) =:= [{<<"a">>, 1}, {<<"b">>, 2}])
},
{"nested object",
?_assert(to_term(<<"{\"key\": {\"key\": true}}">>, []) =:= [{<<"key">>, [{<<"key">>, true}]}])
},
{"empty object", ?_assertEqual(to_term(<<"{}">>, []), [{}])},
{"simple object", ?_assertEqual(to_term(<<"{\"key\": true}">>, []), [{<<"key">>, true}])},
{"less simple object", ?_assertEqual(
to_term(<<"{\"a\": 1, \"b\": 2}">>, []),
[{<<"a">>, 1}, {<<"b">>, 2}]
)},
{"nested object", ?_assertEqual(
to_term(<<"{\"key\": {\"key\": true}}">>, []),
[{<<"key">>, [{<<"key">>, true}]}]
)},
{"empty array", ?_assert(to_term(<<"[]">>, []) =:= [])},
{"list of lists",
?_assert(to_term(<<"[[],[],[]]">>, []) =:= [[], [], []])
},
{"list of strings",
?_assert(to_term(<<"[\"hi\", \"there\"]">>, []) =:= [<<"hi">>, <<"there">>])
},
{"list of numbers",
?_assert(to_term(<<"[1, 2.0, 3e4, -5]">>, []) =:= [1, 2.0, 3.0e4, -5])
},
{"list of literals",
?_assert(to_term(<<"[true,false,null]">>, []) =:= [true,false,null])
},
{"list of objects",
?_assert(to_term(<<"[{}, {\"a\":1, \"b\":2}, {\"key\":[true,false]}]">>, [])
=:= [[{}], [{<<"a">>,1},{<<"b">>,2}], [{<<"key">>,[true,false]}]])
}
{"list of lists", ?_assertEqual(to_term(<<"[[],[],[]]">>, []), [[], [], []])},
{"list of strings", ?_assertEqual(to_term(<<"[\"hi\", \"there\"]">>, []), [<<"hi">>, <<"there">>])},
{"list of numbers", ?_assertEqual(to_term(<<"[1, 2.0, 3e4, -5]">>, []), [1, 2.0, 3.0e4, -5])},
{"list of literals", ?_assertEqual(to_term(<<"[true,false,null]">>, []), [true,false,null])},
{"list of objects", ?_assertEqual(
to_term(<<"[{}, {\"a\":1, \"b\":2}, {\"key\":[true,false]}]">>, []),
[[{}], [{<<"a">>,1},{<<"b">>,2}], [{<<"key">>,[true,false]}]]
)}
].
comprehensive_test_() ->
{"comprehensive test", ?_assert(to_term(comp_json(), []) =:= comp_term())}.
{"comprehensive test", ?_assertEqual(to_term(comp_json(), []), comp_term())}.
comp_json() ->
<<"[
@ -164,7 +160,7 @@ comp_term() ->
].
atom_labels_test_() ->
{"atom labels test", ?_assert(to_term(comp_json(), [{labels, atom}]) =:= atom_term())}.
{"atom labels test", ?_assertEqual(to_term(comp_json(), [{labels, atom}]), atom_term())}.
atom_term() ->
[
@ -180,10 +176,10 @@ atom_term() ->
naked_test_() ->
[
{"naked integer", ?_assert(to_term(<<"123">>, []) =:= 123)},
{"naked float", ?_assert(to_term(<<"-4.32e-17">>, []) =:= -4.32e-17)},
{"naked literal", ?_assert(to_term(<<"true">>, []) =:= true)},
{"naked string", ?_assert(to_term(<<"\"string\"">>, []) =:= <<"string">>)}
{"naked integer", ?_assertEqual(to_term(<<"123">>, []), 123)},
{"naked float", ?_assertEqual(to_term(<<"-4.32e-17">>, []), -4.32e-17)},
{"naked literal", ?_assertEqual(to_term(<<"true">>, []), true)},
{"naked string", ?_assertEqual(to_term(<<"\"string\"">>, []), <<"string">>)}
].
-endif.