ReplaceCulture.cs 3.3 KB

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