Skip to content

Commit

Permalink
Fix #436 Flaky test: UnitsNet.Tests.UnitSystemTests.PositiveInfinityF…
Browse files Browse the repository at this point in the history
…ormatting (#438)

* Fix #436 Flaky test: UnitsNet.Tests.UnitSystemTests.PositiveInfinityFormatting

Fixes #436

Added `UnitSystemFixture` class which is a `CollectionDefinition` class used to group test classes:
1. that rely on manipulating CultureInfo. See #436
2. to avoid accessing static prop DefaultToString in parallel from multiple tests:
	a. UnitSystemTests.DefaultToStringFormatting()
	b. LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()

Changes in this PR:
- `UnitSystemTests.GetDefaultAbbreviationFallsBackToUsEnglishCulture`: Set CultureInfo properties back to their original values.
- Specify `InvariantCulture` on the following `UnitSystemTests` tests:
	1. `NegativeInfinityFormatting`
	2. `PositiveInfinityFormatting`
- Applied `[Collection(nameof(UnitSystemFixture)]` attribute to test classes that:
	- Sets `UnitSystem.DefaultCulture`
	- Originally uses `[Collection("DefaulToString")]` that also depends on `CultureInfo`
		- Only one `Collection` attribute per test class.
- Updated to `xunit` and `xunit.runner.visualstudio` to v2.3.1 (was v2.3.0-beta4-build3742)
	- To have the [CollectionDefinition(DisableParallelization = true)] feature. See https://xunit.github.io/releases/2.3-beta5

* Removed `InvariantCulture`

Removed `InvariantCulture` on the following `UnitSystemTests` tests:
	1. `NegativeInfinityFormatting`
	2. `PositiveInfinityFormatting`

* Specified InvariantCulture in InfinityFormatting Tests
  • Loading branch information
angularsen authored May 8, 2018
2 parents f54e381 + 538f972 commit 3632fdb
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<!--Get the latest released version of UnitsNet.Serialization.JsonNet in Nuget-->
<PackageReference Include="UnitsNet.Serialization.JsonNet" Version="*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions UnitsNet.Tests/CustomCode/LengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace UnitsNet.Tests.CustomCode
// Avoid accessing static prop DefaultToString in parallel from multiple tests:
// UnitSystemTests.DefaultToStringFormatting()
// LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
[Collection("DefaultToString")]
[Collection(nameof(UnitSystemFixture))]
public class LengthTests : LengthTestsBase
{
protected override double CentimetersInOneMeter => 100;
Expand Down Expand Up @@ -156,4 +156,4 @@ public void MinValueIsCorrectForUnitWithBaseTypeDouble()


}
}
}
1 change: 1 addition & 0 deletions UnitsNet.Tests/QuantityTests.ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace UnitsNet.Tests
{
[Collection(nameof(UnitSystemFixture))]
public partial class QuantityTests
{
public class ToString
Expand Down
18 changes: 18 additions & 0 deletions UnitsNet.Tests/UnitSystemFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Xunit;

namespace UnitsNet.Tests
{
[CollectionDefinition(nameof(UnitSystemFixture), DisableParallelization = true)]
public class UnitSystemFixture : ICollectionFixture<object>
{
// This class has no code, and is never created. Its purpose is simply
// to be the place to apply [CollectionDefinition] and all the
// ICollectionFixture<> interfaces.

// Apply this collection fixture to classes:
// 1. that rely on manipulating CultureInfo. See https://github.com/angularsen/UnitsNet/issues/436
// 2. to avoid accessing static prop DefaultToString in parallel from multiple tests:
// a. UnitSystemTests.DefaultToStringFormatting()
// b. LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
}
}
44 changes: 26 additions & 18 deletions UnitsNet.Tests/UnitSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@

namespace UnitsNet.Tests
{
// Avoid accessing static prop DefaultToString in parallel from multiple tests:
// UnitSystemTests.DefaultToStringFormatting()
// LengthTests.ToStringReturnsCorrectNumberAndUnitWithCentimeterAsDefualtUnit()
[Collection("DefaultToString")]
[Collection(nameof(UnitSystemFixture))]
public class UnitSystemTests
{
private readonly ITestOutputHelper _output;
Expand Down Expand Up @@ -412,21 +409,32 @@ public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified()
[Fact]
public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
{
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
// CurrentUICulture affects localization, in this case the abbreviation.
// Zulu (South Africa)
var zuluCulture = new CultureInfo("zu-ZA");
UnitSystem zuluUnits = UnitSystem.GetCached(zuluCulture);
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;
CultureInfo oldCurrentCulture = CultureInfo.CurrentCulture;
CultureInfo oldCurrentUICulture = CultureInfo.CurrentUICulture;

UnitSystem usUnits = UnitSystem.GetCached(AmericanCultureName);
usUnits.MapUnitToAbbreviation(CustomUnit.Unit1, "US english abbreviation for Unit1");
try
{
// CurrentCulture affects number formatting, such as comma or dot as decimal separator.
// CurrentUICulture affects localization, in this case the abbreviation.
// Zulu (South Africa)
var zuluCulture = new CultureInfo("zu-ZA");
UnitSystem zuluUnits = UnitSystem.GetCached(zuluCulture);
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;

UnitSystem usUnits = UnitSystem.GetCached(AmericanCultureName);
usUnits.MapUnitToAbbreviation(CustomUnit.Unit1, "US english abbreviation for Unit1");

// Act
string abbreviation = zuluUnits.GetDefaultAbbreviation(CustomUnit.Unit1);
// Act
string abbreviation = zuluUnits.GetDefaultAbbreviation(CustomUnit.Unit1);

// Assert
Assert.Equal("US english abbreviation for Unit1", abbreviation);
// Assert
Assert.Equal("US english abbreviation for Unit1", abbreviation);
}
finally
{
CultureInfo.CurrentCulture = oldCurrentCulture;
CultureInfo.CurrentUICulture = oldCurrentUICulture;
}
}

[Fact]
Expand All @@ -441,7 +449,7 @@ public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviati
[Fact]
public void NegativeInfinityFormatting()
{
Assert.Equal("- m", Length.FromMeters(double.NegativeInfinity).ToString());
Assert.Equal("-Infinity m", Length.FromMeters(double.NegativeInfinity).ToString(LengthUnit.Meter, CultureInfo.InvariantCulture));
}

[Fact]
Expand Down Expand Up @@ -473,7 +481,7 @@ public void Parse_UnambiguousUnitsDoesNotThrow()
[Fact]
public void PositiveInfinityFormatting()
{
Assert.Equal(" m", Length.FromMeters(double.PositiveInfinity).ToString());
Assert.Equal("Infinity m", Length.FromMeters(double.PositiveInfinity).ToString(LengthUnit.Meter, CultureInfo.InvariantCulture));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions UnitsNet.Tests/UnitsNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
<PackageReference Include="xunit" Version="2.3.0-beta4-build3742" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta4-build3742" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta2-build3683" />
</ItemGroup>

Expand Down

0 comments on commit 3632fdb

Please sign in to comment.