jsone_encode_tests.erl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. %% Copyright (c) 2013-2014, Takeru Ohta <phjgt308@gmail.com>
  2. %% coding: latin-1
  3. -module(jsone_encode_tests).
  4. -include_lib("eunit/include/eunit.hrl").
  5. encode_test_() ->
  6. [
  7. %% Symbols
  8. {"false",
  9. fun () ->
  10. ?assertEqual({ok, <<"false">>}, jsone_encode:encode(false))
  11. end},
  12. {"true",
  13. fun () ->
  14. ?assertEqual({ok, <<"true">>}, jsone_encode:encode(true))
  15. end},
  16. {"null",
  17. fun () ->
  18. ?assertEqual({ok, <<"null">>}, jsone_encode:encode(null))
  19. end},
  20. %% Numbers: Integer
  21. {"zero",
  22. fun () ->
  23. ?assertEqual({ok, <<"0">>}, jsone_encode:encode(0))
  24. end},
  25. {"positive integer",
  26. fun () ->
  27. ?assertEqual({ok, <<"1">>}, jsone_encode:encode(1))
  28. end},
  29. {"negative integer",
  30. fun () ->
  31. ?assertEqual({ok, <<"-1">>}, jsone_encode:encode(-1))
  32. end},
  33. {"large number",
  34. fun () ->
  35. ?assertEqual({ok, <<"11111111111111111111111111111111111111111111111111111111111111111111111">>},
  36. jsone_encode:encode(11111111111111111111111111111111111111111111111111111111111111111111111))
  37. end},
  38. %% Numbers: Float",
  39. {"float",
  40. fun () ->
  41. Input = 1.234,
  42. ?assertMatch({ok, _}, jsone_encode:encode(Input)),
  43. ?assertEqual(Input, binary_to_float(element(2, jsone_encode:encode(Input))))
  44. end},
  45. %% Strings
  46. {"simple string",
  47. fun () ->
  48. ?assertEqual({ok, <<"\"abc\"">>}, jsone_encode:encode(<<"abc">>))
  49. end},
  50. {"atom is regarded as string",
  51. fun () ->
  52. ?assertEqual({ok, <<"\"abc\"">>}, jsone_encode:encode(abc))
  53. end},
  54. {"string: contains escaped characters",
  55. fun () ->
  56. Input = <<"\"\/\\\b\f\n\r\t">>,
  57. Expected = list_to_binary([$", [[$\\, C] || C <- [$", $/, $\\, $b, $f, $n, $r, $t]], $"]),
  58. ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
  59. end},
  60. {"string: contains multi-byte (UTF-8 encoded) characters",
  61. fun () ->
  62. %% japanese
  63. Input1 = <<"あいうえお">>, % assumed that the encoding of this file is UTF-8
  64. Expected1 = <<"\"\\u3042\\u3044\\u3046\\u3048\\u304a\"">>,
  65. ?assertEqual({ok, Expected1}, jsone_encode:encode(Input1)),
  66. Expected12 = <<$", Input1/binary, $">>,
  67. ?assertEqual({ok, Expected12}, jsone_encode:encode(Input1, [native_utf8])),
  68. %% other multi-byte characters
  69. Input2 = <<"۝۞ႮႯ">>,
  70. Expected2 = <<"\"\\u06dd\\u06de\\u10ae\\u10af\"">>,
  71. ?assertEqual({ok, Expected2}, jsone_encode:encode(Input2)),
  72. Expected22 = <<$", Input2/binary, $">>,
  73. ?assertEqual({ok, Expected22}, jsone_encode:encode(Input2, [native_utf8]))
  74. end},
  75. {"string: containts surrogate pairs",
  76. fun () ->
  77. Input = <<"𢁉𢂚𢃼">>,
  78. Expected = <<"\"\\ud848\\udc49\\ud848\\udc9a\\ud848\\udcfc\"">>,
  79. ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
  80. end},
  81. %% Arrays
  82. {"simple array",
  83. fun () ->
  84. Input = [1, 2, 3],
  85. Expected = <<"[1,2,3]">>,
  86. ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
  87. end},
  88. {"empty array",
  89. fun () ->
  90. Input = [],
  91. Expected = <<"[]">>,
  92. ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
  93. end},
  94. %% Objects
  95. {"simple object",
  96. fun () ->
  97. Input1 = {[{<<"key">>, <<"value">>}, {<<"1">>, 2}]},
  98. Input2 = [{<<"key">>, <<"value">>}, {<<"1">>, 2}],
  99. Expected = <<"{\"key\":\"value\",\"1\":2}">>,
  100. ?assertEqual({ok, Expected}, jsone_encode:encode(Input1)),
  101. ?assertEqual({ok, Expected}, jsone_encode:encode(Input2))
  102. end},
  103. {"empty object",
  104. fun () ->
  105. Input1 = {[]},
  106. Input2 = [{}],
  107. Expected = <<"{}">>,
  108. ?assertEqual({ok, Expected}, jsone_encode:encode(Input1)),
  109. ?assertEqual({ok, Expected}, jsone_encode:encode(Input2))
  110. end},
  111. {"atom key is allowed",
  112. fun () ->
  113. Expected = <<"{\"key\":2}">>,
  114. ?assertEqual({ok, Expected}, jsone_encode:encode({[{key, 2}]}))
  115. end},
  116. {"non binary object member key is disallowed",
  117. fun () ->
  118. ?assertMatch({error, {badarg, _}}, jsone_encode:encode({[{1, 2}]})),
  119. ?assertMatch({error, {badarg, _}}, jsone_encode:encode({[{"1", 2}]}))
  120. end},
  121. %% Others
  122. {"compound data",
  123. fun () ->
  124. Input = [true, {[{<<"1">>, 2}, {<<"array">>, [[[[1]]], {[{<<"ab">>, <<"cd">>}]}, false]}]}, null],
  125. Expected = <<"[true,{\"1\":2,\"array\":[[[[1]]],{\"ab\":\"cd\"},false]},null]">>,
  126. ?assertEqual({ok, Expected}, jsone_encode:encode(Input))
  127. end},
  128. {"invalid value",
  129. fun () ->
  130. ?assertMatch({error, {badarg, _}}, jsone_encode:encode(self()))
  131. end}
  132. ].