Skip to content

Commit

Permalink
Merge pull request dotnet#21016 from mellinoe/colortranslator-tests-c…
Browse files Browse the repository at this point in the history
…ulture-fix

Force invariant culture for some ColorTranslator tests
  • Loading branch information
mellinoe authored Jun 14, 2017
2 parents 8cc5c4b + e89d127 commit bba2003
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/Common/tests/System/ThreadCultureChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ namespace System.Common.Tests
{
public sealed class ThreadCultureChange : IDisposable
{
private readonly CultureInfo _originalCultureInfo;
private readonly CultureInfo _originalUICultureInfo;
private readonly CultureInfo _originalCultureInfo = CultureInfo.CurrentCulture;
private readonly CultureInfo _originalUICultureInfo = CultureInfo.CurrentUICulture;

public ThreadCultureChange()
public ThreadCultureChange() { }

public ThreadCultureChange(string culture) : this()
{
_originalCultureInfo = CultureInfo.CurrentCulture;
_originalUICultureInfo = CultureInfo.CurrentUICulture;
ChangeCultureInfo(culture);
}

public void ChangeCultureInfo(string culture)
public ThreadCultureChange(CultureInfo newCulture) : this()
{
ChangeCultureInfo(newCulture);
}

public void ChangeCultureInfo(string culture) => ChangeCultureInfo(new CultureInfo(culture));

public void ChangeCultureInfo(CultureInfo newCulture)
{
var newCulture = new CultureInfo(culture);
CultureInfo.CurrentCulture = newCulture;
CultureInfo.CurrentUICulture = newCulture;
}
Expand Down
13 changes: 11 additions & 2 deletions src/System.Drawing.Common/tests/ColorTranslatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Common.Tests;
using System.Globalization;
using System.Reflection;
using System.Threading;
using Xunit;

namespace System.Drawing.Tests
Expand Down Expand Up @@ -188,7 +191,10 @@ public static IEnumerable<object[]> FromHtml_TestData()
[MemberData(nameof(FromHtml_TestData))]
public void FromHtml_String_ReturnsExpected(string htmlColor, Color expected)
{
Assert.Equal(expected, ColorTranslator.FromHtml(htmlColor));
using (new ThreadCultureChange(CultureInfo.InvariantCulture))
{
Assert.Equal(expected, ColorTranslator.FromHtml(htmlColor));
}
}

[Theory]
Expand Down Expand Up @@ -216,7 +222,10 @@ public void FromHtml_String_ReturnsExpected(string htmlColor, Color expected)
[InlineData("1,2,256", typeof(ArgumentException))]
public void FromHtml_Invalid_Throws(string htmlColor, Type exception)
{
Assert.Throws(exception, () => ColorTranslator.FromHtml(htmlColor));
using (new ThreadCultureChange(CultureInfo.InvariantCulture))
{
Assert.Throws(exception, () => ColorTranslator.FromHtml(htmlColor));
}
}

public static IEnumerable<object[]> ToHtml_TestData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<Compile Include="$(CommonTestPath)\System\PlatformDetection.cs">
<Link>Common\System\PlatformDetection.cs</Link>
</Compile>
<Compile Include="$(CommonTestPath)\System\ThreadCultureChange.cs">
<Link>Common\System\ThreadCultureChange.cs</Link>
</Compile>
<Compile Include="ColorTranslatorTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit bba2003

Please sign in to comment.