/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ #region License // Copyright (c) .NET Foundation and Contributors // All Rights Reserved // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // https://github.com/xunit/samples.xunit/blob/master/UseCulture/UseCultureAttribute.cs #endregion using System; using System.Globalization; using System.Reflection; using System.Threading; using Xunit.Sdk; namespace Tests.Reproduce { /// /// Apply this attribute to your test method to replace the /// and /// with another culture. /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class UseCultureAttribute : BeforeAfterTestAttribute { private readonly Lazy _culture; private readonly Lazy _uiCulture; private CultureInfo _originalCulture; private CultureInfo _originalUiCulture; /// /// Replaces the culture and UI culture of the current thread with /// /// /// The name of the culture. /// /// /// This constructor overload uses for both /// and . /// /// public UseCultureAttribute(string culture) : this(culture, culture) { } /// /// Replaces the culture and UI culture of the current thread with /// and /// /// The name of the culture. /// The name of the UI culture. public UseCultureAttribute(string culture, string uiCulture) { _culture = new Lazy(() => new CultureInfo(culture, false)); _uiCulture = new Lazy(() => new CultureInfo(uiCulture, false)); } /// /// Gets the culture. /// public CultureInfo Culture => _culture.Value; /// /// Gets the UI culture. /// public CultureInfo UICulture => _uiCulture.Value; /// /// Stores the current /// and /// and replaces them with the new cultures defined in the constructor. /// /// The method under test public override void Before(MethodInfo methodUnderTest) { _originalCulture = Thread.CurrentThread.CurrentCulture; _originalUiCulture = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentCulture = Culture; Thread.CurrentThread.CurrentUICulture = UICulture; CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); } /// /// Restores the original and /// to /// /// The method under test public override void After(MethodInfo methodUnderTest) { Thread.CurrentThread.CurrentCulture = _originalCulture; Thread.CurrentThread.CurrentUICulture = _originalUiCulture; CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); } } }