File size: 5,453 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 146 147 148 149 150 151 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe "Scripting.Followup.Tests" -Tags "CI" {
It "'[void](New-Item) | <Cmdlet>' should work and behave like passing AutomationNull to the pipe" {
try {
$testFile = Join-Path $TestDrive (New-Guid)
[void](New-Item $testFile -ItemType File) | ForEach-Object { "YES" } | Should -BeNullOrEmpty
## file should be created
$testFile | Should -Exist
} finally {
Remove-Item $testFile -Force -ErrorAction SilentlyContinue
}
}
## cast non-void method call to [void]
It "'[void]`$arraylist.Add(1) | <Cmdlet>' should work and behave like passing AutomationNull to the pipe" {
$arraylist = [System.Collections.ArrayList]::new()
[void]$arraylist.Add(1) | ForEach-Object { "YES" } | Should -BeNullOrEmpty
## $arraylist.Add(1) should be executed
$arraylist.Count | Should -Be 1
$arraylist[0] | Should -Be 1
}
## void method call
It "'`$arraylist2.Clear() | <Cmdlet>' should work and behave like passing AutomationNull to the pipe" {
$arraylist = [System.Collections.ArrayList]::new()
$arraylist.Add(1) > $null
$arraylist.Clear() | ForEach-Object { "YES" } | Should -BeNullOrEmpty
## $arraylist.Clear() should be executed
$arraylist.Count | Should -Be 0
}
It 'AutomationNull should be same as null for type conversion' {
$result = pwsh -noprofile -command {
class Example {
[string[]]$LogMessage
}
[Example] @{ LogMessage = [System.Management.Automation.Internal.AutomationNull]::Value }
}
$result.LogMessage | Should -BeNullOrEmpty
}
## fix https://github.com/PowerShell/PowerShell/issues/17165
It "([bool] `$var = 42) should return the varaible value" {
([bool]$var = 42).GetType().FullName | Should -Be "System.Boolean"
. { ([bool]$var = 42).GetType().FullName } | Should -Be "System.Boolean"
}
It "Setting property using 'ForEach' method should work on a scalar object" {
$obj = [pscustomobject] @{ p = 1 }
$obj.ForEach('p', 32) | Should -BeNullOrEmpty
$obj.p | Should -Be 32
}
It "Test the special type name 'ordered'" {
class ordered {
[hashtable] $Member
ordered([hashtable] $hash) {
$this.Member = $hash
}
}
## `<expr> -as\-is [ordered]` resolves 'ordered' as a normal type name.
$hash = @{ key = 2 }
$result = $hash -as [ordered]
$result.GetType().FullName | Should -BeExactly ([ordered].FullName)
$result -is [ordered] | Should -BeTrue
$result.Member['key'] | Should -Be 2
$result.Member.Count | Should -Be 1
## `[ordered]$hash` causes parsing error.
$err = $null
$null = [System.Management.Automation.Language.Parser]::ParseInput('[ordered]$hash', [ref]$null, [ref]$err)
$err.Count | Should -Be 1
$err[0].ErrorId | Should -BeExactly 'OrderedAttributeOnlyOnHashLiteralNode'
## `[ordered]@{ key = 1 }` creates 'OrderedDictionary'
$result = [ordered]@{ key = 1 }
$result | Should -BeOfType 'System.Collections.Specialized.OrderedDictionary'
}
It "'[NullString]::Value' should be treated as string type when resolving .NET method" {
$testType = 'NullStringTest' -as [type]
if (-not $testType) {
Add-Type -TypeDefinition @'
using System;
public class NullStringTest {
public static string Test(bool argument)
{
return "bool";
}
public static string Test(string argument)
{
return "string";
}
public static string Get<T>(T argument)
{
string ret = typeof(T).FullName;
if (argument is string[] array)
{
if (array[1] == null)
{
return ret + "; 2nd element is NULL";
}
}
return ret;
}
}
'@
}
[NullStringTest]::Test([NullString]::Value) | Should -BeExactly 'string'
[NullStringTest]::Get([NullString]::Value) | Should -BeExactly 'System.String'
[NullStringTest]::Get(@('foo', [NullString]::Value, 'bar')) | Should -BeExactly 'System.String[]; 2nd element is NULL'
}
It 'Non-default encoding should work in PowerShell' {
$powershell = Join-Path -Path $PSHOME -ChildPath "pwsh"
$result = & $powershell -noprofile -c '[System.Text.Encoding]::GetEncoding("IBM437").WebName'
$result | Should -BeExactly "ibm437"
}
It 'Return statement on the right side of an assignment should write the retrun value to outer pipe' {
function TestFunc1 {
## The return value are not assigned to the variable but should be written to the outer pipe.
$Global:mylhsvar = if ($true) { return "one" }
}
function TestFunc2 {
## The results from the sub-expression need to be preserved and flushed to the outer pipeline.
$("1";return "2")
}
try {
$Global:mylhsvar = $null
TestFunc1 | Should -BeExactly "one"
TestFunc2 | Should -Be @("1", "2")
$Global:mylhsvar | Should -Be $null
}
finally {
Remove-Variable -Name mylhsvar -Scope Global
}
}
}
|