-
Notifications
You must be signed in to change notification settings - Fork 167
/
encode_test.exs
211 lines (170 loc) · 6.33 KB
/
encode_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
defmodule Jason.EncoderTest do
use ExUnit.Case, async: true
alias Jason.{EncodeError, Encoder}
test "atom" do
assert to_json(nil) == "null"
assert to_json(true) == "true"
assert to_json(false) == "false"
assert to_json(:poison) == ~s("poison")
end
test "integer" do
assert to_json(42) == "42"
end
test "float" do
assert to_json(99.99) == "99.99"
assert to_json(9.9e100) == "9.9e100"
end
test "binaries" do
assert to_json("hello world") == ~s("hello world")
assert to_json("hello\nworld") == ~s("hello\\nworld")
assert to_json("\nhello\nworld\n") == ~s("\\nhello\\nworld\\n")
assert to_json("\"") == ~s("\\"")
assert to_json("\0") == ~s("\\u0000")
assert to_json(<<31>>) == ~s("\\u001F")
assert to_json("☃a", escape: :unicode_safe) == ~s("\\u2603a")
assert to_json("𝄞b", escape: :unicode_safe) == ~s("\\uD834\\uDD1Eb")
assert to_json("\u2028\u2029abc", escape: :javascript_safe) == ~s("\\u2028\\u2029abc")
assert to_json("</script>", escape: :html_safe) == ~s("\\u003C\\/script>")
assert to_json(~s(<script>var s = "\u2028\u2029";</script>), escape: :html_safe) == ~s("\\u003Cscript>var s = \\\"\\u2028\\u2029\\\";\\u003C\\/script>")
assert to_json("<!-- fake comment", escape: :html_safe) == ~s("\\u003C!-- fake comment")
assert to_json("áéíóúàèìòùâêîôûãẽĩõũ") == ~s("áéíóúàèìòùâêîôûãẽĩõũ")
assert to_json("a\u2028a", escape: :javascript_safe) == ~s("a\\u2028a")
assert to_json("a\u2028a", escape: :html_safe) == ~s("a\\u2028a")
assert_raise Protocol.UndefinedError, fn ->
to_json(<<0::1>>)
end
# Poison-compatible escape options
assert to_json("a\u2028a", escape: :javascript) == ~s("a\\u2028a")
assert to_json("☃a", escape: :unicode) == ~s("\\u2603a")
end
test "Map" do
assert to_json(%{}) == "{}"
assert to_json(%{"foo" => "bar"}) == ~s({"foo":"bar"})
assert to_json(%{foo: :bar}) == ~s({"foo":"bar"})
assert to_json(%{42 => :bar}) == ~s({"42":"bar"})
assert to_json(%{'foo' => :bar}) == ~s({"foo":"bar"})
multi_key_map = %{"foo" => "foo1", :foo => "foo2"}
assert_raise EncodeError, "duplicate key: foo", fn ->
to_json(multi_key_map, maps: :strict)
end
assert to_json(multi_key_map) == ~s({"foo":"foo2","foo":"foo1"})
end
test "list" do
assert to_json([]) == "[]"
assert to_json([1, 2, 3]) == "[1,2,3]"
end
test "Time" do
{:ok, time} = Time.new(12, 13, 14)
assert to_json(time) == ~s("12:13:14")
end
test "Date" do
{:ok, date} = Date.new(2000, 1, 1)
assert to_json(date) == ~s("2000-01-01")
end
test "NaiveDateTime" do
{:ok, datetime} = NaiveDateTime.new(2000, 1, 1, 12, 13, 14)
assert to_json(datetime) == ~s("2000-01-01T12:13:14")
end
test "DateTime" do
datetime = %DateTime{year: 2000, month: 1, day: 1, hour: 12, minute: 13, second: 14,
microsecond: {0, 0}, zone_abbr: "CET", time_zone: "Europe/Warsaw",
std_offset: -1800, utc_offset: 3600}
assert to_json(datetime) == ~s("2000-01-01T12:13:14+00:30")
datetime = %DateTime{year: 2000, month: 1, day: 1, hour: 12, minute: 13, second: 14,
microsecond: {50000, 3}, zone_abbr: "UTC", time_zone: "Etc/UTC",
std_offset: 0, utc_offset: 0}
assert to_json(datetime) == ~s("2000-01-01T12:13:14.050Z")
end
test "Decimal" do
decimal = Decimal.new("1.0")
assert to_json(decimal) == ~s("1.0")
end
defmodule Derived do
@derive Encoder
defstruct name: ""
end
defmodule DerivedUsingOnly do
@derive {Encoder, only: [:name]}
defstruct name: "", size: 0
end
defmodule DerivedUsingExcept do
@derive {Encoder, except: [:name]}
defstruct name: "", size: 0
end
defmodule NonDerived do
defstruct name: ""
end
test "@derive" do
derived = %Derived{name: "derived"}
assert Encoder.impl_for!(derived) == Encoder.Jason.EncoderTest.Derived
assert Jason.decode!(to_json(derived)) == %{"name" => "derived"}
non_derived = %NonDerived{name: "non-derived"}
assert_raise Protocol.UndefinedError, fn ->
to_json(non_derived)
end
derived_using_only = %DerivedUsingOnly{name: "derived using :only", size: 10}
assert to_json(derived_using_only) == ~s({"name":"derived using :only"})
derived_using_except = %DerivedUsingExcept{name: "derived using :except", size: 10}
assert to_json(derived_using_except) == ~s({"size":10})
end
test "@derive validate `except:`" do
message = "`:except` specified keys ([:invalid]) that are not defined in defstruct: [:name]"
assert_raise ArgumentError, message, fn ->
Code.eval_string("""
defmodule InvalidExceptField do
@derive {Jason.Encoder, except: [:invalid]}
defstruct name: ""
end
""")
end
end
test "@derive validate `only:`" do
message = "`:only` specified keys ([:invalid]) that are not defined in defstruct: [:name]"
assert_raise ArgumentError, message, fn ->
Code.eval_string("""
defmodule InvalidOnlyField do
@derive {Jason.Encoder, only: [:invalid]}
defstruct name: ""
end
""")
end
end
defmodule KeywordTester do
defstruct [:baz, :foo, :quux]
end
defimpl Jason.Encoder, for: [KeywordTester] do
def encode(struct, opts) do
struct
|> Map.from_struct
|> Enum.map(&(&1))
|> Jason.Encode.keyword(opts)
end
end
test "using keyword list encoding" do
t = %KeywordTester{baz: :bar, foo: "bag", quux: 42}
assert to_json(t) == ~s({"baz":"bar","foo":"bag","quux":42})
end
test "EncodeError" do
assert_raise Protocol.UndefinedError, fn ->
to_json(self())
end
assert_raise EncodeError, "invalid byte 0x80 in <<128>>", fn ->
assert to_json(<<0x80>>)
end
assert_raise EncodeError, fn ->
assert to_json(<<?a, 208>>)
end
end
test "encode should not raise on Protocol.UndefinedError" do
assert {:error, %Protocol.UndefinedError{}} = Jason.encode(self())
end
test "pretty: true" do
assert to_json(%{a: 3.14159, b: 1}, pretty: true) == ~s|{\n "a": 3.14159,\n "b": 1\n}|
end
test "pretty: false" do
assert to_json(%{a: 3.14159, b: 1}, pretty: false) == ~s|{"a":3.14159,"b":1}|
end
defp to_json(value, opts \\ []) do
Jason.encode!(value, opts)
end
end