-
Notifications
You must be signed in to change notification settings - Fork 8.2k
LanguagePrimitives: fix second enum value in wildcard duplicate-match error #27104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,4 +223,37 @@ Describe "Language Primitive Tests" -Tags "CI" { | |
| $convertedValue = [System.Management.Automation.LanguagePrimitives]::ConvertTo($formattedNumber, [bigint]) | ||
| $convertedValue | Should -Be 1 | ||
| } | ||
|
|
||
| It 'Wildcard enum string matching two members lists both distinct values in the cast error' { | ||
| # Regression: the second conflicting match used values.GetValue(i) (outer source index) instead of | ||
| # values.GetValue(j) (enum member index), so the message repeated the first member as the second. | ||
| # LanguagePrimitives.ConvertTo(string, enum) often resolves via Enum.Parse / EnumMinimumDisambiguation | ||
| # first; EnumSingleTypeConverter.ConvertFrom hits the wildcard branch that contains the fix. | ||
| $enumTypeName = 'TestLanguagePrimitivesEnumDup.TwoMemberNoFlags' | ||
| if (-not ($enumTypeName -as [type])) { | ||
| Add-Type -TypeDefinition @' | ||
| namespace TestLanguagePrimitivesEnumDup { | ||
| public enum TwoMemberNoFlags { | ||
| Alpha = 0, | ||
| Beta = 1 | ||
| } | ||
| } | ||
| '@ | ||
| } | ||
|
|
||
| $enumType = [TestLanguagePrimitivesEnumDup.TwoMemberNoFlags] | ||
| $asm = [System.Management.Automation.LanguagePrimitives].Assembly | ||
| $converterType = $asm.GetType('System.Management.Automation.LanguagePrimitives+EnumSingleTypeConverter') | ||
| $inst = [Activator]::CreateInstance($converterType, $true) | ||
| $ex = $null | ||
| try { | ||
| $null = $inst.ConvertFrom('*', $enumType, [cultureinfo]::InvariantCulture, $true) | ||
| } catch { | ||
| $ex = $_.Exception.GetBaseException() | ||
| } | ||
|
|
||
| $ex | Should -BeOfType [System.Management.Automation.PSInvalidCastException] | ||
| $ex.Message | Should -Match '\(Alpha, Beta\)' | ||
| $ex.Message | Should -Not -Match '\(Alpha, Alpha\)' | ||
|
Comment on lines
+255
to
+257
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$converterType = $asm.GetType(...)can return$nullif the nested type name changes, which would make the test fail with a less-informative exception when creating the instance or callingConvertFrom. Consider adding explicit assertions that$converterType(and$inst) are not$nullso failures are clearer.