Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Format-Wide.Tests.ps1161 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Format-Wide" -Tags "CI" {4    BeforeAll {5        1..2 | ForEach-Object { New-Item -Path ("TestDrive:\Testdir{0:00}" -f $_) -ItemType Directory }6        1..2 | ForEach-Object { New-Item -Path ("TestDrive:\TestFile{0:00}.txt" -f $_) -ItemType File }7        $pathList = Get-ChildItem $TestDrive8    }9 10    It "Should be able to specify the columns in output using the column switch" {11        { $pathList | Format-Wide -Column 3 } | Should -Not -Throw12    }13 14    It "Should be able to use the autosize switch" {15        { $pathList | Format-Wide -AutoSize } | Should -Not -Throw16        { $pathList | Format-Wide -AutoSize | Out-String } | Should -Not -Throw17    }18 19    It "Should be able to take inputobject instead of pipe" {20        { Format-Wide -InputObject $pathList } | Should -Not -Throw21    }22 23    It "Should be able to use the property switch" {24        { Format-Wide -InputObject $pathList -Property Mode } | Should -Not -Throw25    }26 27    It "Should throw an error when property switch and view switch are used together" {28        { Format-Wide -InputObject $pathList -Property CreationTime -View aoeu } |29            Should -Throw -ErrorId "FormatCannotSpecifyViewAndProperty,Microsoft.PowerShell.Commands.FormatWideCommand"30    }31 32    It "Should throw and suggest proper input when view is used with invalid input without the property switch" {33        { Format-Wide -InputObject $(Get-Process) -View aoeu } | Should -Throw34    }35}36 37Describe "Format-Wide DRT basic functionality" -Tags "CI" {38    It "Format-Wide with array should work" {39        $al = (0..255)40        $info = @{}41        $info.array = $al42        $result = $info | Format-Wide | Out-String43        $result | Should -Match "array"44    }45 46    It "Format-Wide with No Objects for End-To-End should work" {47        $p = @{}48        $result = $p | Format-Wide | Out-String49        $result | Should -BeNullOrEmpty50    }51 52    It "Format-Wide with Null Objects for End-To-End should work" {53        $p = $null54        $result = $p | Format-Wide | Out-String55        $result | Should -BeNullOrEmpty56    }57 58    It "Format-Wide with single line string for End-To-End should work" {59        $p = "single line string"60        $result = $p | Format-Wide | Out-String61        $result | Should -Match $p62    }63 64    It "Format-Wide with multiple line string for End-To-End should work" {65        $p = "Line1\nLine2"66        $result = $p | Format-Wide | Out-String67        $result | Should -Match "Line1"68        $result | Should -Match "Line2"69    }70 71    It "Format-Wide with string sequence for End-To-End should work" {72        $p = "Line1", "Line2"73        $result = $p |Format-Wide | Out-String74        $result | Should -Match "Line1"75        $result | Should -Match "Line2"76    }77 78    It "Format-Wide with complex object for End-To-End should work" {79        Add-Type -TypeDefinition "public enum MyDayOfWeek{Sun,Mon,Tue,Wed,Thu,Fri,Sat}"80        $eto = New-Object MyDayOfWeek81        $info = @{}82        $info.intArray = 1, 2, 3, 483        $info.arrayList = "string1", "string2"84        $info.enumerable = [MyDayOfWeek]$eto85        $info.enumerableTestObject = $eto86        $result = $info|Format-Wide|Out-String87        $result | Should -Match "intArray"88        $result | Should -Match "arrayList"89        $result | Should -Match "enumerable"90        $result | Should -Match "enumerableTestObject"91    }92 93    It "Format-Wide with multiple same class object with grouping should work" {94        Add-Type -TypeDefinition "public class TestGroupingClass{public TestGroupingClass(string name,int length){Name = name;Length = length;}public string Name;public int Length;public string GroupingKey;}"95        $testobject1 = [TestGroupingClass]::New('name1', 1)96        $testobject1.GroupingKey = "foo"97        $testobject2 = [TestGroupingClass]::New('name2', 2)98        $testobject1.GroupingKey = "bar"99        $testobject3 = [TestGroupingClass]::New('name3', 3)100        $testobject1.GroupingKey = "bar"101        $testobjects = @($testobject1, $testobject2, $testobject3)102        $result = $testobjects|Format-Wide -GroupBy GroupingKey|Out-String103        $result | Should -Match "GroupingKey: bar"104        $result | Should -Match "name1"105        $result | Should -Match " GroupingKey:"106        $result | Should -Match "name2\s+name3"107    }108 109    Context 'ExcludeProperty parameter' {110        It 'Should exclude specified property and display first remaining' {111            # PSCustomObject properties are in definition order: Name, Age, City112            $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle' }113            # Exclude Name, should display Age (first remaining)114            $result = $obj | Format-Wide -ExcludeProperty Name | Out-String115            $result | Should -Match '30'116            $result | Should -Not -Match 'Test'117        }118 119        It 'Should work with wildcard patterns' {120            # Properties: Prop1, Prop2, Other121            $obj = [pscustomobject]@{ Prop1 = 1; Prop2 = 2; Other = 3 }122            # Exclude Prop*, should display Other (only remaining)123            $result = $obj | Format-Wide -ExcludeProperty Prop* | Out-String124            $result | Should -Match '3'125            $result | Should -Not -Match '1'126            $result | Should -Not -Match '2'127        }128 129        It 'Should work without Property parameter (implies -Property *)' {130            # Properties: A, B, C131            $obj = [pscustomobject]@{ A = 1; B = 2; C = 3 }132            # Exclude B, C - should display A (first remaining)133            $result = $obj | Format-Wide -ExcludeProperty B, C | Out-String134            $result | Should -Match '1'135            $result | Should -Not -Match '2'136            $result | Should -Not -Match '3'137        }138 139        It 'Should display first remaining property after exclusion' {140            # Properties: Name, Age, City, Country141            $obj = [pscustomobject]@{ Name = 'Test'; Age = 30; City = 'Seattle'; Country = 'USA' }142            # Exclude Name and Age, should display City (first remaining)143            $result = $obj | Format-Wide -ExcludeProperty Name, Age | Out-String144            $result | Should -Match 'Seattle'145            $result | Should -Not -Match 'Test'146            $result | Should -Not -Match '30'147            # USA might appear but not in the wide field, or might not appear at all since only first property is shown148        }149 150        It 'Should handle multiple excluded properties' {151            # Properties: A, B, C, D152            $obj = [pscustomobject]@{ A = 1; B = 2; C = 3; D = 4 }153            # Exclude A and B, should display C (first remaining)154            $result = $obj | Format-Wide -ExcludeProperty A, B | Out-String155            $result | Should -Match '3'156            $result | Should -Not -Match '1'157            $result | Should -Not -Match '2'158        }159    }160}161