Skip to content

Commit

Permalink
Fix unit tests culture (#13016)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrJul authored Sep 25, 2023
1 parent 3471620 commit e6c1670
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@
using System.Collections.Generic;
using System.Globalization;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Data.Core;
using Avalonia.Markup.Parsers;
using Avalonia.UnitTests;
using Moq;
using Xunit;

namespace Avalonia.Base.UnitTests.Data.Core
{
public class BindingExpressionTests : IClassFixture<InvariantCultureFixture>
[InvariantCulture]
public class BindingExpressionTests
{
[Fact]
public async Task Should_Get_Simple_Property_Value()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using Avalonia.Data;
using Avalonia.Data.Core;
using Avalonia.Markup.Parsers;
using Avalonia.Threading;
using Avalonia.UnitTests;
using Xunit;

namespace Avalonia.Base.UnitTests.Data.Core
{
public class ExpressionObserverTests_DataValidation : IClassFixture<InvariantCultureFixture>
[InvariantCulture]
public class ExpressionObserverTests_DataValidation
{
////[Fact]
////public void Doesnt_Send_DataValidationError_When_DataValidatation_Not_Enabled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

namespace Avalonia.Base.UnitTests.Data.Core.Plugins
{
public class DataAnnotationsValidationPluginTests : IClassFixture<InvariantCultureFixture>
[InvariantCulture]
public class DataAnnotationsValidationPluginTests
{
[Fact]
public void Should_Match_Property_With_ValidatorAttribute()
Expand Down
42 changes: 42 additions & 0 deletions tests/Avalonia.UnitTests/InvariantCultureAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#nullable enable

using System;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;

namespace Avalonia.UnitTests;

/// <summary>
/// Runs tests in the invariant culture.
/// </summary>
/// <remarks>
/// Some tests check exception messages, and those from the .NET framework will be translated.
/// Some tests are formatting numbers, expecting a dot as a decimal point.
/// Use this fixture to set the current culture to the invariant culture.
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class InvariantCultureAttribute : BeforeAfterTestAttribute
{
private CultureInfo? _previousCulture;
private CultureInfo? _previousUICulture;

public override void Before(MethodInfo methodUnderTest)
{
base.Before(methodUnderTest);

_previousCulture = CultureInfo.CurrentCulture;
_previousUICulture = CultureInfo.CurrentUICulture;

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
}

public override void After(MethodInfo methodUnderTest)
{
CultureInfo.CurrentCulture = _previousCulture!;
CultureInfo.CurrentUICulture = _previousUICulture!;

base.After(methodUnderTest);
}
}
29 changes: 0 additions & 29 deletions tests/Avalonia.UnitTests/InvariantCultureFixture.cs

This file was deleted.

0 comments on commit e6c1670

Please sign in to comment.