Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Format-List.Tests.ps1309 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Format-List" -Tags "CI" {4    BeforeAll {5        $nl = [Environment]::NewLine6 7        if ($null -ne $PSStyle) {8            $outputRendering = $PSStyle.OutputRendering9            $PSStyle.OutputRendering = 'plaintext'10        }11    }12 13    AfterAll {14        if ($null -ne $PSStyle) {15            $PSStyle.OutputRendering = $outputRendering16        }17    }18 19    BeforeEach {20        $in = New-Object PSObject21        Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue22    }23 24    It "Should call format list without error" {25        { $in | Format-List } | Should -Not -BeNullOrEmpty26    }27 28    It "Should be able to call the alias" {29        { $in | fl } | Should -Not -BeNullOrEmpty30    }31 32    It "Should have the same output whether choosing alias or not" {33        $expected = $in | Format-List | Out-String34        $actual   = $in | fl          | Out-String35 36        $actual | Should -Be $expected37    }38 39    It "Should produce the expected output" {40        $expected = "${nl}testName : testValue${nl}${nl}"41        $in = New-Object PSObject42        Add-Member -InputObject $in -MemberType NoteProperty -Name testName -Value testValue43 44        $in | Format-List                  | Should -Not -BeNullOrEmpty45        $in | Format-List   | Out-String   | Should -Not -BeNullOrEmpty46        $in | Format-List   | Out-String   | Should -Be $expected47    }48 49    It "Should be able to call a property of the piped input" {50        # Tested on two input commands to verify functionality.51        Get-Command | Select-Object -First 1 | Format-List -Property Name | Should -Not -BeNullOrEmpty52 53        Get-Date | Format-List -Property DisplayName | Should -Not -BeNullOrEmpty54    }55 56    It "Should be able to display a list of props when separated by a comma" {57 58        (Get-Command | Select-Object -First 5 | Format-List -Property Name,Source | Out-String) -Split "${nl}" |59          Where-Object { $_.trim() -ne "" } |60          ForEach-Object { $_ | Should -Match "(Name)|(Source)" }61    }62 63    It "Should show the requested prop in every element" {64        # Testing each element of format-list, using a for-each loop since the Format-List is so opaque65        (Get-Command | Select-Object -First 5 | Format-List -Property Source | Out-String) -Split "${nl}" |66          Where-Object { $_.trim() -ne "" } |67          ForEach-Object { $_ | Should -Match "Source :" }68    }69 70    It "Should not show anything other than the requested props" {71        $output = Get-Command | Select-Object -First 5 | Format-List -Property Name | Out-String72 73        $output | Should -Not -Match "CommandType :"74        $output | Should -Not -Match "Source :"75        $output | Should -Not -Match "Module :"76    }77 78    It "Should be able to take input without piping objects to it" {79        $output = { Format-List -InputObject $in }80 81        $output | Should -Not -BeNullOrEmpty82 83    }84}85 86Describe "Format-List DRT basic functionality" -Tags "CI" {87    BeforeAll {88        if ($null -ne $PSStyle) {89            $outputRendering = $PSStyle.OutputRendering90            $PSStyle.OutputRendering = 'plaintext'91        }92    }93 94    AfterAll {95        if ($null -ne $PSStyle) {96            $PSStyle.OutputRendering = $outputRendering97        }98    }99 100    It "Format-List with array should work" {101        $al = (0..255)102        $info = @{}103        $info.array = $al104        $result = $info | Format-List | Out-String105        $result | Should -Match "Name  : array\s+Value : {0, 1, 2, 3`u{2026}}" # ellipsis106    }107 108	It "Format-List with No Objects for End-To-End should work"{109		$p = @{}110		$result = $p | Format-List -Force -Property "foo","bar" | Out-String111		$result.Trim() | Should -BeNullOrEmpty112	}113 114	It "Format-List with Null Objects for End-To-End should work"{115		$p = $null116		$result = $p | Format-List -Force -Property "foo","bar" | Out-String117		$result.Trim() | Should -BeNullOrEmpty118	}119 120	It "Format-List with single line string for End-To-End should work"{121		$p = "single line string"122		$result = $p | Format-List -Force -Property "foo","bar" | Out-String123		$result.Trim() | Should -BeNullOrEmpty124	}125 126	It "Format-List with multiple line string for End-To-End should work"{127		$p = "Line1\nLine2"128		$result = $p | Format-List -Force -Property "foo","bar" | Out-String129		$result.Trim() | Should -BeNullOrEmpty130	}131 132	It "Format-List with string sequence for End-To-End should work"{133		$p = "Line1","Line2"134		$result = $p | Format-List -Force -Property "foo","bar" | Out-String135		$result.Trim() | Should -BeNullOrEmpty136	}137 138    It "Format-List with complex object for End-To-End should work" {139        Add-Type -TypeDefinition "public enum MyDayOfWeek{Sun,Mon,Tue,Wed,Thu,Fri,Sat}"140        $eto = [MyDayOfWeek]::New()141        $info = @{}142        $info.intArray = 1,2,3,4143        $info.arrayList = "string1","string2"144        $info.enumerable = [MyDayOfWeek]$eto145        $info.enumerableTestObject = $eto146        $result = $info|Format-List|Out-String147        $result | Should -Match "Name  : enumerableTestObject"148        $result | Should -Match "Value : Sun"149        $result | Should -Match "Name  : arrayList"150        $result | Should -Match "Value : {string1, string2}"151        $result | Should -Match "Name  : enumerable"152        $result | Should -Match "Value : Sun"153        $result | Should -Match "Name  : intArray"154        $result | Should -Match "Value : {1, 2, 3, 4}"155    }156 157	It "Format-List with multiple same class object should work"{158		Add-Type -TypeDefinition "public class TestClass{public TestClass(string name,int length){Name = name;Length = length;}public string Name;public int Length;}"159		$testobjects = [TestClass]::New('name1',1),[TestClass]::New('name2',2),[TestClass]::New('name3',3)160		$result = $testobjects|Format-List|Out-String161		$result | Should -Match "Name   : name1"162		$result | Should -Match "Length : 1"163		$result | Should -Match "Name   : name2"164		$result | Should -Match "Length : 2"165		$result | Should -Match "Name   : name3"166		$result | Should -Match "Length : 3"167	}168 169	It "Format-List with multiple different class object should work"{170		Add-Type -TypeDefinition "public class TestClass{public TestClass(string name,int length){Name = name;Length = length;}public string Name;public int Length;}"171		Add-Type -TypeDefinition "public class TestClass2{public TestClass2(string name,string value,int length){Name = name;Value = value; Length = length;}public string Name;public string Value;public int Length;}"172		$testobjects = [TestClass]::New('name1',1),[TestClass2]::New('name2',"value2",2),[TestClass]::New('name3',3)173		$result = $testobjects|Format-List|Out-String174		$result | Should -Match "Name   : name1"175		$result | Should -Match "Length : 1"176		$result | Should -Match "Name   : name2"177		$result | Should -Match "Value  : value2"178		$result | Should -Match "Length : 2"179		$result | Should -Match "Name   : name3"180		$result | Should -Match "Length : 3"181    }182 183    It "Format-List with FileInfo should work" {184        $null = New-Item $testdrive\test.txt -ItemType File -Value "hello" -Force185        $result = Get-ChildItem -File $testdrive\test.txt | Format-List | Out-String186        $result | Should -Match "Name\s*:\s*test.txt"187        $result | Should -Match "Length\s*:\s*5"188    }189 190    It "Format-List should work with double byte wide chars" {191        $obj = [pscustomobject]@{192            "哇" = "62";193            "dbda" = "KM";194            "消息" = "千"195        }196 197        $expected = @"198 199哇   : 62200dbda : KM201消息 : 千202 203 204"@205        $expected = $expected -replace "`r`n", "`n"206 207        $actual = $obj | Format-List | Out-String208        $actual = $actual -replace "`r`n", "`n"209        $actual | Should -BeExactly $expected210    }211 212    It 'Float, double, and decimal should not be truncated to number of decimals from current culture' {213        $o = [PSCustomObject]@{214            double = [double]1234.56789215            float = [float]9876.543216            decimal = [decimal]4567.123456789217        }218 219        $expected = @"220 221double  : 1234.56789222float   : 9876.543223decimal : 4567.123456789224 225 226"@227 228        $actual = $o | Format-List | Out-String229        ($actual.Replace("`r`n", "`n")) | Should -BeExactly ($expected.Replace("`r`n", "`n"))230    }231}232 233Describe 'Format-List color tests' -Tag 'CI' {234    BeforeAll {235        $originalRendering = $PSStyle.OutputRendering236        $PSStyle.OutputRendering = 'Ansi'237        [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $true)238    }239 240    AfterAll {241        $PSStyle.OutputRendering = $originalRendering242        [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('ForceFormatListFixedLabelWidth', $false)243    }244 245    It 'Property names should use FormatAccent' {246        $out = ([pscustomobject]@{Short=1;LongLabelName=2} | fl | out-string).Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries)247        $out.Count | Should -Be 2248        $out[0] | Should -BeExactly "$($PSStyle.Formatting.FormatAccent)Short      : $($PSStyle.Reset)1" -Because ($out[0] | Format-Hex)249        $out[1] | Should -BeExactly "$($PSStyle.Formatting.FormatAccent)LongLabelN : $($PSStyle.Reset)2"250    }251 252    It 'VT decorations in a property value should not be leaked in list view' {253        $expected = @"254`e[32;1ma : `e[0mHello255`e[32;1mb : `e[0m`e[36mworld`e[0m256"@257        ## Format-List should append the 'reset' escape sequence to the value of 'b' property.258        $obj = [pscustomobject]@{ a = "Hello"; b = $PSStyle.Foreground.Cyan + "world"; }259        $obj | Format-List | Out-File "$TestDrive/outfile.txt"260 261        $output = Get-Content "$TestDrive/outfile.txt" -Raw262        $output.Trim().Replace("`r", "") | Should -BeExactly $expected.Replace("`r", "")263    }264 265    Context 'ExcludeProperty parameter' {266        It 'Should exclude specified properties' {267            $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle' }268            $result = $obj | Format-List -ExcludeProperty Age | Out-String269            $result | Should -Match 'Name'270            $result | Should -Match 'City'271            $result | Should -Not -Match 'Age'272        }273 274        It 'Should work with wildcard patterns' {275            $obj = [pscustomobject]@{ Prop1 = 1; Prop2 = 2; Other = 3 }276            $result = $obj | Format-List -ExcludeProperty Prop* | Out-String277            $result | Should -Match 'Other'278            $result | Should -Not -Match 'Prop1'279            $result | Should -Not -Match 'Prop2'280        }281 282        It 'Should work without Property parameter (implies -Property *)' {283            $obj = [pscustomobject]@{ A = 1; B = 2; C = 3 }284            $result = $obj | Format-List -ExcludeProperty B | Out-String285            $result | Should -Match 'A'286            $result | Should -Match 'C'287            $result | Should -Not -Match 'B'288        }289 290        It 'Should work with Property parameter' {291            $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle'; Country = 'USA' }292            $result = $obj | Format-List -Property Name, Age, City, Country -ExcludeProperty Age | Out-String293            $result | Should -Match 'Name'294            $result | Should -Match 'City'295            $result | Should -Match 'Country'296            $result | Should -Not -Match 'Age'297        }298 299        It 'Should handle multiple excluded properties' {300            $obj = [pscustomobject]@{ A = 1; B = 2; C = 3; D = 4 }301            $result = $obj | Format-List -ExcludeProperty B, D | Out-String302            $result | Should -Match 'A'303            $result | Should -Match 'C'304            $result | Should -Not -Match 'B'305            $result | Should -Not -Match 'D'306        }307    }308}309