-
Notifications
You must be signed in to change notification settings - Fork 1
/
decoding_test.exs
101 lines (78 loc) · 2.08 KB
/
decoding_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
defmodule DecodingTest do
alias AlchemyTable.Decoding
use ExUnit.Case
doctest Decoding
describe "Decoding.decode/1 - from string" do
test "should decode integers" do
assert decode_string(:integer, "1") == 1
end
test "should decode lists" do
string = "\[1, 2, 3\]"
expected = [1, 2, 3]
assert decode_string(:list, string) == expected
end
test "should decode maps" do
string = "{\"key\": true}"
expected = %{key: true}
assert decode_string(:map, string) == expected
end
test "should decode floats" do
assert decode_string(:float, "24.2") == 24.2
end
test "should decode booleans" do
assert decode_string(:boolean, "true") == true
assert decode_string(:boolean, "false") == false
end
test "should decode strings" do
assert decode_string(:string, "value") == "value"
end
end
describe "Decoding.decode/1 - from bytes" do
test "should decode integers" do
result =
<<1::integer-signed-64>>
|> decode_bytes(:integer)
assert result == 1
end
test "should decode lists" do
result =
"\[1, 2, 3\]"
|> decode_bytes(:list)
assert result == [1, 2, 3]
end
test "should decode maps" do
result =
"{\"key\": true}"
|> decode_bytes(:map)
assert result == %{key: true}
end
test "should decode floats" do
result =
<<24.2::float-signed-64>>
|> decode_bytes(:float)
assert result == 24.2
end
test "should decode booleans" do
t_result =
<<1>>
|> decode_bytes(:boolean)
f_result =
<<0>>
|> decode_bytes(:boolean)
assert t_result == true
assert f_result == false
end
test "should decode strings" do
result =
<<"value">>
|> decode_bytes(:string)
assert result == "value"
end
end
defp decode_string(type, string) do
Decoding.decode(type, string, mode: :string)
end
defp decode_bytes(bytes, type) do
Decoding.decode(type, bytes, mode: :bytes)
end
end