|
| 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 | + |
| 4 | +using System; |
| 5 | +using System.Globalization; |
| 6 | +using System.Threading; |
| 7 | +using Xunit; |
| 8 | + |
| 9 | +namespace Microsoft.AspNet.Localization.Tests |
| 10 | +{ |
| 11 | + public class RequestLocalizationOptionsTest : IDisposable |
| 12 | + { |
| 13 | + private readonly CultureInfo _initialCulture; |
| 14 | + private readonly CultureInfo _initialUICulture; |
| 15 | + |
| 16 | + public RequestLocalizationOptionsTest() |
| 17 | + { |
| 18 | + _initialCulture = CultureInfo.CurrentCulture; |
| 19 | + _initialUICulture = CultureInfo.CurrentUICulture; |
| 20 | + } |
| 21 | + |
| 22 | + [Fact] |
| 23 | + public void DefaultRequestCulture_DefaultsToCurrentCulture() |
| 24 | + { |
| 25 | + // Arrange/Act |
| 26 | + var options = new RequestLocalizationOptions(); |
| 27 | + |
| 28 | + // Assert |
| 29 | + Assert.NotNull(options.DefaultRequestCulture); |
| 30 | + Assert.Equal(CultureInfo.CurrentCulture, options.DefaultRequestCulture.Culture); |
| 31 | + Assert.Equal(CultureInfo.CurrentUICulture, options.DefaultRequestCulture.UICulture); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void DefaultRequestCulture_DefaultsToCurrentCultureWhenExplicitlySet() |
| 36 | + { |
| 37 | + // Arrange |
| 38 | + var explicitCulture = new CultureInfo("fr-FR"); |
| 39 | +#if DNX451 |
| 40 | + Thread.CurrentThread.CurrentCulture = explicitCulture; |
| 41 | + Thread.CurrentThread.CurrentUICulture = explicitCulture; |
| 42 | +#else |
| 43 | + CultureInfo.CurrentCulture = explicitCulture; |
| 44 | + CultureInfo.CurrentUICulture = explicitCulture; |
| 45 | +#endif |
| 46 | + // Act |
| 47 | + var options = new RequestLocalizationOptions(); |
| 48 | + |
| 49 | + // Assert |
| 50 | + Assert.Equal(explicitCulture, options.DefaultRequestCulture.Culture); |
| 51 | + Assert.Equal(explicitCulture, options.DefaultRequestCulture.UICulture); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void DefaultRequestCulture_ThrowsWhenTryingToSetToNull() |
| 56 | + { |
| 57 | + // Arrange |
| 58 | + var options = new RequestLocalizationOptions(); |
| 59 | + |
| 60 | + // Act/Assert |
| 61 | + Assert.Throws(typeof(ArgumentNullException), () => options.DefaultRequestCulture = null); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void SupportedCultures_DefaultsToCurrentCulture() |
| 66 | + { |
| 67 | + // Arrange/Act |
| 68 | + var options = new RequestLocalizationOptions(); |
| 69 | + |
| 70 | + // Assert |
| 71 | + Assert.Collection(options.SupportedCultures, item => Assert.Equal(CultureInfo.CurrentCulture, item)); |
| 72 | + Assert.Collection(options.SupportedUICultures, item => Assert.Equal(CultureInfo.CurrentUICulture, item)); |
| 73 | + } |
| 74 | + |
| 75 | + [Fact] |
| 76 | + public void SupportedCultures_DefaultsToCurrentCultureWhenExplicitlySet() |
| 77 | + { |
| 78 | + // Arrange |
| 79 | + var explicitCulture = new CultureInfo("fr-FR"); |
| 80 | +#if DNX451 |
| 81 | + Thread.CurrentThread.CurrentCulture = explicitCulture; |
| 82 | + Thread.CurrentThread.CurrentUICulture = explicitCulture; |
| 83 | +#else |
| 84 | + CultureInfo.CurrentCulture = explicitCulture; |
| 85 | + CultureInfo.CurrentUICulture = explicitCulture; |
| 86 | +#endif |
| 87 | + |
| 88 | + // Act |
| 89 | + var options = new RequestLocalizationOptions(); |
| 90 | + |
| 91 | + // Assert |
| 92 | + Assert.Collection(options.SupportedCultures, item => Assert.Equal(explicitCulture, item)); |
| 93 | + Assert.Collection(options.SupportedUICultures, item => Assert.Equal(explicitCulture, item)); |
| 94 | + } |
| 95 | + |
| 96 | + public void Dispose() |
| 97 | + { |
| 98 | +#if DNX451 |
| 99 | + Thread.CurrentThread.CurrentCulture = _initialCulture; |
| 100 | + Thread.CurrentThread.CurrentUICulture = _initialUICulture; |
| 101 | +#else |
| 102 | + CultureInfo.CurrentCulture = _initialCulture; |
| 103 | + CultureInfo.CurrentUICulture = _initialUICulture; |
| 104 | +#endif |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments