2

I am new to unit test case writing using mstest framework and I am stuck in retrieving new line character from the xml file as an input to expected value. Below is the piece of test method

public void ExtractNewLineTest()

{
    MathLibraray target = new MathLibraray(); // TODO: Initialize to an appropriate value
    string expected = TestContext.DataRow["ExpectedValue"].ToString(); => I am retrieving the value from the xml file.
    string actual;
    actual = target.ExtractNewLine();
    Assert.AreEqual(expected, actual);
}

Below is the xml content

<ExtractNewLineTest>
      <ExpectedValue><![CDATA[select distinct\tCDBREGNO,\r\n\tMOLWEIGHT,\r\n\tMDLNUMBER\r\nfrom Mol]]></ExpectedValue>
 </ExtractNewLineTest>

When I retrieve the data from the xml to expected value, I am getting below string

ExpectedValue = “select distinct\\tCDBREGNO,\\r\\n\\tMOLWEIGHT,\\r\\n\\tMDLNUMBER\\r\\nfrom Mol”;

If we see the above value, extra slash is getting added for both \n and \r. Please let me know, how I can assert this value. Thanks!

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
vasujc
  • 21
  • 2

1 Answers1

2

Your XML file doesn't contain a newline. It contains a backslash followed by a t, r or n. That combination is faithfully being read from the XML file, and then the debugger is escaping the backslash when it's displaying it to you.

If you want to apply "C# string escaping" rules to the value you read from the XML, you can do so, but you should be aware that you do need to do so. A simplistic approach would be:

expectedValue = expectedValue.Replace("\\t", "\t")
                             .Replace("\\n", "\n")
                             .Replace("\\r", "\r")
                             .Replace("\\\\", "\\");

There are cases where that will do the wrong thing - for example if you've got a genuine backslash followed by a t. It may be good enough for you though. If not, you'll have to write a "proper" replacement which reads the string from the start and handles the various cases.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194