-
Notifications
You must be signed in to change notification settings - Fork 19
/
format_test.exs
175 lines (153 loc) · 3.5 KB
/
format_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
defmodule Apex.Format.Test do
use ExUnit.Case
import Apex.Format
test "Can format references" do
reference = make_ref()
assert format(reference, color: false) =~ ~r/#Reference<\d+\.\d+\.\d+\.\d+>/
end
test "Can format a list of strings" do
assert format(~w(a b c d), color: false) == """
[
[0] "a"
[1] "b"
[2] "c"
[3] "d"
]
"""
end
test "Can format a list containing a list" do
assert format(["a", "b", ["c", "d"]], color: false) == """
[
[0] "a"
[1] "b"
[2] [
[0] "c"
[1] "d"
]
]
"""
end
test "Can format integers" do
assert format(1, color: false) == "1\n"
end
test "Can format floats" do
assert format(1.1, color: false) == "1.1\n"
end
test "Can format atoms" do
assert format(true, color: false) == "true\n"
assert format(false, color: false) == "false\n"
end
test "Can format keyword list" do
assert format([first_name: "John", last_name: "Doe"], color: false) == """
[
[0] first_name: "John"
[1] last_name: "Doe"
]
"""
end
test "Can format empty tuples" do
assert format({}, color: false) == "{}\n"
end
test "Can format tuples that don't start with an atom" do
assert format({1, "John", 1, true}, color: false) == """
{
[0] 1
[1] "John"
[2] 1
[3] true
}
"""
end
test "Can format tuples that start with an atom" do
assert format({:name, "John"}, color: false) == """
{
[0] :name
[1] "John"
}
"""
end
test "Can format second level tuples in a map" do
map = %{
some_key: {:name, "John"}
}
assert format(map, color: false) == """
%{
some_key: {
[0] :name
[1] "John"
}
}
"""
end
test "Can format ranges" do
assert format(1..2, color: false) == """
#Elixir.Range 1..2
"""
end
test "Can format pid" do
assert format(self(), color: false) =~ ~r(#PID<0.\d+.0>\n)
end
test "Can format port" do
assert format(Port.list |> List.first, color: false) =~ ~r(#Port<0.\d+>\n)
end
test "Can format function" do
f = fn(a) -> "#{a}" end
assert format(f, color: false) =~ ~r(#Function<0.\d+/1 in Apex.Format.Test."test Can format function"/1>\n)
end
test "Can format MapSets" do
set = MapSet.new |> MapSet.put(:foo) |> MapSet.put(:baz)
assert format(set, color: false) == """
MapSet<[
:baz
:foo
]>
"""
end
require Record
Record.defrecord :user, [first_name: nil, last_name: nil]
test "Can format records" do
assert format(user(first_name: "John", last_name: "Doe"), color: false) == """
user{
[0] "John"
[1] "Doe"
}
"""
end
test "Can format Maps" do
map = %{foo: "bar", bar: "baz"}
assert format(map, color: false) == """
%{
bar: "baz"
foo: "bar"
}
"""
end
defmodule TestStruct, do: defstruct first_name: nil, last_name: nil
test "Can format structs" do
assert format(%TestStruct{first_name: "John", last_name: "Doe"} , color: false) == """
%Elixir.Apex.Format.Test.TestStruct{
first_name: "John"
last_name: "Doe"
}
"""
end
test "Can turn off list numbering" do
data = [false, 42, ~w(forty two), [time: "now"], %{foo: :bar}]
assert format(data, numbers: false, color: false) == """
[
false
42
[
"forty"
"two"
]
[
time: "now"
]
%{
foo: :bar
}
]
"""
end
end