Search code examples
c#.netxunitxunit.netsurrogate-pairs

xUnit.net: Why do these 2 equivalent tests have different results?


For some reason, this test utilizing InlineData fails in xUnit:

[Theory]
[InlineData("\uD800", 1)]
public static void HasLength(string s, int length)
{
    Assert.Equal(length, s.Length);
}

while this, which uses MemberData, passes:

public static IEnumerable<object[]> HasLength_TestData()
{
    yield return new object[] { "\uD800", 1 };
}

[Theory]
[MemberData(nameof(HasLength_TestData))]
public static void HasLength(string s, int length)
{
    Assert.Equal(length, s.Length);
}

What is the reason for this? Have I discovered a bug in xUnit.net? (I think it may have something to do with the fact that \uD800 is a surrogate character, and it's somehow getting translated to 2 characters when passing thru InlineData. Not sure why, though.)


Solution

  • Nicolay's answer doesn't answer the question, but does link to it:

    Attribute values are stored using UTF-8, and an isolated high surrogate cannot successfully be converted from UTF-16.

    Here's a (VB.NET) LinqPad query confirming the situation.