I’ve been following along with the book and typing in the example application of Chapter 13.
My problem is in running the “mix text” from page 162. It always fails because the ‘atom-elixir-formatter’ v1.0.3 for the Atom editor does too good of a job and removes the trailing spaces of the text block used by the ‘assert’ of the “Output is correct” test.
Is there any way to tell the ‘atom-elixir-formatter’, from inside the file’s source code, to temporarily not remove trailing spaces? Or perhaps tell the formatter to skip a specific set of lines?
I know I could always temporarily disable the Atom plugin but, knowing me, I’ll forget to re-enable it when I’m done confirming that all tests pass.
For the time being, I just visually compared the left/right differences that the test discovered and ensured that it was only the trailing spaces that weren’t matching the left and right sides of the assert.
I do not know that plugin as I do not use atom, but if it is using the elixir formatter, then you have no chance but to disable it.
But if though by “text block” you actually mean a string, then there shouldn’t get anything removed inside, as it would change the semantic of the code.
I think this is the code you are referring to, which means you think the formatter is removing trailing spaces from within a heredoc (multi-line string).
I think that the position of the final quotes affects leading spaces, so make sure they are indented correctly.
Can you post your exact code?
test "Output is correct" do
result = capture_io fn ->
TF.print_table_for_columns(simple_test_data, headers)
end
assert result == """
c1 | c2 | c4
------+--------+--------
r1 c1 | r1 c2 | r1+++c4
r2 c1 | r2 c2 | r2 c4
r3 c1 | r3 c2 | r3 c4
r4 c1 | r4++c2| r4 c4
"""
end
EDIT: I was not able to get this test to pass, due to ElixirLS extension in VSCode running the formatter and stripping trailing whitespace from the end of "hello " and "world " in the heredoc:
test "test something" do
string = "hello \nworld \n"
heredoc = """
hello
world
"""
assert string == heredoc
end
EDIT 2: But this works:
test "test something" do
string = "hello \nworld \n"
heredoc = """
hello\s
world\s
"""
assert string == heredoc
end
That’s not elixirls or the formatter, that’s probably VScode itself.
As I already said, the formatter will not change semantics of code.
I’m also pretty sure that in the atom case, it’s atom stripping trailing whitespace, as most editors simply strip trailing whitespace without even knowing about semantics.
It doesn’t look like Atom is the culprit - Atom has no preference setting regarding the stripping, but having said that, I just created a test text file (.txt to ensure that the formatter plugin wouldn’t touch it) and it saved with my trailing spaces.
I think I’m going to report the problem to the plugin author - maybe its an oversight!?
IMHO: The formatter should not be touching text inside of a heredoc.
Interesting. I made sure to change VSCode settings to not strip trailing whitespace before testing. With the ElixirLS extension disabled whitespace would not be stripped. As soon as I enabled it, whitespace would be stripped. I assume that the extension is just using the Elixir formatter. I will test again using the formatter directly instead of the extension.
EDIT:@NobbZ is correct, the elixir formatter is not doing the whitespace stripping in my case. Although I had this setting in VSCode "files.trimTrailingWhitespace": false, the ElixirLS extension injected this into my settings:
// Configure editor settings to be overridden for [elixir] language.
"[elixir]": {
"editor.insertSpaces": true,
"editor.tabSize": 2,
"editor.wordBasedSuggestions": false,
"editor.formatOnType": true,
"editor.acceptSuggestionOnEnter": "off",
"editor.trimAutoWhitespace": false,
"files.trimTrailingWhitespace": true,
"files.insertFinalNewline": true
}
Maybe the Elixir extension in atom is doing something similar?
No problem I learnt that ElixirLS is overwriting my settings for trailing whitespace and have opened a Github issue for this. Thanks to @NobbZ I also learnt about xxd which I think could be quite useful.