Use invariant culture string conversion for -replace operator (#10954)

This commit is contained in:
Ilya
2019-12-19 09:50:46 -08:00
committed by Dongbo Wang
parent a1066ac0ac
commit 54e6199bbb
4 changed files with 45 additions and 2 deletions
@@ -117,6 +117,9 @@ namespace System.Management.Automation
new ExperimentalFeature(
name: "PSNullConditionalOperators",
description: "Support the null conditional member access operators in PowerShell language"),
new ExperimentalFeature(
name: "PSCultureInvariantReplaceOperator",
description: "Use culture invariant to-string convertor for lval in replace operator"),
};
EngineExperimentalFeatures = new ReadOnlyCollection<ExperimentalFeature>(engineFeatures);
@@ -967,7 +967,15 @@ namespace System.Management.Automation
IEnumerator list = LanguagePrimitives.GetEnumerator(lval);
if (list == null)
{
string lvalString = lval?.ToString() ?? string.Empty;
string lvalString;
if (ExperimentalFeature.IsEnabled("PSCultureInvariantReplaceOperator"))
{
lvalString = PSObject.ToStringParser(context, lval) ?? string.Empty;
}
else
{
lvalString = lval?.ToString() ?? string.Empty;
}
return ReplaceOperatorImpl(context, lvalString, rr, substitute);
}
@@ -64,4 +64,35 @@ Describe "Replace Operator" -Tags CI {
$res | Should -BeExactly "ID XXXX123"
}
}
Describe "Culture-invariance tests for -split and -replace" -Tags CI {
BeforeAll {
$skipTest = -not [ExperimentalFeature]::IsEnabled("PSCultureInvariantReplaceOperator")
if ($skipTest) {
Write-Verbose "Test Suite Skipped. The test suite requires the experimental feature 'PSCultureInvariantReplaceOperator' to be enabled." -Verbose
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues["it:skip"] = $true
} else {
$prevCulture = [cultureinfo]::CurrentCulture
# The French culture uses "," as the decimal mark.
[cultureinfo]::CurrentCulture = 'fr'
}
}
AfterAll {
if ($skipTest) {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
} else {
[cultureinfo]::CurrentCulture = $prevCulture
}
}
It "-split: LHS stringification is not culture-sensitive" {
1.2 -split ',' | Should -Be '1.2'
}
It "-replace: LHS stringification is not culture-sensitive" {
1.2 -replace ',' | Should -Be '1.2'
}
}
}
+2 -1
View File
@@ -1,5 +1,6 @@
{
"ExperimentalFeatures": {
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ]
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ],
"PSCultureInvariantReplaceOperator": [ "test/powershell/Language/Operators/ReplaceOperator.Tests.ps1" ]
}
}