// 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;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
namespace Microsoft.AspNetCore.Mvc.Testing.Xunit.Internal
{
///
/// Replaces the current culture and UI culture for the test.
///
[AttributeUsage(AttributeTargets.Method)]
internal class ReplaceCultureAttribute : BeforeAfterTestAttribute
{
private const string _defaultCultureName = "en-GB";
private const string _defaultUICultureName = "en-US";
private CultureInfo _originalCulture;
private CultureInfo _originalUICulture;
///
/// Replaces the current culture and UI culture to en-GB and en-US respectively.
///
public ReplaceCultureAttribute() :
this(_defaultCultureName, _defaultUICultureName)
{
}
///
/// 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);
}
///
/// 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.
///
public CultureInfo Culture { get; }
///
/// The for the test. Defaults to en-US.
///
public CultureInfo UICulture { get; }
public override void Before(MethodInfo methodUnderTest)
{
_originalCulture = CultureInfo.CurrentCulture;
_originalUICulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentCulture = Culture;
CultureInfo.CurrentUICulture = UICulture;
}
public override void After(MethodInfo methodUnderTest)
{
CultureInfo.CurrentCulture = _originalCulture;
CultureInfo.CurrentUICulture = _originalUICulture;
}
}
}