12

I have the following unit test that I'm porting from a .Net Framework library to .Net core xunint test library. The project the unit test needs to be added to is

https://github.com/dotliquid/dotliquid

and is being added to the selected file as show here

enter image description here

The unit test I'm trying to add is

[Test]
public void ParsingWithCommaDecimalSeparatorShouldWork()
{
    var ci = new CultureInfo(CultureInfo.CurrentCulture.Name)
             {
                 NumberFormat =
                 {
                     NumberDecimalSeparator = ","
                     , NumberGroupSeparator = "."
                 }
             };
    Thread.CurrentThread.CurrentCulture = ci;

    var t = Template.Parse("{{2.5}}");
    var result = t.Render( new Hash(), CultureInfo.InvariantCulture );
    Assert.AreEqual( result, "2.5"   );
}

However the test fails to compile in dotnet core.

Severity Code Description Project File Line Suppression State Error CS1061 'Thread' does not contain a definition for 'CurrentCulture' and no extension method 'CurrentCulture' accepting a first argument of type 'Thread' could be found (are you missing a using directive or an assembly reference?) DotLiquid.Tests(net451) C:\Users\phelan\workspace\dotliquid\src\DotLiquid.Tests\OutputTests.cs 113 N/A

I need to have different unit tests with different cultures. I would like to create an XUnit theory where each instance passes in a different culture for the unit test to verify against. How is this done in .NetCore?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217

2 Answers2

11

I looked at some of the dotnet source and I found this.

CultureInfo.DefaultThreadCurrentCulture = ci;

Basically it looks like you can set the default thread current culture from a static property of CultureInfo rather than from Thread.CurrentThread

poking around a bit more I found this

public CultureInfo CurrentCulture
{
    get
    {
        Contract.Ensures(Contract.Result<CultureInfo>() != null);
        return CultureInfo.CurrentCulture;
    }

    set
    {
        Contract.EndContractBlock();

        // If you add more pre-conditions to this method, check to see if you also need to 
        // add them to CultureInfo.DefaultThreadCurrentCulture.set.

        if (m_CurrentCulture == null && m_CurrentUICulture == null)
            nativeInitCultureAccessors();

        CultureInfo.CurrentCulture = value;
    }
}

This is in Thread.cs. So you can set the CultureInfo.CurrentCulture property explicitly.

example:

CultureInfo.CurrentCulture = new CultureInfo("en-GB"); ;

Assert.Equal("£1,000.00", String.Format("{0:C}", 1000));

CultureInfo.CurrentCulture = new CultureInfo("en-US"); ;

Assert.Equal("$1,000.00", String.Format("{0:C}", 1000));

enter image description here

csproj file for unit test project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.0</TargetFramework>

    <IsPackable>false</IsPackable>

    <ApplicationIcon />

    <OutputType>Library</OutputType>

    <StartupObject />
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170425-07" />
    <PackageReference Include="xunit" Version="2.2.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
  </ItemGroup>

</Project>
Aaron Roberts
  • 1,342
  • 10
  • 21
0

The solution is to set

CultureInfo.DefaultThreadCurrentCulture = ci;

and then spin up a new thread. This will set the current culture for the next thread. The final test case is.

[Test]
public void ParsingWithCommaDecimalSeparatorShouldWork()
{
    var ci = new CultureInfo(CultureInfo.CurrentCulture.Name)
             {
                 NumberFormat =
                 {
                     NumberDecimalSeparator = ","
                     , NumberGroupSeparator = "."
                 }
             };

    CultureInfo.DefaultThreadCurrentCulture = ci;
    var result = "";
    var thread = new Thread
        ( delegate()
        {
            Console.WriteLine(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
            Console.WriteLine(CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator);

            var t = Template.Parse("{{2.5}}");
            result = t.Render(new Hash(), CultureInfo.InvariantCulture);
        } );
    thread.Start();
    thread.Join();
    Assert.AreEqual(result, "2.5");

}

which is a bit messy but get the job done.

bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217