From 49a826317aa6a912e1438259ffbb29f4b29318d7 Mon Sep 17 00:00:00 2001 From: "B. Nordli" Date: Wed, 15 Jun 2016 17:50:24 +0200 Subject: [PATCH] Fix tests in PatternMatchingTests that fails in cultures with different decimal separator than '.'. --- .../Semantics/PatternMatchingTests.cs | 56 ++++++++++++----- .../Shared/FX/EnsureEnglishCulture.cs | 63 +++++++++++++++++++ .../Utilities/Shared/TestUtilities.projitems | 1 + 3 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 src/Test/Utilities/Shared/FX/EnsureEnglishCulture.cs diff --git a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs index 678fdcd1b23..4539b113bad 100644 --- a/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs +++ b/src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs @@ -177,7 +177,9 @@ public static void Test(object x) var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics( ); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"expression 1 is not String expression foo is not Int32 expression 1 is Int32 1 @@ -186,7 +188,8 @@ public static void Test(object x) expression 1 is Nullable`1 1 expression is not Nullable`1 expression is not String"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact] @@ -285,11 +288,14 @@ private static void M(object o) "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"No for 1 Yes for 10 No for 1.2"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact] @@ -317,11 +323,14 @@ private static bool M(object o, bool result) "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"False for 1 True for 10 False for 1.2"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact] @@ -350,11 +359,14 @@ private static bool M(object o, bool result) "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"False for 1 True for 10 False for 1.2"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact, WorkItem(8778, "https://github.com/dotnet/roslyn/issues/8778")] @@ -383,11 +395,14 @@ private static bool M(object o, bool result) "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions.WithLocalFunctionsFeature()); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"False for 1 True for 10 False for 1.2"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact, WorkItem(8778, "https://github.com/dotnet/roslyn/issues/8778")] @@ -418,11 +433,14 @@ private static bool M(object o, bool result) "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"False for 1 True for 10 False for 1.2"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact] @@ -489,11 +507,14 @@ private static bool M(object o, bool result) "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"False for 1 True for 10 False for 1.2"; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact] @@ -539,7 +560,9 @@ public static void Main() "; var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe, parseOptions: patternParseOptions); compilation.VerifyDiagnostics(); - var expectedOutput = + using (new EnsureEnglishCulture()) + { + var expectedOutput = @"one int 10 long 20 @@ -550,7 +573,8 @@ struct Boolean True struct X X class Exception System.Exception: boo "; - var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + var comp = CompileAndVerify(compilation, expectedOutput: expectedOutput); + } } [Fact] diff --git a/src/Test/Utilities/Shared/FX/EnsureEnglishCulture.cs b/src/Test/Utilities/Shared/FX/EnsureEnglishCulture.cs new file mode 100644 index 00000000000..15215cc9a5e --- /dev/null +++ b/src/Test/Utilities/Shared/FX/EnsureEnglishCulture.cs @@ -0,0 +1,63 @@ +// Copyright (c) Microsoft. 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.Diagnostics; +using System.Globalization; +using System.Threading; + +namespace Roslyn.Test.Utilities +{ + public class EnsureEnglishCulture : IDisposable + { + public static CultureInfo PreferredOrNull + { + get + { + var currentCultureName = CultureInfo.CurrentCulture.Name; + if (currentCultureName.Length == 0 || currentCultureName.StartsWith("en", StringComparison.OrdinalIgnoreCase)) + { + return null; + } + + return CultureInfo.InvariantCulture; + } + } + + private bool _needToRestore; + private readonly CultureInfo _threadCulture; + private readonly int _threadId; + + public EnsureEnglishCulture() + { + _threadId = Thread.CurrentThread.ManagedThreadId; + var preferred = PreferredOrNull; + + if (preferred != null) + { + _threadCulture = CultureInfo.CurrentCulture; + _needToRestore = true; + +#if DNX + CultureInfo.CurrentCulture = preferred; +#else + Thread.CurrentThread.CurrentCulture = preferred; +#endif + } + } + + public void Dispose() + { + Debug.Assert(_threadId == Thread.CurrentThread.ManagedThreadId); + + if (_needToRestore && _threadId == Thread.CurrentThread.ManagedThreadId) + { + _needToRestore = false; +#if DNX + CultureInfo.CurrentCulture = _threadCulture; +#else + Thread.CurrentThread.CurrentCulture = _threadCulture; +#endif + } + } + } +} diff --git a/src/Test/Utilities/Shared/TestUtilities.projitems b/src/Test/Utilities/Shared/TestUtilities.projitems index 05bc65735ee..1fb49246e3d 100644 --- a/src/Test/Utilities/Shared/TestUtilities.projitems +++ b/src/Test/Utilities/Shared/TestUtilities.projitems @@ -43,6 +43,7 @@ + -- GitLab