Skip to content

Commit 126c2cb

Browse files
Chris Xuclaude
andcommitted
fix(tests): improve semantic correctness of test assertions
- Fix same_keys? test to exercise correct code path - Split "list containing non-maps" into two cases: - List starting with non-map (exercises fallback clause) - List with map followed by non-maps (exercises Enum.all? logic) - Add nested path key_order test for Objects.encode - Verifies path-specific key ordering for nested objects - Exercises the Map.fetch(key_order, path) logic Addresses semantic quality issues found during code review. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c30cee1 commit 126c2cb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

test/toon/encode/objects_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,27 @@ defmodule Toon.Encode.ObjectsTest do
102102

103103
assert result == "age: 30\nname: Alice"
104104
end
105+
106+
test "uses nested path key_order for nested objects" do
107+
opts = %{
108+
delimiter: ",",
109+
length_marker: nil,
110+
indent: 2,
111+
indent_string: " ",
112+
key_order: %{["user"] => ["first", "last"]},
113+
key_folding: "off"
114+
}
115+
116+
data = %{"user" => %{"last" => "Smith", "first" => "John"}}
117+
result = Objects.encode(data, 0, opts) |> IO.iodata_to_binary()
118+
119+
# Nested "user" object should use specified order: first, last
120+
assert result =~ "first: John"
121+
assert result =~ "last: Smith"
122+
# Verify "first" appears before "last" in the output
123+
first_pos = :binary.match(result, "first")
124+
last_pos = :binary.match(result, "last")
125+
assert elem(first_pos, 0) < elem(last_pos, 0)
126+
end
105127
end
106128
end

test/toon/utils_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,15 @@ defmodule Toon.UtilsTest do
4444
assert Utils.same_keys?(42) == false
4545
end
4646

47-
test "returns false for list containing non-maps" do
47+
test "returns false for list starting with non-map" do
48+
# Exercises the fallback clause: def same_keys?(_), do: false
4849
assert Utils.same_keys?([1, 2, 3]) == false
4950
end
51+
52+
test "returns false for list with map followed by non-maps" do
53+
# Exercises the Enum.all? logic with is_map check inside
54+
assert Utils.same_keys?([%{"a" => 1}, 2, 3]) == false
55+
end
5056
end
5157

5258
describe "all_primitives?/1" do

0 commit comments

Comments
 (0)