File size: 4,208 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 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe 'Misc Test' -Tags "CI" {
Context 'Where' {
class C1 {
[int[]] $Wheels = @(1,2,3);
[string] Foo() {
return (1..10).Where({ $PSItem -in $this.Wheels; }) -join ';'
}
[string] Bar()
{
return (1..10 | Where-Object { $PSItem -in $this.Wheels; }) -join ';'
}
}
It 'Invoke Where' {
[C1]::new().Foo() | Should -Be "1;2;3"
}
It 'Pipe to where' {
[C1]::new().Bar() | Should -Be "1;2;3"
}
}
Context 'ForEach' {
class C1 {
[int[]] $Wheels = @(1,2,3);
[string] Foo() {
$ret=""
Foreach($PSItem in $this.Wheels) { $ret +="$PSItem;"}
return $ret
}
[string] Bar()
{
$ret = ""
$this.Wheels | ForEach-Object { $ret += "$_;" }
return $ret
}
}
It 'Invoke Foreach' {
[C1]::new().Foo() | Should -Be "1;2;3;"
}
It 'Pipe to Foreach' {
[C1]::new().Bar() | Should -Be "1;2;3;"
}
}
Context 'Class instantiation' {
Class C1 {
[string] Foo() {
return (Get-TestText)
}
}
BeforeAll {
$ExpectedTextFromBoundInstance = "Class C1 was defined in this Runspace"
$ExpectedTextFromUnboundInstance = "New Runspace without class C1 defined"
## Define 'Get-TestText' in the current Runspace
function Get-TestText { return $ExpectedTextFromBoundInstance }
$NewRunspaceFunctionDefinitions = @"
## Define 'Get-TestText' in the new Runspace
function Get-TestText { return '$ExpectedTextFromUnboundInstance' }
## Define the function to create an instance of the given type using the default constructor
function New-UnboundInstance([Type]`$type) { `$type::new() }
## Define the function to call 'Foo()' on the given C1 instance, and return the result
function Run-Foo(`$C1Instance) { `$C1Instance.Foo() }
"@
## Create a new Runspace and define helper functions in it
$powershell = [powershell]::Create()
$powershell.AddScript($NewRunspaceFunctionDefinitions).Invoke() > $null
$powershell.Commands.Clear()
function InstantiateInNewRunspace([Type]$type) {
try {
$result = $powershell.AddCommand("New-UnboundInstance").AddParameter("type", $type).Invoke()
$result.Count | Should -Be 1
return $result[0]
} finally {
$powershell.Commands.Clear()
}
}
function RunFooInNewRunspace($instance) {
try {
$result = $powershell.AddCommand("Run-Foo").AddParameter("C1Instance", $instance).Invoke()
$result.Count | Should -Be 1
return $result[0]
} finally {
$powershell.Commands.Clear()
}
}
}
AfterAll {
$powershell.Dispose()
}
It "Create instance that is bound to a SessionState" {
$instance = [C1]::new()
## For a bound class instance, the execution of an instance method is
## done in the Runspace/SessionState the instance is bound to.
$instance.Foo() | Should -BeExactly $ExpectedTextFromBoundInstance
RunFooInNewRunspace $instance | Should -BeExactly $ExpectedTextFromBoundInstance
}
It "Create instance that is NOT bound to a SessionState" {
$instance = InstantiateInNewRunspace ([C1])
## For an unbound class instance, the execution of an instance method is done in
## the Runspace/SessionState where the call to the instance method is made.
$instance.Foo() | Should -BeExactly $ExpectedTextFromBoundInstance
RunFooInNewRunspace $instance | Should -BeExactly $ExpectedTextFromUnboundInstance
}
}
}
|