Skip to content

Commit a66b0ff

Browse files
authored
Task: Add tests for LiveDebugger.Utils.Parsers (#336)
* add spec * add tests
1 parent a075d56 commit a66b0ff

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

lib/live_debugger/utils/parsers.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule LiveDebugger.Utils.Parsers do
1818
end
1919
end
2020

21+
@spec parse_elapsed_time(non_neg_integer()) :: String.t()
2122
def parse_elapsed_time(microseconds) do
2223
cond do
2324
microseconds < 1_000 -> "#{microseconds} µs"

test/utils/parsers_test.exs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
defmodule LiveDebugger.Utils.ParsersTest do
2+
use ExUnit.Case, async: true
3+
4+
alias LiveDebugger.Utils.Parsers
5+
6+
describe "parse_timestamp/1" do
7+
test "parses a valid timestamp" do
8+
timestamp = 1_000_000
9+
assert Parsers.parse_timestamp(timestamp) == "00:00:01.000000"
10+
end
11+
12+
test "returns \"Invalid timestamp\" when timestamp is invalid" do
13+
timestamp = 109_201_930_129_391_238_012_983_091_283_712_097_380_127
14+
assert Parsers.parse_timestamp(timestamp) == "Invalid timestamp"
15+
end
16+
end
17+
18+
describe "parse_elapsed_time/1" do
19+
test "parses microseconds for less than 1ms" do
20+
assert Parsers.parse_elapsed_time(500) == "500 µs"
21+
end
22+
23+
test "parses milliseconds" do
24+
assert Parsers.parse_elapsed_time(1_500) == "1 ms"
25+
end
26+
27+
test "parses seconds" do
28+
assert Parsers.parse_elapsed_time(1_500_000) == "1.50 s"
29+
end
30+
end
31+
32+
test "pid_to_string/1 converts pid to string" do
33+
pid = :c.pid(0, 123, 0)
34+
assert Parsers.pid_to_string(pid) == "0.123.0"
35+
end
36+
37+
describe "string_to_pid/1" do
38+
test "converts string to pid" do
39+
pid = :c.pid(0, 123, 0)
40+
assert {:ok, ^pid} = Parsers.string_to_pid("0.123.0")
41+
end
42+
43+
test "returns :error for invalid string" do
44+
assert Parsers.string_to_pid("invalid") == :error
45+
end
46+
end
47+
48+
test "cid_to_string/1 converts cid to string" do
49+
cid = %Phoenix.LiveComponent.CID{cid: 123}
50+
assert Parsers.cid_to_string(cid) == "123"
51+
end
52+
53+
describe "string_to_cid/1" do
54+
test "converts string to cid" do
55+
cid = %Phoenix.LiveComponent.CID{cid: 123}
56+
assert {:ok, ^cid} = Parsers.string_to_cid("123")
57+
end
58+
59+
test "returns :error for invalid string" do
60+
assert Parsers.string_to_cid("invalid") == :error
61+
end
62+
end
63+
64+
test "module_to_string/1 converts module to string" do
65+
assert Parsers.module_to_string(LiveDebuggerTest.TestView) == "LiveDebuggerTest.TestView"
66+
end
67+
end

0 commit comments

Comments
 (0)