2020-03-24 11:08:37 -07:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-02-13 09:23:53 -08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
Describe 'Testing of MyInvocation' -Tags "CI" {
|
2016-09-15 11:27:44 -07:00
|
|
|
It 'MyInvocation works in Function' {
|
|
|
|
|
|
|
|
|
|
function myfunc
|
|
|
|
|
{
|
2018-03-21 10:47:08 -07:00
|
|
|
$MyInvocation.Line.IndexOf("myfunc") | Should -BeGreaterThan -1
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{ . myfunc } | Should -Not -Throw
|
|
|
|
|
{& myfunc } | Should -Not -Throw
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
It 'MyInvocation works in Filter' {
|
|
|
|
|
|
|
|
|
|
filter myfilter
|
|
|
|
|
{
|
2018-03-21 10:47:08 -07:00
|
|
|
$MyInvocation.Line.IndexOf("myfilter") | Should -BeGreaterThan -1
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
|
|
|
|
|
2018-03-21 10:47:08 -07:00
|
|
|
{. myfilter } | Should -Not -Throw
|
|
|
|
|
{ & myfilter } | Should -Not -Throw
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
|
|
|
|
|
2023-03-07 00:01:42 +01:00
|
|
|
Context 'MyInvocation works with multi-line invocations' {
|
|
|
|
|
It 'MyInvocation.Statement works in & Script block' {
|
|
|
|
|
$a = & {
|
|
|
|
|
$MyInvocation.Statement
|
|
|
|
|
}
|
|
|
|
|
$a.IndexOf('& {
|
|
|
|
|
$MyInvocation.Statement
|
|
|
|
|
}') |Should -BeGreaterThan -1
|
|
|
|
|
}
|
|
|
|
|
It 'MyInvocation.Statement works in dot sourced Script block' {
|
|
|
|
|
$a = . {
|
|
|
|
|
$MyInvocation.Statement
|
|
|
|
|
}
|
|
|
|
|
$a.IndexOf('. {
|
|
|
|
|
$MyInvocation.Statement
|
|
|
|
|
}') |Should -BeGreaterThan -1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-16 13:31:14 -08:00
|
|
|
Context 'MyInvocation works in Script block' {
|
2016-09-15 11:27:44 -07:00
|
|
|
|
|
|
|
|
It 'MyInvocation works in dot sourced Script block' {
|
2017-01-16 13:31:14 -08:00
|
|
|
$a = . {$MyInvocation.Line}
|
2018-03-21 10:47:08 -07:00
|
|
|
$a.IndexOf('$a = . {$MyInvocation.Line}') | Should -BeGreaterThan -1
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
It 'MyInvocation works in & Script block2' {
|
2016-09-15 11:27:44 -07:00
|
|
|
$a = & {$MyInvocation.Line}
|
2018-03-21 10:47:08 -07:00
|
|
|
$a.IndexOf('$a = & {$MyInvocation.Line}') | Should -BeGreaterThan -1
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
|
|
|
|
|
2017-01-16 13:31:14 -08:00
|
|
|
It 'MyInvocation works when run Script file' {
|
2016-09-15 11:27:44 -07:00
|
|
|
$a = & {$MyInvocation.ScriptName}
|
2018-03-21 10:47:08 -07:00
|
|
|
$a.ToLower().IndexOf("myinvocation.tests.ps1") | Should -BeGreaterThan -1
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
2017-01-16 13:31:14 -08:00
|
|
|
|
2016-09-15 11:27:44 -07:00
|
|
|
It 'MyInvocation works when dot source Script file' {
|
|
|
|
|
$a = . {$MyInvocation.ScriptName}
|
2018-03-21 10:47:08 -07:00
|
|
|
$a.ToLower().IndexOf("myinvocation.tests.ps1") | Should -BeGreaterThan -1
|
2016-09-15 11:27:44 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|