ReplaceCulture.cs 3.2 KB

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