diff --git a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs index 1dbd5a6ad0..4781e3d1d8 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/FormatViewGenerator_Complex.cs @@ -149,7 +149,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format TypeMatch match = new TypeMatch(_expressionFactory, _db, typeNames); foreach (ComplexControlEntryDefinition x in complexBody.optionalEntryList) { - if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo))) + if (match.PerfectMatch(new TypeMatchItem(x, x.appliesTo, so))) { return x; } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 index 55d211a012..2578ccdd92 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Format-Custom.Tests.ps1 @@ -298,3 +298,145 @@ class MyLeaf2 $result | Should -Be $expectedResult } } + +Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl" -Tags "CI" { + BeforeAll { + $formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml' + $xmlContent = @' + + + + + MyTestControl + + + + + + MyTestObject + $_.Name -eq 'SelectScriptBlock' + + + + Entry selected by ScriptBlock + + + + + + MyTestObject + SelectProperty + + + + Entry selected by property + + + + + Default was picked + + + + + + + + + DefaultView + + MyTestObject + + + Name + MyTestControl + + + + + + + + + + Name + + + + + + + + +'@ + + Set-Content -Path $formatFilePath -Value $xmlContent + $ps = [powershell]::Create() + $iss = [initialsessionstate]::CreateDefault2() + $iss.Formats.Add($formatFilePath) + $rs = [runspacefactory]::CreateRunspace($iss) + $rs.Open() + $ps.Runspace = $rs + } + + AfterAll { + $rs.Close() + $ps.Dispose() + } + + It 'Property expression binding should be able to access the current object' { + $script = { + [PSCustomObject]@{ + PSTypeName = 'MyTestObject' + SelectProperty = $true + Name = 'testing' + } + } + + $null = $ps.AddScript($script).AddCommand('Out-String') + $ps.Streams.Error.Clear() + $expectedOutput = @' + + +Entry selected by property + +Name +---- +testing + + + +'@ -replace '\r?\n', [Environment]::NewLine + + $ps.Invoke() | Should -BeExactly $expectedOutput + $ps.Streams.Error | Should -BeNullOrEmpty + } + + It 'ScriptBlock expression binding should be able to access the current object' { + $script = { + [PSCustomObject]@{ + PSTypeName = 'MyTestObject' + SelectProperty = $false + Name = 'SelectScriptBlock' + } + } + + $null = $ps.AddScript($script).AddCommand('Out-String') + $ps.Streams.Error.Clear() + $expectedOutput = @' + + +Entry selected by ScriptBlock + +Name +---- +SelectScriptBlock + + + +'@ -replace '\r?\n', [Environment]::NewLine + + $ps.Invoke() | Should -BeExactly $expectedOutput + $ps.Streams.Error | Should -BeNullOrEmpty + } +}