2

I have encountered a strange issue today. I have a verbatim string like this:

var s = @"     0     1
0     0"

i.e. there is a new line after the one. My Environment.NewLine is set to \r\n

This is part of a unit test and the tests have been working fine for the past several months. Now when I run my tests the above string declaration is resulting in:

"     0     1\n     0     0"

instead of

"     0     1\r\n     0     0"

Meaning the tests fail.

I have dumped out every character to prove this is true. I have also tried not using a verbatim string instead like this:

var s = "     0     1\r\n    0     0"

and the tests then pass.

Does anyone have any idea what could be happening here?

Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
  • 2
    Maybe your source file literally has only `\n` - have you tried deleting the break and inserting it again, or else taken a look at the file with something that can show the actual bytes there? – 500 - Internal Server Error Oct 21 '19 at 09:26
  • 1
    Thats it! I tried copying and pasting the code into notepad++ but obviously when I did that it corrected the line endings. If I open the file directly the line endings for some reason have been changed to just LF# – Tim Rutter Oct 21 '19 at 09:31
  • 4
    Depending on a specific line ending is dangerous. Source control software (e.g. Git) might convert line endings on push or pull depending on the user's settings. – cremor Oct 21 '19 at 09:31
  • @cremor yes I've just learnt an important lesson there. – Tim Rutter Oct 21 '19 at 09:31
  • i think it must have been git that did it. :) – Tim Rutter Oct 21 '19 at 09:38

4 Answers4

2

The answer is this: Git (most probably) swapped the line endings in the file to just LF. Depending on file line endings in unit tests is not a good idea so I have changed the code to specify the newlines explicitly in all cases, thus:

var s = "     0     1\r\n    0     0"
Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
1

If you want to use the string in a unit test it is best to ensure the content is always the same. I suggest to declare it like this:

var s = $"     0     1{Enviroment.NewLine}0     0";
Stefan
  • 652
  • 5
  • 19
  • Can Environment.NewLine not change? I appreciate its unlikely. Better to actually use \r\n explicitly – Tim Rutter Oct 21 '19 at 09:44
  • The `Enviroment.NewLine` can be different depending on the OS the software is running. It depends on how the unit test is implemented if it will break or not. If you use `Enviroment.NewLine` on both places it will not break the unit test. – Stefan Oct 21 '19 at 09:58
  • its a good point, I should be writing the data out using environment.newline. – Tim Rutter Oct 21 '19 at 10:04
1

You can use string interpolation together with verbatim to add control characters.

var s = @$"     0     1{'\r'}
0     0"
Bartosz Wójtowicz
  • 1,321
  • 10
  • 18
0

Please check this answer - basically it is caused by code editor end of line settings. We faced the same issue in unit tests.

Pavel Cermak
  • 1,215
  • 10
  • 13