Skip to content

Commit 77373e1

Browse files
authored
Test: Add tests for LiveDebugger.Utils.TermParser (#335)
1 parent cc381c4 commit 77373e1

1 file changed

Lines changed: 358 additions & 0 deletions

File tree

test/utils/term_parser_test.exs

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
defmodule LiveDebugger.Utils.TermParserTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LiveDebugger.Utils.TermParser
5+
6+
defmodule TestStruct do
7+
defstruct [:field1, :field2]
8+
end
9+
10+
describe "term_to_display_tree/1" do
11+
test "parses a string term" do
12+
term = "Hello, World!"
13+
14+
expected = %{
15+
kind: "binary",
16+
children: nil,
17+
content: [%{text: "\"Hello, World!\"", color: "text-code-4"}],
18+
expanded_before: nil,
19+
expanded_after: nil
20+
}
21+
22+
assert TermParser.term_to_display_tree(term) == expected
23+
end
24+
25+
test "parses an atom term" do
26+
term = :hello
27+
28+
expected = %{
29+
kind: "atom",
30+
children: nil,
31+
content: [%{text: ":hello", color: "text-code-1"}],
32+
expanded_before: nil,
33+
expanded_after: nil
34+
}
35+
36+
assert TermParser.term_to_display_tree(term) == expected
37+
end
38+
39+
test "parses a number term" do
40+
term = 42
41+
42+
expected = %{
43+
kind: "number",
44+
children: nil,
45+
content: [%{text: "42", color: "text-code-1"}],
46+
expanded_before: nil,
47+
expanded_after: nil
48+
}
49+
50+
assert TermParser.term_to_display_tree(term) == expected
51+
end
52+
53+
test "parses a tuple term" do
54+
term = {:ok, "Hello"}
55+
56+
expected = %{
57+
kind: "tuple",
58+
children: [
59+
%{
60+
kind: "atom",
61+
children: nil,
62+
content: [%{text: ":ok", color: "text-code-1"}, %{text: ",", color: "text-code-2"}],
63+
expanded_before: nil,
64+
expanded_after: nil
65+
},
66+
%{
67+
kind: "binary",
68+
children: nil,
69+
content: [%{text: "\"Hello\"", color: "text-code-4"}],
70+
expanded_before: nil,
71+
expanded_after: nil
72+
}
73+
],
74+
content: [%{text: "{...}", color: "text-code-2"}],
75+
expanded_before: [%{text: "{", color: "text-code-2"}],
76+
expanded_after: [%{text: "}", color: "text-code-2"}]
77+
}
78+
79+
assert TermParser.term_to_display_tree(term) == expected
80+
end
81+
82+
test "parses empty tuple term" do
83+
term = {}
84+
85+
expected = %{
86+
kind: "tuple",
87+
children: nil,
88+
content: [%{text: "{}", color: "text-code-2"}],
89+
expanded_before: nil,
90+
expanded_after: nil
91+
}
92+
93+
assert TermParser.term_to_display_tree(term) == expected
94+
end
95+
96+
test "parses a list term" do
97+
term = [1, 2, 3]
98+
99+
expected = %{
100+
kind: "list",
101+
children: [
102+
%{
103+
kind: "number",
104+
children: nil,
105+
content: [%{text: "1", color: "text-code-1"}, %{text: ",", color: "text-code-2"}],
106+
expanded_before: nil,
107+
expanded_after: nil
108+
},
109+
%{
110+
kind: "number",
111+
children: nil,
112+
content: [%{text: "2", color: "text-code-1"}, %{text: ",", color: "text-code-2"}],
113+
expanded_before: nil,
114+
expanded_after: nil
115+
},
116+
%{
117+
kind: "number",
118+
children: nil,
119+
content: [%{text: "3", color: "text-code-1"}],
120+
expanded_before: nil,
121+
expanded_after: nil
122+
}
123+
],
124+
content: [%{text: "[...]", color: "text-code-2"}],
125+
expanded_before: [%{text: "[", color: "text-code-2"}],
126+
expanded_after: [%{text: "]", color: "text-code-2"}]
127+
}
128+
129+
assert TermParser.term_to_display_tree(term) == expected
130+
end
131+
132+
test "parses empty list term" do
133+
term = []
134+
135+
expected = %{
136+
kind: "list",
137+
children: nil,
138+
content: [%{text: "[]", color: "text-code-2"}],
139+
expanded_before: nil,
140+
expanded_after: nil
141+
}
142+
143+
assert TermParser.term_to_display_tree(term) == expected
144+
end
145+
146+
test "parses regex term" do
147+
term = ~r/hello/
148+
149+
expected = %{
150+
kind: "regex",
151+
children: nil,
152+
content: [%{text: "~r/hello/", color: "text-code-2"}],
153+
expanded_before: nil,
154+
expanded_after: nil
155+
}
156+
157+
assert TermParser.term_to_display_tree(term) == expected
158+
end
159+
160+
test "parses constant terms" do
161+
terms = [nil, true, false]
162+
163+
Enum.map(terms, fn term ->
164+
expected = %{
165+
kind: "atom",
166+
children: nil,
167+
content: [%{text: inspect(term), color: "text-code-3"}],
168+
expanded_before: nil,
169+
expanded_after: nil
170+
}
171+
172+
assert TermParser.term_to_display_tree(term) == expected
173+
end)
174+
end
175+
176+
test "parses empty map term" do
177+
term = %{}
178+
179+
expected = %{
180+
kind: "map",
181+
children: nil,
182+
content: [%{text: "%{}", color: "text-code-2"}],
183+
expanded_before: nil,
184+
expanded_after: nil
185+
}
186+
187+
assert TermParser.term_to_display_tree(term) == expected
188+
end
189+
190+
test "parses struct without `Inspect.Any` implementation" do
191+
term = %TestStruct{field1: "value1", field2: 42}
192+
193+
"Elixir." <> struct_name = __MODULE__.TestStruct |> Atom.to_string()
194+
195+
expected = %{
196+
kind: "struct",
197+
children: [
198+
%{
199+
kind: "binary",
200+
children: nil,
201+
content: [
202+
%{text: "field1:", color: "text-code-1"},
203+
%{text: " ", color: "text-code-2"},
204+
%{text: "\"value1\"", color: "text-code-4"},
205+
%{text: ",", color: "text-code-2"}
206+
],
207+
expanded_before: nil,
208+
expanded_after: nil
209+
},
210+
%{
211+
kind: "number",
212+
children: nil,
213+
content: [
214+
%{text: "field2:", color: "text-code-1"},
215+
%{text: " ", color: "text-code-2"},
216+
%{text: "42", color: "text-code-1"}
217+
],
218+
expanded_before: nil,
219+
expanded_after: nil
220+
}
221+
],
222+
content: [
223+
%{color: "text-code-2", text: "%"},
224+
%{text: struct_name, color: "text-code-1"},
225+
%{text: "{...}", color: "text-code-2"}
226+
],
227+
expanded_before: [
228+
%{text: "%", color: "text-code-2"},
229+
%{text: struct_name, color: "text-code-1"},
230+
%{text: "{", color: "text-code-2"}
231+
],
232+
expanded_after: [%{text: "}", color: "text-code-2"}]
233+
}
234+
235+
assert TermParser.term_to_display_tree(term) == expected
236+
end
237+
238+
test "parses structs" do
239+
{:ok, term} = Date.new(2023, 5, 10)
240+
241+
expected = %{
242+
children: [
243+
%{
244+
kind: "atom",
245+
children: nil,
246+
content: [
247+
%{text: "calendar:", color: "text-code-1"},
248+
%{text: " ", color: "text-code-2"},
249+
%{text: "Calendar.ISO", color: "text-code-1"},
250+
%{text: ",", color: "text-code-2"}
251+
],
252+
expanded_before: nil,
253+
expanded_after: nil
254+
},
255+
%{
256+
kind: "number",
257+
children: nil,
258+
content: [
259+
%{text: "month:", color: "text-code-1"},
260+
%{text: " ", color: "text-code-2"},
261+
%{text: "5", color: "text-code-1"},
262+
%{text: ",", color: "text-code-2"}
263+
],
264+
expanded_before: nil,
265+
expanded_after: nil
266+
},
267+
%{
268+
kind: "number",
269+
children: nil,
270+
content: [
271+
%{text: "day:", color: "text-code-1"},
272+
%{text: " ", color: "text-code-2"},
273+
%{text: "10", color: "text-code-1"},
274+
%{text: ",", color: "text-code-2"}
275+
],
276+
expanded_before: nil,
277+
expanded_after: nil
278+
},
279+
%{
280+
kind: "number",
281+
children: nil,
282+
content: [
283+
%{text: "year:", color: "text-code-1"},
284+
%{text: " ", color: "text-code-2"},
285+
%{text: "2023", color: "text-code-1"}
286+
],
287+
expanded_before: nil,
288+
expanded_after: nil
289+
}
290+
],
291+
content: [%{color: "text-code-2", text: "~D[2023-05-10]"}],
292+
expanded_before: [
293+
%{text: "%", color: "text-code-2"},
294+
%{text: "Date", color: "text-code-1"},
295+
%{text: "{", color: "text-code-2"}
296+
],
297+
expanded_after: [%{text: "}", color: "text-code-2"}],
298+
kind: "struct"
299+
}
300+
301+
assert TermParser.term_to_display_tree(term) == expected
302+
end
303+
304+
test "parses map terms" do
305+
term = %{
306+
"key1" => "value1",
307+
"key2" => 42,
308+
"key3" => :test
309+
}
310+
311+
expected = %{
312+
kind: "map",
313+
children: [
314+
%{
315+
kind: "binary",
316+
children: nil,
317+
content: [
318+
%{text: "\"key1\"", color: "text-code-4"},
319+
%{text: " => ", color: "text-code-2"},
320+
%{text: "\"value1\"", color: "text-code-4"},
321+
%{text: ",", color: "text-code-2"}
322+
],
323+
expanded_before: nil,
324+
expanded_after: nil
325+
},
326+
%{
327+
kind: "number",
328+
children: nil,
329+
content: [
330+
%{text: "\"key2\"", color: "text-code-4"},
331+
%{text: " => ", color: "text-code-2"},
332+
%{text: "42", color: "text-code-1"},
333+
%{text: ",", color: "text-code-2"}
334+
],
335+
expanded_before: nil,
336+
expanded_after: nil
337+
},
338+
%{
339+
kind: "atom",
340+
children: nil,
341+
content: [
342+
%{text: "\"key3\"", color: "text-code-4"},
343+
%{text: " => ", color: "text-code-2"},
344+
%{text: ":test", color: "text-code-1"}
345+
],
346+
expanded_before: nil,
347+
expanded_after: nil
348+
}
349+
],
350+
content: [%{text: "%{...}", color: "text-code-2"}],
351+
expanded_before: [%{text: "%{", color: "text-code-2"}],
352+
expanded_after: [%{text: "}", color: "text-code-2"}]
353+
}
354+
355+
assert TermParser.term_to_display_tree(term) == expected
356+
end
357+
end
358+
end

0 commit comments

Comments
 (0)