end-of-line characters causing test fail

Topics: General
Apr 4, 2012 at 8:09 PM

Today I have downloaded ASP.NET Web Stack source code, I started wandering on code of aplication

Then I run some tests and find out some of them was failing because of change in end-of-line characters, for example:

 

Test named: CollectionTemplateWithSingleItemCollectionWithPrefix
Located in System.Web.Mvc.Html.Test.DefaultDisplayTemplatesTest

It have been written in like this:

 

[Fact]
public void CollectionTemplateWithSingleItemCollectionWithPrefix()
{
    // Arrange
    HtmlHelper html = MakeHtmlHelper<List<string>>(new List<string> { "foo" });
    html.ViewContext.ViewData.TemplateInfo.HtmlFieldPrefix = "ModelProperty";

    // Act
    string result = DefaultDisplayTemplates.CollectionTemplate(html, CollectionSpyCallback);

    // Assert
    Assert.Equal(@"
Model = foo, ModelType = System.String, PropertyName = (null), HtmlFieldName = ModelProperty[0], TemplateName = (null), Mode = ReadOnly, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                 result);
}

 

@ sign is used for to generate \r\n characters, but it don't it. It gives \n which, causing problem. Probably Git is removing \r character, because UNIX style of ending line. 

Fix for bad written test is replacing Assert.Equal with this line:

 

Assert.Equal("\r\nModel = foo, ModelType = System.String, PropertyName = (null), HtmlFieldName = ModelProperty[0], TemplateName = (null), Mode = ReadOnly, TemplateInfo.HtmlFieldPrefix = , AdditionalViewData = (null)",
                         result);

 

It's proper resolution for this problem?

Should I pull request? Small changes like this are welcomed here?

PS: Same problem have couple tests in same test class.

Apr 4, 2012 at 9:25 PM

Hi,

Thanks for the feedback. The tests do work for me so it would be good to try investigate why they are failing for you before starting work on any changes. It might be a matter of the file line endings, though we do have a gitattributes file checked in that should instruct git about the right way to treat line endings. You might also want to double check that the git tools you are using are configured to use auto crlf.

Apr 5, 2012 at 8:36 PM

Hello,

Setting core.autocrlf to true, helped. Thank you.

I encountered another problem, some tests are failing, because of decimal mark. 

Test like this:

Located in System.Web.Mvc.Html.Test.DefaultDisplayTemplatesTest

 

        [Fact]
        public void DecimalTemplateTests()
        {
            Assert.Equal(
                @"12.35",
                DefaultDisplayTemplates.DecimalTemplate(MakeHtmlHelper<decimal>(12.3456M)));

            Assert.Equal(
                @"Formatted Value",
                DefaultDisplayTemplates.DecimalTemplate(MakeHtmlHelper<decimal>(12.3456M, "Formatted Value")));

            Assert.Equal(
                @"&lt;script&gt;alert(&#39;XSS!&#39;)&lt;/script&gt;",
                DefaultDisplayTemplates.DecimalTemplate(MakeHtmlHelper<decimal>(12.3456M, "<script>alert('XSS!')</script>")));
        }

 

Result of that test is:

Expected: 12.35
Actual: 12,35

In my country we used comma as separator.

Solution like this is proper solution for this problem?

 

 Assert.Equal(
                12.35M.ToString(),
                DefaultDisplayTemplates.DecimalTemplate(MakeHtmlHelper<decimal>(12.3456M)));