< Summary - CoreWCF Coverage — PR #1766

Information
Class: UseCultureAttribute
Assembly: UnitTests.Common
File(s): /home/runner/work/CoreWCF/CoreWCF/src/Common/UnitTests.Common/UseCultureAttribute.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 93
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
Before(...)100%11100%
After(...)100%11100%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/Common/UnitTests.Common/UseCultureAttribute.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Globalization;
 6using System.Reflection;
 7using System.Threading;
 8using Xunit.v3;
 9
 10/// <summary>
 11/// Apply this attribute to your test method to replace the
 12/// <see cref="Thread.CurrentThread" /> <see cref="CultureInfo.CurrentCulture" /> and
 13/// <see cref="CultureInfo.CurrentUICulture" /> with another culture.
 14/// </summary>
 15[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
 16public class UseCultureAttribute : BeforeAfterTestAttribute
 17{
 18    readonly Lazy<CultureInfo> culture;
 19    readonly Lazy<CultureInfo> uiCulture;
 20
 21    CultureInfo originalCulture;
 22    CultureInfo originalUICulture;
 23
 24    /// <summary>
 25    /// Replaces the culture and UI culture of the current thread with
 26    /// <paramref name="culture" />
 27    /// </summary>
 28    /// <param name="culture">The name of the culture.</param>
 29    /// <remarks>
 30    /// <para>
 31    /// This constructor overload uses <paramref name="culture" /> for both
 32    /// <see cref="Culture" /> and <see cref="UICulture" />.
 33    /// </para>
 34    /// </remarks>
 35    public UseCultureAttribute(string culture)
 836        : this(culture, culture) { }
 37
 38    /// <summary>
 39    /// Replaces the culture and UI culture of the current thread with
 40    /// <paramref name="culture" /> and <paramref name="uiCulture" />
 41    /// </summary>
 42    /// <param name="culture">The name of the culture.</param>
 43    /// <param name="uiCulture">The name of the UI culture.</param>
 444    public UseCultureAttribute(string culture, string uiCulture)
 45    {
 746        this.culture = new Lazy<CultureInfo>(() => new CultureInfo(culture, false));
 747        this.uiCulture = new Lazy<CultureInfo>(() => new CultureInfo(uiCulture, false));
 448    }
 49
 50    /// <summary>
 51    /// Gets the culture.
 52    /// </summary>
 353    public CultureInfo Culture { get { return culture.Value; } }
 54
 55    /// <summary>
 56    /// Gets the UI culture.
 57    /// </summary>
 358    public CultureInfo UICulture { get { return uiCulture.Value; } }
 59
 60    /// <summary>
 61    /// Stores the current <see cref="Thread.CurrentPrincipal" />
 62    /// <see cref="CultureInfo.CurrentCulture" /> and <see cref="CultureInfo.CurrentUICulture" />
 63    /// and replaces them with the new cultures defined in the constructor.
 64    /// </summary>
 65    /// <param name="methodUnderTest">The method under test</param>
 66    /// <param name="test">The test being run</param>
 67    public override void Before(MethodInfo methodUnderTest, IXunitTest test)
 68    {
 369        originalCulture = Thread.CurrentThread.CurrentCulture;
 370        originalUICulture = Thread.CurrentThread.CurrentUICulture;
 71
 372        Thread.CurrentThread.CurrentCulture = Culture;
 373        Thread.CurrentThread.CurrentUICulture = UICulture;
 74
 375        CultureInfo.CurrentCulture.ClearCachedData();
 376        CultureInfo.CurrentUICulture.ClearCachedData();
 377    }
 78
 79    /// <summary>
 80    /// Restores the original <see cref="CultureInfo.CurrentCulture" /> and
 81    /// <see cref="CultureInfo.CurrentUICulture" /> to <see cref="Thread.CurrentPrincipal" />
 82    /// </summary>
 83    /// <param name="methodUnderTest">The method under test</param>
 84    /// <param name="test">The test being run</param>
 85    public override void After(MethodInfo methodUnderTest, IXunitTest test)
 86    {
 387        Thread.CurrentThread.CurrentCulture = originalCulture;
 388        Thread.CurrentThread.CurrentUICulture = originalUICulture;
 89
 390        CultureInfo.CurrentCulture.ClearCachedData();
 391        CultureInfo.CurrentUICulture.ClearCachedData();
 392    }
 93}