InvariantCultureAttribute.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #nullable enable
  2. using System;
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Xunit.Sdk;
  6. namespace Avalonia.UnitTests;
  7. /// <summary>
  8. /// Runs tests in the invariant culture.
  9. /// </summary>
  10. /// <remarks>
  11. /// Some tests check exception messages, and those from the .NET framework will be translated.
  12. /// Some tests are formatting numbers, expecting a dot as a decimal point.
  13. /// Use this fixture to set the current culture to the invariant culture.
  14. /// </remarks>
  15. [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
  16. public sealed class InvariantCultureAttribute : BeforeAfterTestAttribute
  17. {
  18. private CultureInfo? _previousCulture;
  19. private CultureInfo? _previousUICulture;
  20. public override void Before(MethodInfo methodUnderTest)
  21. {
  22. base.Before(methodUnderTest);
  23. _previousCulture = CultureInfo.CurrentCulture;
  24. _previousUICulture = CultureInfo.CurrentUICulture;
  25. CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
  26. CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
  27. }
  28. public override void After(MethodInfo methodUnderTest)
  29. {
  30. CultureInfo.CurrentCulture = _previousCulture!;
  31. CultureInfo.CurrentUICulture = _previousUICulture!;
  32. base.After(methodUnderTest);
  33. }
  34. }