@@ -298,3 +298,145 @@ class MyLeaf2
298298 $result | Should - Be $expectedResult
299299 }
300300}
301+
302+ Describe " Format-Custom with expression based EntrySelectedBy in a CustomControl" - Tags " CI" {
303+ BeforeAll {
304+ $formatFilePath = Join-Path $TestDrive ' UpdateFormatDataTests.format.ps1xml'
305+ $xmlContent = @'
306+ <?xml version="1.0" encoding="UTF-8" ?>
307+ <Configuration>
308+ <Controls>
309+ <Control>
310+ <Name>MyTestControl</Name>
311+ <CustomControl>
312+ <CustomEntries>
313+ <CustomEntry>
314+ <EntrySelectedBy>
315+ <SelectionCondition>
316+ <TypeName>MyTestObject</TypeName>
317+ <ScriptBlock>$_.Name -eq 'SelectScriptBlock'</ScriptBlock>
318+ </SelectionCondition>
319+ </EntrySelectedBy>
320+ <CustomItem>
321+ <Text>Entry selected by ScriptBlock</Text>
322+ </CustomItem>
323+ </CustomEntry>
324+ <CustomEntry>
325+ <EntrySelectedBy>
326+ <SelectionCondition>
327+ <TypeName>MyTestObject</TypeName>
328+ <PropertyName>SelectProperty</PropertyName>
329+ </SelectionCondition>
330+ </EntrySelectedBy>
331+ <CustomItem>
332+ <Text>Entry selected by property</Text>
333+ </CustomItem>
334+ </CustomEntry>
335+ <CustomEntry>
336+ <CustomItem>
337+ <Text>Default was picked</Text>
338+ </CustomItem>
339+ </CustomEntry>
340+ </CustomEntries>
341+ </CustomControl>
342+ </Control>
343+ </Controls>
344+ <ViewDefinitions>
345+ <View>
346+ <Name>DefaultView</Name>
347+ <ViewSelectedBy>
348+ <TypeName>MyTestObject</TypeName>
349+ </ViewSelectedBy>
350+ <GroupBy>
351+ <PropertyName>Name</PropertyName>
352+ <CustomControlName>MyTestControl</CustomControlName>
353+ </GroupBy>
354+ <TableControl>
355+ <TableHeaders>
356+ <TableColumnHeader />
357+ </TableHeaders>
358+ <TableRowEntries>
359+ <TableRowEntry>
360+ <TableColumnItems>
361+ <TableColumnItem>
362+ <PropertyName>Name</PropertyName>
363+ </TableColumnItem>
364+ </TableColumnItems>
365+ </TableRowEntry>
366+ </TableRowEntries>
367+ </TableControl>
368+ </View>
369+ </ViewDefinitions>
370+ </Configuration>
371+ '@
372+
373+ Set-Content - Path $formatFilePath - Value $xmlContent
374+ $ps = [powershell ]::Create()
375+ $iss = [initialsessionstate ]::CreateDefault2()
376+ $iss.Formats.Add ($formatFilePath )
377+ $rs = [runspacefactory ]::CreateRunspace($iss )
378+ $rs.Open ()
379+ $ps.Runspace = $rs
380+ }
381+
382+ AfterAll {
383+ $rs.Close ()
384+ $ps.Dispose ()
385+ }
386+
387+ It ' Property expression binding should be able to access the current object' {
388+ $script = {
389+ [PSCustomObject ]@ {
390+ PSTypeName = ' MyTestObject'
391+ SelectProperty = $true
392+ Name = ' testing'
393+ }
394+ }
395+
396+ $null = $ps.AddScript ($script ).AddCommand(' Out-String' )
397+ $ps.Streams.Error.Clear ()
398+ $expectedOutput = @'
399+
400+
401+ Entry selected by property
402+
403+ Name
404+ ----
405+ testing
406+
407+
408+
409+ '@ -replace ' \r?\n' , [Environment ]::NewLine
410+
411+ $ps.Invoke () | Should - BeExactly $expectedOutput
412+ $ps.Streams.Error | Should - BeNullOrEmpty
413+ }
414+
415+ It ' ScriptBlock expression binding should be able to access the current object' {
416+ $script = {
417+ [PSCustomObject ]@ {
418+ PSTypeName = ' MyTestObject'
419+ SelectProperty = $false
420+ Name = ' SelectScriptBlock'
421+ }
422+ }
423+
424+ $null = $ps.AddScript ($script ).AddCommand(' Out-String' )
425+ $ps.Streams.Error.Clear ()
426+ $expectedOutput = @'
427+
428+
429+ Entry selected by ScriptBlock
430+
431+ Name
432+ ----
433+ SelectScriptBlock
434+
435+
436+
437+ '@ -replace ' \r?\n' , [Environment ]::NewLine
438+
439+ $ps.Invoke () | Should - BeExactly $expectedOutput
440+ $ps.Streams.Error | Should - BeNullOrEmpty
441+ }
442+ }
0 commit comments