// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
namespace Jint.Tests;
///
/// Replaces the current culture and UI culture for the test.
///
[AttributeUsage(AttributeTargets.Method)]
public class ReplaceCultureAttribute : BeforeAfterTestAttribute
{
private CultureInfo _originalCulture;
private CultureInfo _originalUICulture;
///
/// Replaces the current culture and UI culture based on specified value.
///
public ReplaceCultureAttribute(string currentCulture) : this(currentCulture, currentCulture)
{
}
///
/// Replaces the current culture and UI culture based on specified values.
///
public ReplaceCultureAttribute(string currentCulture, string currentUICulture)
{
Culture = new CultureInfo(currentCulture);
UICulture = new CultureInfo(currentUICulture);
}
#if NETFRAMEWORK
///
/// The for the test. Defaults to en-GB.
///
///
/// en-GB is used here as the default because en-US is equivalent to the InvariantCulture. We
/// want to be able to find bugs where we're accidentally relying on the Invariant instead of the
/// user's culture.
///
#else
///
/// The for the test. Defaults to en-GB.
///
///
/// en-GB is used here as the default because en-US is equivalent to the InvariantCulture. We
/// want to be able to find bugs where we're accidentally relying on the Invariant instead of the
/// user's culture.
///
#endif
public CultureInfo Culture { get; }
#if NETFRAMEWORK
///
/// The for the test. Defaults to en-US.
///
#else
///
/// The for the test. Defaults to en-US.
///
#endif
public CultureInfo UICulture { get; }
public override void Before(MethodInfo methodUnderTest)
{
_originalCulture = CultureInfo.CurrentCulture;
_originalUICulture = CultureInfo.CurrentUICulture;
#if NETFRAMEWORK
System.Threading.Thread.CurrentThread.CurrentCulture = Culture;
System.Threading.Thread.CurrentThread.CurrentUICulture = UICulture;
#else
CultureInfo.CurrentCulture = Culture;
CultureInfo.CurrentUICulture = UICulture;
#endif
}
public override void After(MethodInfo methodUnderTest)
{
#if NETFRAMEWORK
System.Threading.Thread.CurrentThread.CurrentCulture = _originalCulture;
System.Threading.Thread.CurrentThread.CurrentUICulture = _originalUICulture;
#else
CultureInfo.CurrentCulture = _originalCulture;
CultureInfo.CurrentUICulture = _originalUICulture;
#endif
}
}