I wan’t to write a test about Clouseau, a tool I am writing to help me debug. Here is what I have so far
test "displays correct info" do
result =
capture_io(fn ->
Cl.inspect(@shopping_list, label: "The shopping list")
end)
|> String.split("\n", trim: true)
# WARNING: The next line should be always be 5 lines below the Cl.inspect otherwise the test will fail
line = __ENV__.line - 5
# ...a bunch of code and asserts
end
Cl.inspect
inspects the pased term and prepends some meta information. Among others the line number where it was called.
I want to test that it shows the correct number. For example, in the code above, the call is in the number 17 in my file. So it should output the number 17. But I don’t have any means to see that in my file and compare actual with output.
The best I came up with is putting that line line = __ENV__.line - 5
exactly 5 lines below the call. This way I know that the line Cl.inspect
was called is exactly 5 lines above
.
The problem is that this kind of code is the kind of code I would show my nephew to make him eat his vegetables. Many things could change in the file, causing the test to fail.
Is there any better way to get the line number of Cl.inspect
inside the same macro?