forked from michalmuskala/jason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty_test.exs
75 lines (65 loc) · 2.09 KB
/
property_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
if Code.ensure_loaded?(ExUnitProperties) do
defmodule Jason.PropertyTest do
use ExUnit.Case, async: true
use ExUnitProperties
property "string rountrip" do
check all string <- string(:printable) do
assert decode(encode(string)) == string
end
end
property "integer roundtrip" do
check all integer <- integer() do
assert decode(encode(integer)) == integer
end
end
property "float roundtrip" do
check all float <- float() do
assert decode(encode(float)) == float
end
end
property "string-keyed objects roundrtip" do
check all json <- json(string(:printable)) do
assert decode(encode(json)) == json
end
end
property "atom-keyed objects roundtrip" do
check all json <- json(atom(:alphanumeric)) do
assert decode(encode(json), keys: :atoms!) == json
end
end
property "unicode escaping" do
check all string <- string(:printable) do
encoded = encode(string, escape: :unicode)
for << <<byte>> <- encoded >> do
assert byte < 127
end
assert decode(encoded) == string
end
end
property "html_safe escaping" do
check all string <- string(:printable) do
encoded = encode(string, escape: :html_safe)
refute encoded =~ <<0x2028::utf8>>
refute encoded =~ <<0x2029::utf8>>
refute encoded =~ ~r"(?<!\\)/"
assert decode(encoded) == string
end
end
property "javascript escaping" do
check all string <- string(:printable) do
encoded = encode(string, escape: :javascript)
refute encoded =~ <<0x2028::utf8>>
refute encoded =~ <<0x2029::utf8>>
assert decode(encoded) == string
end
end
defp decode(data, opts \\ []), do: Jason.decode!(data, opts)
defp encode(data, opts \\ []), do: Jason.encode!(data, opts)
defp json(keys) do
simple = one_of([integer(), float(), string(:printable), boolean(), nil])
tree(simple, fn json ->
one_of([list_of(json), map_of(keys, json)])
end)
end
end
end