I’m TDD’ing parsing code, and I’d been writing tests like this:
test "First - 001", %{sections_001: sections} do
assert List.first(sections).number == "1.001"
end
But then I realized I could also write them list this:
test "First - 001", %{sections_001: sections} do
assert [%{number: "1.001"} | _] = sections
end
I find the first section more readable. But when the list is empty (as it is when TDD’ing this) it gives a long, indirect nil error.
But the second style, which I find less readable, gives a much more readable error when the list is empty:
1) test Section.number First - 001 (ChapterFileComplexTest)
test/chapter_file_complex_test.exs:103
match (=) failed
code: assert [%{number: "1.001"} | _] = sections
left: [%{number: "1.001"} | _]
right: []
stacktrace:
test/chapter_file_complex_test.exs:104: (test)
Is there another way to do this that I’m not considering?