MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Hidden properties should not be returned by the 'FirstOrDefault' primitive" -Tag CI {5 BeforeAll {6 if ($null -ne $PSStyle) {7 $outputRendering = $PSStyle.OutputRendering8 $PSStyle.OutputRendering = 'plaintext'9 }10 }11 12 AfterAll {13 if ($null -ne $PSStyle) {14 $PSStyle.OutputRendering = $outputRendering15 }16 }17 18 It "Formatting for an object with no property/field should use 'ToString'" {19 class Empty {20 [String]ToString() { return 'MyString' }21 }22 23 $outstring = [Empty]::new() | Out-String24 $outstring.Trim() | Should -BeExactly "MyString"25 26 class Empty2 { }27 28 $outstring = [Empty2]::new() | Out-String29 $outstring.Trim() | Should -BeLike "*.Empty2"30 }31 32 It "Formatting for an object with only hidden property should use 'ToString'" {33 class Hidden {34 hidden $Param = 'Foo'35 [String]ToString() { return 'MyString' }36 }37 38 $outstring = [Hidden]::new() | Out-String39 $outstring.Trim() | Should -BeExactly "MyString"40 41 class Hidden2 {42 hidden $Param = 'Foo'43 }44 45 $outstring = [Hidden2]::new() | Out-String46 $outstring.Trim() | Should -BeLike "*.Hidden2"47 }48 49 It 'Formatting for an object with no-hidden property should use the default view' {50 class Params {51 $Param = 'Foo'52 [String]ToString() { return 'MyString' }53 }54 55 $outstring = [Params]::new() | Out-String56 $outstring.Trim() | Should -BeExactly "Param$([System.Environment]::NewLine)-----$([System.Environment]::NewLine)Foo"57 58 class Params2 {59 $Param = 'Foo'60 }61 62 $outstring = [Params2]::new() | Out-String63 $outstring.Trim() | Should -BeExactly "Param$([System.Environment]::NewLine)-----$([System.Environment]::NewLine)Foo"64 }65}66 67Describe "'Format-Table/List/Custom -Property' should not throw NullRef exception" -Tag CI {68 69 It "'<Command> -Property' requires value to be not null and not empty" -TestCases @(70 @{ Command = "Format-Table"; NameInErrorId = "FormatTableCommand" }71 @{ Command = "Format-List"; NameInErrorId = "FormatListCommand" }72 @{ Command = "Format-Custom"; NameInErrorId = "FormatCustomCommand" }73 ) {74 param($Command, $NameInErrorId)75 76 { Get-Process -Id $PID | & $Command -Property @() } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.$NameInErrorId"77 { Get-Process -Id $PID | & $Command -Property $null } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.$NameInErrorId"78 }79}80 