jsone_decode_tests.erl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. %% Copyright (c) 2013-2015, Takeru Ohta <phjgt308@gmail.com>
  2. %% coding: latin-1
  3. -module(jsone_decode_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. -ifdef('NO_MAP_TYPE').
  6. -define(MAP_OBJECT_TYPE, tuple).
  7. -define(OBJ0, {[]}).
  8. -define(OBJ1(K, V), {[{K, V}]}).
  9. -define(OBJ2(K1, V1, K2, V2), {[{K1, V1}, {K2, V2}]}).
  10. -else.
  11. -define(MAP_OBJECT_TYPE, map).
  12. -define(OBJ0, #{}).
  13. -define(OBJ1(K, V), #{K => V}).
  14. -define(OBJ2(K1, V1, K2, V2), #{K1 => V1, K2 => V2}).
  15. -endif.
  16. decode_test_() ->
  17. [
  18. %% Symbols
  19. {"false",
  20. fun () ->
  21. ?assertEqual({ok, false, <<"">>}, jsone_decode:decode(<<"false">>))
  22. end},
  23. {"true",
  24. fun () ->
  25. ?assertEqual({ok, true, <<"">>}, jsone_decode:decode(<<"true">>))
  26. end},
  27. {"null",
  28. fun () ->
  29. ?assertEqual({ok, null, <<"">>}, jsone_decode:decode(<<"null">>))
  30. end},
  31. %% Numbers: Integer
  32. {"positive integer",
  33. fun () ->
  34. ?assertEqual({ok, 1, <<"">>}, jsone_decode:decode(<<"1">>))
  35. end},
  36. {"zero",
  37. fun () ->
  38. ?assertEqual({ok, 0, <<"">>}, jsone_decode:decode(<<"0">>))
  39. end},
  40. {"negative integer",
  41. fun () ->
  42. ?assertEqual({ok, -1, <<"">>}, jsone_decode:decode(<<"-1">>))
  43. end},
  44. {"large integer (no limit on size)",
  45. fun () ->
  46. ?assertEqual({ok, 111111111111111111111111111111111111111111111111111111111111111111111111111111, <<"">>},
  47. jsone_decode:decode(<<"111111111111111111111111111111111111111111111111111111111111111111111111111111">>))
  48. end},
  49. {"integer with leading zero (interpreted as zero and remaining binary)",
  50. fun () ->
  51. ?assertEqual({ok, 0, <<"0">>}, jsone_decode:decode(<<"00">>)),
  52. ?assertEqual({ok, 0, <<"1">>}, jsone_decode:decode(<<"01">>)),
  53. ?assertEqual({ok, 0, <<"1">>}, jsone_decode:decode(<<"-01">>))
  54. end},
  55. {"integer can't begin with an explicit plus sign",
  56. fun () ->
  57. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"+1">>))
  58. end},
  59. %% Numbers: Floats
  60. {"float: decimal notation",
  61. fun () ->
  62. ?assertEqual({ok, 1.23, <<"">>}, jsone_decode:decode(<<"1.23">>))
  63. end},
  64. {"float: exponential notation",
  65. fun () ->
  66. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"12345e-3">>)), % lower case 'e'
  67. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"12345E-3">>)), % upper case 'E'
  68. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"12345.0e-3">>)),
  69. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"0.12345E2">>)),
  70. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"0.12345e+2">>)), % exponent part can begin with plus sign
  71. ?assertEqual({ok, 12.345, <<"">>}, jsone_decode:decode(<<"0.12345E+2">>)),
  72. ?assertEqual({ok, -12.345, <<"">>}, jsone_decode:decode(<<"-0.012345e3">>))
  73. end},
  74. {"float: invalid format",
  75. fun () ->
  76. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<".123">>)), % omitted integer part
  77. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.">>)), % omitted fraction part: EOS
  78. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.e+3">>)), % omitted fraction part: with exponent part
  79. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e">>)), % imcomplete fraction part
  80. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e-">>)), % imcomplete fraction part
  81. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1ee-1">>)), % duplicated 'e'
  82. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e--1">>)), % duplicated sign
  83. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"1e999">>)), % exponent out of range
  84. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"0.1e999">>)), % exponent out of range
  85. ?assertEqual({ok, 0.1, <<".2">>}, jsone_decode:decode(<<"0.1.2">>)) % duplicated '.': interpreted as individual tokens
  86. end},
  87. %% Strings
  88. {"simple string",
  89. fun () ->
  90. ?assertEqual({ok, <<"abc">>, <<"">>}, jsone_decode:decode(<<"\"abc\"">>))
  91. end},
  92. {"string: escaped characters",
  93. fun () ->
  94. Input = list_to_binary([$", [[$\\, C] || C <- [$", $/, $\\, $b, $f, $n, $r, $t]], $"]),
  95. Expected = <<"\"\/\\\b\f\n\r\t">>,
  96. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  97. end},
  98. {"string: escaped Unicode characters",
  99. fun () ->
  100. %% japanese
  101. Input1 = <<"\"\\u3042\\u3044\\u3046\\u3048\\u304A\"">>,
  102. Expected1 = <<"あいうえお">>, % assumed that the encoding of this file is UTF-8
  103. ?assertEqual({ok, Expected1, <<"">>}, jsone_decode:decode(Input1)),
  104. %% ascii
  105. Input2 = <<"\"\\u0061\\u0062\\u0063\"">>,
  106. Expected2 = <<"abc">>,
  107. ?assertEqual({ok, Expected2, <<"">>}, jsone_decode:decode(Input2)),
  108. %% other multi-byte characters
  109. Input3 = <<"\"\\u06DD\\u06DE\\u10AE\\u10AF\"">>,
  110. Expected3 = <<"۝۞ႮႯ">>,
  111. ?assertEqual({ok, Expected3, <<"">>}, jsone_decode:decode(Input3)),
  112. %% mixture of ascii and japanese characters
  113. Input4 = <<"\"a\\u30421\\u3044bb\\u304622\\u3048ccc\\u304A333\"">>,
  114. Expected4 = <<"aあ1いbbう22えcccお333">>, % assumed that the encoding of this file is UTF-8
  115. ?assertEqual({ok, Expected4, <<"">>}, jsone_decode:decode(Input4))
  116. end},
  117. {"string: surrogate pairs",
  118. fun () ->
  119. Input = <<"\"\\ud848\\udc49\\ud848\\udc9a\\ud848\\udcfc\"">>,
  120. Expected = <<"𢁉𢂚𢃼">>,
  121. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  122. end},
  123. {"string: control characters",
  124. fun () ->
  125. Ctrls = lists:seq(0, 16#1f),
  126. lists:foreach(
  127. fun (C) ->
  128. %% Control characters are unacceptable
  129. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<$", C, $">>))
  130. end,
  131. Ctrls),
  132. lists:foreach(
  133. fun (C) ->
  134. %% `allow_ctrl_chars' option allows strings which contain unescaped control characters
  135. ?assertEqual({ok, <<C>>, <<"">>}, jsone_decode:decode(<<$", C, $">>, [{allow_ctrl_chars, true}]))
  136. end,
  137. Ctrls)
  138. end},
  139. {"string: invalid escape characters",
  140. fun () ->
  141. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\z\"">>)), % '\z' is undefined
  142. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\uab\"">>)), % too few hex characters
  143. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\ud848\"">>)), % high(first) surrogate only
  144. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\udc49\"">>)), % low(second) surrogate only
  145. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(<<"\"\\ud848\\u0061\"">>)) % missing low(second) surrogate
  146. end},
  147. %% Arrays
  148. {"simple array",
  149. fun () ->
  150. Input = <<"[1,2,\"abc\",null]">>,
  151. Expected = [1, 2, <<"abc">>, null],
  152. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  153. end},
  154. {"array: contains whitespaces",
  155. fun () ->
  156. Input = <<"[ 1,\t2, \n \"abc\",\r null]">>,
  157. Expected = [1, 2, <<"abc">>, null],
  158. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  159. end},
  160. {"empty array",
  161. fun () ->
  162. ?assertEqual({ok, [], <<"">>}, jsone_decode:decode(<<"[]">>)),
  163. ?assertEqual({ok, [], <<"">>}, jsone_decode:decode(<<"[ \t\r\n]">>))
  164. end},
  165. {"array: trailing comma is disallowed",
  166. fun () ->
  167. Input = <<"[1, 2, \"abc\", null, ]">>,
  168. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  169. end},
  170. {"array: missing comma",
  171. fun () ->
  172. Input = <<"[1 2, \"abc\", null]">>, % a missing comma between '1' and '2'
  173. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  174. end},
  175. {"array: missing closing bracket",
  176. fun () ->
  177. Input = <<"[1, 2, \"abc\", null">>,
  178. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  179. end},
  180. %% Objects
  181. {"simple object",
  182. fun () ->
  183. Input = <<"{\"1\":2,\"key\":\"value\"}">>,
  184. Expected = ?OBJ2(<<"1">>, 2, <<"key">>, <<"value">>),
  185. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input)), % `map' is the default format
  186. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, ?MAP_OBJECT_TYPE}]))
  187. end},
  188. {"simple object: tuple or proplist",
  189. fun () ->
  190. Input = <<"{\"1\":2,\"key\":\"value\"}">>,
  191. Expected = {[{<<"1">>, 2},{<<"key">>, <<"value">>}]},
  192. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, tuple}])),
  193. ?assertEqual({ok, element(1, Expected), <<"">>}, jsone_decode:decode(Input, [{object_format, proplist}]))
  194. end},
  195. {"object: contains whitespaces",
  196. fun () ->
  197. Input = <<"{ \"1\" :\t 2,\n\r\"key\" : \n \"value\"}">>,
  198. Expected = ?OBJ2(<<"1">>, 2, <<"key">>, <<"value">>),
  199. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input))
  200. end},
  201. {"empty object",
  202. fun () ->
  203. ?assertEqual({ok, ?OBJ0, <<"">>}, jsone_decode:decode(<<"{}">>)),
  204. ?assertEqual({ok, ?OBJ0, <<"">>}, jsone_decode:decode(<<"{ \t\r\n}">>)),
  205. ?assertEqual({ok, {[]}, <<"">>}, jsone_decode:decode(<<"{}">>, [{object_format, tuple}])),
  206. ?assertEqual({ok, [{}], <<"">>}, jsone_decode:decode(<<"{}">>, [{object_format, proplist}]))
  207. end},
  208. {"empty object: map",
  209. fun () ->
  210. ?assertEqual({ok, ?OBJ0, <<"">>}, jsone_decode:decode(<<"{}">>, [{object_format, ?MAP_OBJECT_TYPE}]))
  211. end},
  212. {"duplicated members: map",
  213. fun () ->
  214. Input = <<"{\"1\":\"first\",\"1\":\"second\"}">>,
  215. case ?MAP_OBJECT_TYPE of
  216. map ->
  217. Expected = ?OBJ1(<<"1">>, <<"first">>), % the first (leftmost) value is used
  218. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, ?MAP_OBJECT_TYPE}]));
  219. tuple ->
  220. Expected = ?OBJ2(<<"1">>, <<"first">>, <<"1">>, <<"second">>),
  221. ?assertEqual({ok, Expected, <<"">>}, jsone_decode:decode(Input, [{object_format, ?MAP_OBJECT_TYPE}]))
  222. end
  223. end},
  224. {"object: trailing comma is disallowed",
  225. fun () ->
  226. Input = <<"{\"1\":2, \"key\":\"value\", }">>,
  227. io:format("~p\n", [catch jsone_decode:decode(Input)]),
  228. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input, [{object_format, tuple}]))
  229. end},
  230. {"object: missing comma",
  231. fun () ->
  232. Input = <<"{\"1\":2 \"key\":\"value\"}">>,
  233. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  234. end},
  235. {"object: missing field key",
  236. fun () ->
  237. Input = <<"{:2, \"key\":\"value\"}">>,
  238. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  239. end},
  240. {"object: non string key",
  241. fun () ->
  242. Input = <<"{1:2, \"key\":\"value\"}">>,
  243. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  244. end},
  245. {"object: missing field value",
  246. fun () ->
  247. Input = <<"{\"1\", \"key\":\"value\"}">>,
  248. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  249. end},
  250. {"object: missing closing brace",
  251. fun () ->
  252. Input = <<"{\"1\":2 \"key\":\"value\"">>,
  253. ?assertMatch({error, {badarg, _}}, jsone_decode:decode(Input))
  254. end},
  255. {"atom keys",
  256. fun () ->
  257. KeyOpt = fun(Keys) -> [{keys, Keys}, {object_format, proplist}]
  258. end,
  259. Input = <<"{\"foo\":\"ok\"}">>,
  260. ?assertEqual([{<<"foo">>, <<"ok">>}], jsone:decode(Input, KeyOpt(binary))),
  261. ?assertEqual([{foo, <<"ok">>}], jsone:decode(Input, KeyOpt(atom))),
  262. ?assertEqual([{foo, <<"ok">>}], jsone:decode(Input, KeyOpt(existing_atom))),
  263. ?assertError(badarg, jsone:decode(<<"{\"@#$%^!\":\"ok\"}">>, KeyOpt(existing_atom))),
  264. ?assertEqual([{foo, <<"ok">>}], jsone:decode(Input, KeyOpt(attempt_atom))),
  265. ?assertEqual([{<<"@#$%^!">>, <<"ok">>}], jsone:decode(<<"{\"@#$%^!\":\"ok\"}">>, KeyOpt(attempt_atom))),
  266. Value = integer_to_binary(rand:uniform(9999)),
  267. % do not make atom in test code
  268. [{Atom, <<"ok">>}] = jsone:decode(<<"{\"", Value/binary, "\":\"ok\"}">>, KeyOpt(atom)),
  269. ?assertEqual(Value, atom_to_binary(Atom, latin1))
  270. end},
  271. %% Others
  272. {"compound data",
  273. fun () ->
  274. Input = <<" [true, {\"1\" : 2, \"array\":[[[[1]]], {\"ab\":\"cd\"}, false]}, null] ">>,
  275. Expected = [true, ?OBJ2(<<"1">>, 2, <<"array">>, [[[[1]]], ?OBJ1(<<"ab">>, <<"cd">>), false]), null],
  276. ?assertEqual({ok, Expected, <<" ">>}, jsone_decode:decode(Input))
  277. end}
  278. ].