Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ private object CoerceTypeAsNeeded(
_context.LanguageMode = PSLanguageMode.FullLanguage;
}

result = LanguagePrimitives.ConvertTo(currentValue, toType, CultureInfo.CurrentCulture);
result = LanguagePrimitives.ConvertTo(currentValue, toType, CultureInfo.InvariantCulture);
}
finally
{
Expand Down
13 changes: 13 additions & 0 deletions test/powershell/Language/Parser/Conversions.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ Describe 'conversion syntax' -Tags "CI" {
([System.Management.Automation.ActionPreference]"Stop").ToString() | Should Be "Stop"
}

It "language conversion uses 'InvariantCulture' by default" {
$dateString = "1/11/1111"
$expected = [System.Management.Automation.LanguagePrimitives]::ConvertTo($dateString, [datetime], [cultureinfo]::InvariantCulture)
$oldCulture = [CultureInfo]::CurrentCulture
$result = try {
[CultureInfo]::CurrentCulture = 'ru-RU'
[datetime] $dateString
} finally {
[CultureInfo]::CurrentCulture = $oldCulture
}
$result | Should Be $expected
}

Context "Cast object[] to more narrow generic collection" {
BeforeAll {
$testCases1 = @(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,42 @@
}
}

It "Verify implicit argument conversion uses InvariantCulture" {
function Test-Function {
param([DateTime] $date)
$date
}

function Test-ScriptCmdlet {
[CmdletBinding()]
param([DateTime] $date)
$date
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add C# cmdlet?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use Get-Date -Date instead, so no need to add a C# cmdlet.

PS:162> gcm Get-Date -Syntax
Get-Date [[-Date] <datetime>] ...


# Explict type cast using 'InvariantCulture'
$expected = [System.Management.Automation.LanguagePrimitives]::ConvertTo("1/11/1111", [datetime], [cultureinfo]::InvariantCulture)

# Cast result with ru-RU culture
$ruCulture = [cultureinfo]::GetCultureInfo("ru-RU")
$ruCastResult = [System.Management.Automation.LanguagePrimitives]::ConvertTo("1/11/1111", [datetime], $ruCulture)

# Implict parameter argument conversion should also use 'InvariantCulture'
$oldCulture = [CultureInfo]::CurrentCulture
$csharpCmdlet, $scriptFunction, $scriptCmdlet = try {
[CultureInfo]::CurrentCulture = $ruCulture
Get-Date -Date 1/11/1111
Test-Function 1/11/1111
Test-ScriptCmdlet 1/11/1111
} finally {
[CultureInfo]::CurrentCulture = $oldCulture
}

$expected | Should Not Be $ruCastResult
$csharpCmdlet | Should Be $expected
$scriptFunction | Should Be $expected
$scriptFunction | Should Be $expected
}

Context "Use automatic variables as default value for parameters" {
BeforeAll {
## Explicit use of 'CmdletBinding' make it a script cmdlet
Expand Down