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
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Format-Custom" -Tags "CI" {

    Context "Check Format-Custom aliases" {

        It "Should have the same output between the alias and the unaliased function" {
            $nonaliased = Get-FormatData | Format-Custom
            $aliased    = Get-FormatData | fc
            $($nonaliased | Out-String).CompareTo($($aliased | Out-String)) | Should -Be 0
        }
    }

    Context "Check specific flags on Format-Custom" {

        It "Should be able to specify the depth in output" {
            $getprocesspester =  Get-FormatData | Format-Custom -Depth 1
            ($getprocesspester).Count | Should -BeGreaterThan 0
        }

        It "Should be able to use the Property flag to select properties" {
            Get-Command | Format-Custom -Property "Name" | Should -Not -Match "Source"
        }

    }
}

Describe "Format-Custom DRT basic functionality" -Tags "CI" {
    BeforeAll {
        if ($null -ne $PSStyle) {
            $outputRendering = $PSStyle.OutputRendering
            $PSStyle.OutputRendering = 'plaintext'
        }
    }

    AfterAll {
        if ($null -ne $PSStyle) {
            $PSStyle.OutputRendering = $outputRendering
        }
    }

    Add-Type -TypeDefinition @"
    public abstract class NamedItem
    {
        public string name;
    }

    public abstract class MyContainerBase : NamedItem
    {
        public System.Collections.Generic.List<NamedItem> children = new System.Collections.Generic.List<NamedItem> ();
    }

    public sealed class MyLeaf1 : NamedItem
    {
        public int val1 = 0;
    }

    public sealed class MyLeaf2 : NamedItem
    {
        public int val2 = 0;
    }

    public sealed class MyContainer1 : MyContainerBase
    {
        public string data1;
    }

    public sealed class MyContainer2 : MyContainerBase
    {
        public string data2;
    }
"@

    It "Format-Custom with subobject should work" {
        $expectResult1 = "this is the name"
        $expectResult2 = "this is the name of the sub object"
        $testObject = @{}
        $testObject.name = $expectResult1
        $testObject.subObjectValue = @{}
        $testObject.subObjectValue.name = $expectResult2
        $testObject.subObjectValue.array = (0..63)
        $testObject.subObjectValue.stringarray = @("one","two")
        $result = $testObject | Format-Custom | Out-String
        $result | Should -Match $expectResult1
        $result | Should -Match $expectResult2
        $result | Should -Match "one"
        $result | Should -Match "two"
    }

	It "Format-Custom with Tree Object should work" {
		$expectedResult=@"
class MyContainer1
{
  data1 = data 1
  children =
    [
      class MyLeaf1
      {
        val1 = 10
        name = leaf 1
      }
      class MyLeaf2
      {
        val2 = 20
        name = leaf 2
      }
    ]

  name = container 1
}

class MyContainer2
{
  data2 = data 2
  children =
    [
      class MyLeaf1
      {
        val1 = 10
        name = leaf 1
      }
      class MyLeaf2
      {
        val2 = 20
        name = leaf 2
      }
    ]

  name = container 2
}

class MyContainer2
{
  data2 = data 2 deep
  children =
    [
      class MyContainer1
      {
        data1 = cx data
        children =
          [
            class MyLeaf1
            {
              val1 = 10
              name = leaf 1
            }
            class MyLeaf2
            {
              val2 = 20
              name = leaf 2
            }
          ]

        name = cx
      }
    ]

  name = container 2 deep
}

class MyContainer1
{
  data1 = cx data
  children =
    [
      class MyLeaf1
      {
        val1 = 10
        name = leaf 1
      }
      class MyLeaf2
      {
        val2 = 20
        name = leaf 2
      }
    ]

  name = cx
}
"@
		$leaf1 = New-Object MyLeaf1
		$leaf1.name = "leaf 1"
		$leaf1.val1 = 10

		$leaf2 = New-Object MyLeaf2
		$leaf2.name = "leaf 2"
		$leaf2.val2 = 20

		$c1 = New-Object MyContainer1
		$c1.name = "container 1"
		$c1.data1 = "data 1"
		$c1.children.Add($leaf1)
		$c1.children.Add($leaf2)

		$c2 = New-Object MyContainer2
		$c2.name = "container 2"
		$c2.data2 = "data 2"
		$c2.children.Add($leaf1)
		$c2.children.Add($leaf2)

		$cDeep = New-Object MyContainer2
		$cDeep.name = "container 2 deep"
		$cDeep.data2 = "data 2 deep"

		$cx= New-Object MyContainer1
		$cx.name = "cx"
		$cx.data1 = "cx data"
		$cx.children.Add($leaf1)
		$cx.children.Add($leaf2)
		$cDeep.children.Add($cx)

		$objectList = @($c1,$c2,$cDeep,$cx)
		$result = $objectList | Format-Custom | Out-String
		$result = $result -replace "[{} `n\r]",""
		$expectedResult = $expectedResult -replace "[{} `n\r]",""
		$result | Should -Be $expectedResult
	}

	It "Format-Custom with Empty Data Tree Object should work" {
		$expectedResult=@"
class MyContainer1
{
  data1 =
  children =
  name =
}
"@

		$c= New-Object MyContainer1
		$c.name = $null
		$c.data1 = $null
		$c.children = $null
		$objectList = @($c)

		$result = $objectList | Format-Custom | Out-String
		$result = $result -replace "[{} `n\r]",""
		$expectedResult = $expectedResult -replace "[{} `n\r]",""
		$result | Should -Be $expectedResult
	}

	It "Format-Custom with Back Pointers Tree Object should work" {
		$expectedResult=@"
class MyContainer1
{
  data1 =
  children =
    [
      class MyContainer1
      {
        data1 =
        children =
          [
            class MyContainer1
            {
              data1 =
              children =
                [
                  MyContainer1
                ]

              name = ROOT
            }
          ]

        name = ROOT
      }
    ]

  name = ROOT
}
"@

		$root= New-Object MyContainer1
		$root.name = "ROOT"
		$root.children.Add($root)
		$objectList = @($root)

		$result = $objectList | Format-Custom | Out-String
		$result = $result -replace "[{} `n\r]",""
		$expectedResult = $expectedResult -replace "[{} `n\r]",""
		$result | Should -Be $expectedResult
	}

	It "Format-Custom with Leaf Only Data should work" {
		$expectedResult=@"
class MyLeaf1
{
  val1 = 10
  name = leaf 1
}

class MyLeaf2
{
  val2 = 20
  name = leaf 2
}
"@

		$leaf1 = New-Object MyLeaf1
		$leaf1.name = "leaf 1"
		$leaf1.val1 = 10

		$leaf2 = New-Object MyLeaf2
		$leaf2.name = "leaf 2"
		$leaf2.val2 = 20

		$objectList = @($leaf1,$leaf2)
		$result = $objectList | Format-Custom | Out-String
		$result = $result -replace "[{} `n\r]",""
		$expectedResult = $expectedResult -replace "[{} `n\r]",""
		$result | Should -Be $expectedResult
	}

    It "Format-Custom should not lost data" {
      # See https://github.com/PowerShell/PowerShell/pull/11342 for more information
      $data = (Get-Help Out-Null).Examples
      $formattedData = $data | Format-Custom | Out-String
      $formattedData | Should -BeLike "*$($data.Example.title)*"
      $formattedData | Should -BeLike "*$($data.Example.code)*"
      $formattedData | Should -BeLike "*$($data.Example.remarks.Text)*"
    }
  }

Describe "Format-Custom with expression based EntrySelectedBy in a CustomControl" -Tags "CI" {
    BeforeAll {
        if ($null -ne $PSStyle) {
            $outputRendering = $PSStyle.OutputRendering
            $PSStyle.OutputRendering = 'plaintext'
        }

        $formatFilePath = Join-Path $TestDrive 'UpdateFormatDataTests.format.ps1xml'
        $xmlContent = @'
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
    <Controls>
        <Control>
            <Name>MyTestControl</Name>
            <CustomControl>
                <CustomEntries>
                    <CustomEntry>
                        <EntrySelectedBy>
                            <SelectionCondition>
                                <TypeName>MyTestObject</TypeName>
                                <ScriptBlock>$_.Name -eq 'SelectScriptBlock'</ScriptBlock>
                            </SelectionCondition>
                        </EntrySelectedBy>
                        <CustomItem>
                            <Text>Entry selected by ScriptBlock</Text>
                        </CustomItem>
                    </CustomEntry>
                    <CustomEntry>
                        <EntrySelectedBy>
                            <SelectionCondition>
                                <TypeName>MyTestObject</TypeName>
                                <PropertyName>SelectProperty</PropertyName>
                            </SelectionCondition>
                        </EntrySelectedBy>
                        <CustomItem>
                            <Text>Entry selected by property</Text>
                        </CustomItem>
                    </CustomEntry>
                    <CustomEntry>
                        <CustomItem>
                            <Text>Default was picked</Text>
                        </CustomItem>
                    </CustomEntry>
                </CustomEntries>
            </CustomControl>
        </Control>
    </Controls>
    <ViewDefinitions>
        <View>
            <Name>DefaultView</Name>
            <ViewSelectedBy>
                <TypeName>MyTestObject</TypeName>
            </ViewSelectedBy>
            <GroupBy>
                <PropertyName>Name</PropertyName>
                <CustomControlName>MyTestControl</CustomControlName>
            </GroupBy>
            <TableControl>
                <TableHeaders>
                    <TableColumnHeader />
                </TableHeaders>
                <TableRowEntries>
                    <TableRowEntry>
                        <TableColumnItems>
                            <TableColumnItem>
                                <PropertyName>Name</PropertyName>
                            </TableColumnItem>
                        </TableColumnItems>
                    </TableRowEntry>
                </TableRowEntries>
            </TableControl>
        </View>
    </ViewDefinitions>
</Configuration>
'@

        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 {
        if ($null -ne $PSStyle) {
            $PSStyle.OutputRendering = $outputRendering
        }

        $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', "^"

        $ps.Invoke() -replace '\r?\n', "^" | 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', "^"

        $ps.Invoke() -replace '\r?\n', "^" | Should -BeExactly $expectedOutput
        $ps.Streams.Error | Should -BeNullOrEmpty
    }

    Context 'ExcludeProperty parameter' {
        It 'Should exclude specified properties' {
            $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle' }
            $result = $obj | Format-Custom -ExcludeProperty Age | Out-String
            $result | Should -Match 'Name'
            $result | Should -Match 'City'
            $result | Should -Not -Match 'Age'
        }

        It 'Should work with wildcard patterns' {
            $obj = [pscustomobject]@{ Prop1 = 1; Prop2 = 2; Other = 3 }
            $result = $obj | Format-Custom -ExcludeProperty Prop* | Out-String
            $result | Should -Match 'Other'
            $result | Should -Not -Match 'Prop1'
            $result | Should -Not -Match 'Prop2'
        }

        It 'Should work without Property parameter (implies -Property *)' {
            $obj = [pscustomobject]@{ A = 1; B = 2; C = 3 }
            $result = $obj | Format-Custom -ExcludeProperty B | Out-String
            $result | Should -Match 'A\s*='
            $result | Should -Match 'C\s*='
            $result | Should -Not -Match 'B\s*='
        }

        It 'Should work with Property parameter' {
            $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle'; Country = 'USA' }
            $result = $obj | Format-Custom -Property Name, Age, City, Country -ExcludeProperty Age | Out-String
            $result | Should -Match 'Name'
            $result | Should -Match 'City'
            $result | Should -Match 'Country'
            $result | Should -Not -Match 'Age'
        }

        It 'Should handle multiple excluded properties' {
            $obj = [pscustomobject]@{ A = 1; B = 2; C = 3; D = 4 }
            $result = $obj | Format-Custom -ExcludeProperty B, D | Out-String
            $result | Should -Match 'A\s*='
            $result | Should -Match 'C\s*='
            $result | Should -Not -Match 'B\s*='
            $result | Should -Not -Match 'D\s*='
        }
    }
}