ParsingTestCase ignores trailing whitespace

Answered

I followed these instructions (https://www.jetbrains.org/intellij/sdk/docs/tutorials/writing_tests_for_plugins/parsing_test.html) to create a test class for my plugin parser. Just to try out the test class, I started the IDE with my plugin, opened a file, opened the PSI structure viewer, built the PSI tree, copied the tree, then pasted it in a text file like in the instructions (obviously I also copied the source code for the test).

Naturally I was expecting the test to succeed right away, since I basically took the output of the parser and set that as the expected output, but it failed. After looking at the tree difference, it turns out that the tree I copied from the PSI viewer correctly includes a final `PsiWhiteSpace` element if the source file ends with a trailing whitespace (which the file I'm testing with does) whereas the tree generated by the parsing test class never includes an element for trailing whitespace no matter how long it is. I checked if there was some method I could override from `ParsingTestCase` to enable trailing whitespace or something but I didn't find anything.

Is this a bug? Is there a way around it?

0
3 comments

You probably can just remove your test results file and it's going to be created with proper results.

0

I'd not cal it a bug (ensuring that trailing whitespaces are processed correctly is the least relevant part of any parser's tests). But you may override `ParsingTestCase#loadFile` in your tests to return untrimmed text.

Copying test data from the PSI Viewer is too much ceremony. As Alexandr noted, if you execute tests without the result files, they will be created (see `UsefulTestCase#assertSameLinesWithFile`).

0
Avatar
Permanently deleted user

Oh I haven't noticed the call to trim in loadFileDefault. I still don't understand why this is done though. Regardless of how relevant it is to test parsing whitespaces, you're literally modifying the source code before feeding it to the parser when testing. Someone's tests were surely going to fail at some point because of this.

Anyways thanks for the help :)

0

Please sign in to leave a comment.