ReplaceCulture.cs 3.3 KB

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