ReplaceCulture.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System.Globalization;
  4. using System.Reflection;
  5. using Xunit.v3;
  6. namespace Jint.Tests;
  7. /// <summary>
  8. /// Replaces the current culture and UI culture for the test.
  9. /// </summary>
  10. [AttributeUsage(AttributeTargets.Method)]
  11. public class ReplaceCultureAttribute : BeforeAfterTestAttribute
  12. {
  13. private CultureInfo _originalCulture;
  14. private CultureInfo _originalUICulture;
  15. /// <summary>
  16. /// Replaces the current culture and UI culture based on specified value.
  17. /// </summary>
  18. public ReplaceCultureAttribute(string currentCulture) : this(currentCulture, currentCulture)
  19. {
  20. }
  21. /// <summary>
  22. /// Replaces the current culture and UI culture based on specified values.
  23. /// </summary>
  24. public ReplaceCultureAttribute(string currentCulture, string currentUICulture)
  25. {
  26. Culture = new CultureInfo(currentCulture);
  27. UICulture = new CultureInfo(currentUICulture);
  28. }
  29. #if NETFRAMEWORK
  30. /// <summary>
  31. /// The <see cref="Thread.CurrentCulture"/> for the test. Defaults to en-GB.
  32. /// </summary>
  33. /// <remarks>
  34. /// en-GB is used here as the default because en-US is equivalent to the InvariantCulture. We
  35. /// want to be able to find bugs where we're accidentally relying on the Invariant instead of the
  36. /// user's culture.
  37. /// </remarks>
  38. #else
  39. /// <summary>
  40. /// The <see cref="CultureInfo.CurrentCulture"/> for the test. Defaults to en-GB.
  41. /// </summary>
  42. /// <remarks>
  43. /// en-GB is used here as the default because en-US is equivalent to the InvariantCulture. We
  44. /// want to be able to find bugs where we're accidentally relying on the Invariant instead of the
  45. /// user's culture.
  46. /// </remarks>
  47. #endif
  48. public CultureInfo Culture { get; }
  49. #if NETFRAMEWORK
  50. /// <summary>
  51. /// The <see cref="Thread.CurrentUICulture"/> for the test. Defaults to en-US.
  52. /// </summary>
  53. #else
  54. /// <summary>
  55. /// The <see cref="CultureInfo.CurrentUICulture"/> for the test. Defaults to en-US.
  56. /// </summary>
  57. #endif
  58. public CultureInfo UICulture { get; }
  59. public override void Before(MethodInfo methodUnderTest, IXunitTest test)
  60. {
  61. _originalCulture = CultureInfo.CurrentCulture;
  62. _originalUICulture = CultureInfo.CurrentUICulture;
  63. #if NETFRAMEWORK
  64. System.Threading.Thread.CurrentThread.CurrentCulture = Culture;
  65. System.Threading.Thread.CurrentThread.CurrentUICulture = UICulture;
  66. #else
  67. CultureInfo.CurrentCulture = Culture;
  68. CultureInfo.CurrentUICulture = UICulture;
  69. #endif
  70. }
  71. public override void After(MethodInfo methodUnderTest, IXunitTest test)
  72. {
  73. #if NETFRAMEWORK
  74. System.Threading.Thread.CurrentThread.CurrentCulture = _originalCulture;
  75. System.Threading.Thread.CurrentThread.CurrentUICulture = _originalUICulture;
  76. #else
  77. CultureInfo.CurrentCulture = _originalCulture;
  78. CultureInfo.CurrentUICulture = _originalUICulture;
  79. #endif
  80. }
  81. }