File size: 5,847 Bytes
8c763fb | 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 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "ArrayExpression Tests" -Tags "CI" {
It "@([object[]](1,2,3)) should return a 3-element array of object[]" {
$result = @([object[]](1,2,3))
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 3
}
It "@([int[]](1,2,3)) should return a 3-element array of object[]" {
$result = @([int[]](1,2,3))
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 3
}
It "@([object[]]`$null) should return a 1-element(`$null) array of object[]" {
$result = @([object[]]$null)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 1
$result[0] | Should -BeNullOrEmpty
}
It "@([int[]]`$null) should return a 1-element(`$null) array of object[]" {
$result = @([int[]]$null)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 1
$result[0] | Should -BeNullOrEmpty
}
It "@([object[]][System.Management.Automation.Internal.AutomationNull]::Value) should return a 1-element(`$null) array of object[]" {
$result = @([object[]][System.Management.Automation.Internal.AutomationNull]::Value)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 1
$result[0] | Should -BeNullOrEmpty
}
It "@([int[]][System.Management.Automation.Internal.AutomationNull]::Value) should return a 1-element(`$null) array of object[]" {
$result = @([int[]][System.Management.Automation.Internal.AutomationNull]::Value)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 1
$result[0] | Should -BeNullOrEmpty
}
It "@(`$null) should return a 1-element(`$null) array of object[]" {
$result = @($null)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 1
$result[0] | Should -BeNullOrEmpty
}
It "@([System.Management.Automation.Internal.AutomationNull]::Value) should return an empty array of object[]" {
$result = @([System.Management.Automation.Internal.AutomationNull]::Value)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 0
}
It "@([object[]]`$a) should return a new array" {
$a = 1,2,3
$result = @([object[]]$a)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 3
}
It "@([int[]]`$a) should return a new array" {
$a = 1,2,3
$result = @([int[]]$a)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 3
}
It "@([System.Collections.Generic.List[object]]`$null) should return a 1-element(`$null) array of object[]" {
$result = @([System.Collections.Generic.List[object]]$null)
$result.GetType().FullName | Should -BeExactly "System.Object[]"
$result.Length | Should -Be 1
$result[0] | Should -BeNullOrEmpty
}
It "@([void](New-Item)) should create file" {
try {
$testFile = Join-Path $TestDrive (New-Guid)
$result = @([void](New-Item $testFile -ItemType File))
## file should be created
$testFile | Should -Exist
## the array should be empty
$result.Count | Should -Be 0
} finally {
Remove-Item $testFile -Force -ErrorAction SilentlyContinue
}
}
}
Describe "ArrayLiteral Tests" -Tags "CI" {
It "'[void](New-Item),2,3' should return a 3-element array and first element is AutomationNull" {
try {
$testFile = Join-Path $TestDrive (New-Guid)
$result = [void](New-Item $testFile -ItemType File), 2, 3
## file should be created
$testFile | Should -Exist
## the array should contain 3 items
$result.Count | Should -Be 3
## the first item should be AutomationNull
$result[0] | ForEach-Object { "YES" } | Should -BeNullOrEmpty
$result | Measure-Object | ForEach-Object -MemberName Count | Should -Be 2
} finally{
Remove-Item $testFile -Force -ErrorAction SilentlyContinue
}
}
It "'[void]1, [void](New-Item), [void]2' should return a 3-AutomationNull-element array" {
try {
$testFile = Join-Path $TestDrive (New-Guid)
$result = [void]1, [void](New-Item $testFile -ItemType File), [void]2
## file should be created
$testFile | Should -Exist
## the array should contain 3 items
$result.Count | Should -Be 3
## all items should be AutomationNull
$result | ForEach-Object { "YES" } | Should -BeNullOrEmpty
} finally {
Remove-Item $testFile -Force -ErrorAction SilentlyContinue
}
}
It "'[void]`$arraylist1.Add(1), `$arraylist2.Clear()' should return a 2-AutomationNull-element array" {
$arraylist1 = [System.Collections.ArrayList]::new()
$arraylist2 = [System.Collections.ArrayList]::new()
$arraylist2.Add(2) > $null
$arraylist2.Count | Should -Be 1
## first item is a non-void method call
## second item is a void method call
$result = [void]$arraylist1.Add(1), $arraylist2.Clear()
$result.Count | Should -Be 2
$result | ForEach-Object { "YES" } | Should -BeNullOrEmpty
$arraylist1.Count | Should -Be 1
$arraylist1[0] | Should -Be 1
$arraylist2.Count | Should -Be 0
}
}
|