chore: section 12 closed, had to do with map and atoms

This commit is contained in:
2026-05-01 17:37:08 +02:00
parent eb358bf512
commit c12001307f
18 changed files with 1025 additions and 386 deletions

View File

@@ -10,6 +10,12 @@ defmodule BDS.MapUtilsTest do
assert MapUtils.attr(%{"title" => "fallback", title: nil}, :title) == nil
assert MapUtils.attr(%{}, :title) == nil
end
test "reads with a default while preserving explicit nil and false" do
assert MapUtils.attr(%{}, :published, true) == true
assert MapUtils.attr(%{"published" => false}, :published, true) == false
assert MapUtils.attr(%{"published" => nil}, :published, true) == nil
end
end
describe "maybe_put/3" do
@@ -28,4 +34,47 @@ defmodule BDS.MapUtilsTest do
assert MapUtils.blank_to_nil(42) == 42
end
end
describe "atom/string key duality" do
test "shared attr helper is used for same-name atom and string reads" do
root = File.cwd!()
offenders =
[Path.join(root, "lib/**/*.ex"), Path.join(root, "lib/**/*.heex")]
|> Enum.flat_map(&Path.wildcard/1)
|> Enum.flat_map(fn path ->
path
|> File.stream!()
|> Stream.with_index(1)
|> Enum.flat_map(fn {line, line_number} ->
if same_name_dual_key_read?(line) do
["#{Path.relative_to(path, root)}:#{line_number}:#{String.trim(line)}"]
else
[]
end
end)
end)
assert offenders == []
end
end
defp same_name_dual_key_read?(line) do
Regex.match?(
~r/Map\.get\((\w+),\s*:([a-zA-Z_][a-zA-Z0-9_?!]*)\).{0,120}Map\.get\(\1,\s*"\2"\)/,
line
) or
Regex.match?(
~r/Map\.get\((\w+),\s*:([a-zA-Z_][a-zA-Z0-9_?!]*),\s*Map\.get\(\1,\s*"\2"/,
line
) or
Regex.match?(
~r/Map\.get\((\w+),\s*"([a-zA-Z_][a-zA-Z0-9_?!]*)"\).{0,120}Map\.get\(\1,\s*:\2\)/,
line
) or
Regex.match?(
~r/Map\.get\((\w+),\s*"([a-zA-Z_][a-zA-Z0-9_?!]*)",\s*Map\.get\(\1,\s*:\2/,
line
)
end
end