[Tests]Disallow user override of culture options while running tests (#28207)

* [Tests]Disallow user override of culture options while running tests

* Revert unintended changes to the system's plugin

* Embed utc in the date to run utc tests on other timezones
This commit is contained in:
Jaime Bernardo 2023-09-04 16:16:26 +01:00 committed by GitHub
parent 2c5659d746
commit 90e5362385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 34 additions and 26 deletions

View File

@ -239,7 +239,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
// Act
// Using en-us culture to have a fixed number style
var result = engine.Interpret(input, new CultureInfo("en-us"), out _);
var result = engine.Interpret(input, new CultureInfo("en-us", false), out _);
// Assert
Assert.IsNotNull(result);

View File

@ -46,7 +46,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void Translate_ThrowError_WhenCalledNull(string input)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("de-DE"), new CultureInfo("en-US"));
var translator = NumberTranslator.Create(new CultureInfo("de-DE", false), new CultureInfo("en-US", false));
// Act
Assert.ThrowsException<ArgumentNullException>(() => translator.Translate(input));
@ -58,7 +58,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void Translate_WhenCalledEmpty(string input)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("de-DE"), new CultureInfo("en-US"));
var translator = NumberTranslator.Create(new CultureInfo("de-DE", false), new CultureInfo("en-US", false));
// Act
var result = translator.Translate(input);
@ -76,7 +76,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void Translate_NoErrors_WhenCalled(string input, string expectedResult)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("de-DE"), new CultureInfo("en-US"));
var translator = NumberTranslator.Create(new CultureInfo("de-DE", false), new CultureInfo("en-US", false));
// Act
var result = translator.Translate(input);
@ -95,7 +95,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void TranslateBack_NoErrors_WhenCalled(string input, string expectedResult)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("de-DE"), new CultureInfo("en-US"));
var translator = NumberTranslator.Create(new CultureInfo("de-DE", false), new CultureInfo("en-US", false));
// Act
var result = translator.TranslateBack(input);
@ -113,7 +113,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void Translate_RemoveNumberGroupSeparator_WhenCalled(string decimalSeparator, string groupSeparator, string input, string expectedResult)
{
// Arrange
var sourceCulture = new CultureInfo("en-US")
var sourceCulture = new CultureInfo("en-US", false)
{
NumberFormat =
{
@ -121,7 +121,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
NumberGroupSeparator = groupSeparator,
},
};
var translator = NumberTranslator.Create(sourceCulture, new CultureInfo("en-US"));
var translator = NumberTranslator.Create(sourceCulture, new CultureInfo("en-US", false));
// Act
var result = translator.Translate(input);
@ -137,7 +137,7 @@ namespace Microsoft.PowerToys.Run.Plugin.Calculator.UnitTests
public void Translate_NoRemovalOfLeadingZeroesOnEdgeCases(string input, string expectedResult)
{
// Arrange
var translator = NumberTranslator.Create(new CultureInfo("de-de"), new CultureInfo("en-US"));
var translator = NumberTranslator.Create(new CultureInfo("de-de", false), new CultureInfo("en-US", false));
// Act
var result = translator.Translate(input);

View File

@ -24,9 +24,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
}
[DataTestMethod]

View File

@ -25,9 +25,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
}
[DataTestMethod]

View File

@ -20,9 +20,9 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
}
[DataTestMethod]

View File

@ -21,14 +21,22 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
{
// Set culture to 'en-us'
originalCulture = CultureInfo.CurrentCulture;
CultureInfo.CurrentCulture = new CultureInfo("en-us");
CultureInfo.CurrentCulture = new CultureInfo("en-us", false);
originalUiCulture = CultureInfo.CurrentUICulture;
CultureInfo.CurrentUICulture = new CultureInfo("en-us");
CultureInfo.CurrentUICulture = new CultureInfo("en-us", false);
}
private DateTime GetDateTimeForTest()
private DateTime GetDateTimeForTest(bool embedUtc = false)
{
return new DateTime(2022, 03, 02, 22, 30, 45);
var dateTime = new DateTime(2022, 03, 02, 22, 30, 45);
if (embedUtc)
{
return DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);
}
else
{
return dateTime;
}
}
[DataTestMethod]
@ -181,8 +189,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithShortTimeAndShortDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, false, GetDateTimeForTest());
var expectedResult = GetDateTimeForTest().ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
var helperResults = AvailableResultsList.GetList(true, false, false, GetDateTimeForTest(true));
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@ -201,8 +209,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithShortTimeAndLongDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, false, true, GetDateTimeForTest());
var expectedResult = GetDateTimeForTest().ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
var helperResults = AvailableResultsList.GetList(true, false, true, GetDateTimeForTest(true));
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@ -221,8 +229,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithLongTimeAndShortDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, false, GetDateTimeForTest());
var expectedResult = GetDateTimeForTest().ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
var helperResults = AvailableResultsList.GetList(true, true, false, GetDateTimeForTest(true));
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));
@ -241,8 +249,8 @@ namespace Microsoft.PowerToys.Run.Plugin.TimeDate.UnitTests
public void UtcFormatsWithLongTimeAndLongDate(string formatLabel, string expectedFormat)
{
// Setup
var helperResults = AvailableResultsList.GetList(true, true, true, GetDateTimeForTest());
var expectedResult = GetDateTimeForTest().ToUniversalTime().ToString(expectedFormat, CultureInfo.CurrentCulture);
var helperResults = AvailableResultsList.GetList(true, true, true, GetDateTimeForTest(true));
var expectedResult = GetDateTimeForTest().ToString(expectedFormat, CultureInfo.CurrentCulture);
// Act
var result = helperResults.FirstOrDefault(x => x.Label.Equals(formatLabel, StringComparison.OrdinalIgnoreCase));