diff --git a/test/powershell/Language/Parser/Parser.Tests.ps1 b/test/powershell/Language/Parser/Parser.Tests.ps1 index bfaf9a721..48cdb098d 100644 --- a/test/powershell/Language/Parser/Parser.Tests.ps1 +++ b/test/powershell/Language/Parser/Parser.Tests.ps1 @@ -1,76 +1,75 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + Describe "ParserTests (admin\monad\tests\monad\src\engine\core\ParserTests.cs)" -Tags "CI" { BeforeAll { - $functionDefinitionFile = Join-Path -Path $TestDrive -ChildPath "functionDefinition.ps1" - $functionDefinition = @' - function testcmd-parserbvt - { - [CmdletBinding()] - param ( - [Parameter(Position = 0)] - [string] $Property1 = "unset", + $functionDefinitionFile = Join-Path -Path $TestDrive -ChildPath "functionDefinition.ps1" + $functionDefinition = @' + function testcmd-parserbvt { + [CmdletBinding()] + param ( + [Parameter(Position = 0)] + [string] $Property1 = "unset", - [Parameter(Position = 1)] - [string] $Property2 = "unset", + [Parameter(Position = 1)] + [string] $Property2 = "unset", - [Parameter(Position = 2)] - [string] $Property3 = "unset", + [Parameter(Position = 2)] + [string] $Property3 = "unset", - [Parameter()] - [ValidateSet("default","array","object","nestedobject","struct","mshobject","nulltostring")] - [string]$ReturnType = "default" - ) + [Parameter()] + [ValidateSet("default","array","object","nestedobject","struct","mshobject","nulltostring")] + [string]$ReturnType = "default" + ) - BEGIN {} + BEGIN {} - PROCESS { - if ( ! $ReturnType ) { - $ReturnType = "default" - } + PROCESS { + if ( ! $ReturnType ) { + $ReturnType = "default" + } - switch ( $ReturnType ) - { - "default" { - $result = "$Property1;$Property2;$Property3" - break - } - "array" { - $result = 1,2,3 - break - } - "object" { - $result = new-object psobject - break - } - "nestedobject" { - $result = [pscustomobject]@{Name="John";Person=[pscustomobject]@{Name="John";Age=30}} - break - } - "struct" { - $result = [pscustomobject]@{Name="John";Age=30} - break - } - "mshobject" { - $result = new-object psobject - break - } - "nulltostring" { - $result = $null - break - } - default { - throw ([invalidoperationexception]::new("ReturnType parameter wasn't of any Expected value!")) - break - } - } - return $result - } + switch ( $ReturnType ) { + "default" { + $result = "$Property1;$Property2;$Property3" + break + } + "array" { + $result = 1,2,3 + break + } + "object" { + $result = new-object psobject + break + } + "nestedobject" { + $result = [pscustomobject]@{Name="John";Person=[pscustomobject]@{Name="John";Age=30}} + break + } + "struct" { + $result = [pscustomobject]@{Name="John";Age=30} + break + } + "mshobject" { + $result = new-object psobject + break + } + "nulltostring" { + $result = $null + break + } + default { + throw ([invalidoperationexception]::new("ReturnType parameter wasn't of any Expected value!")) + break + } + } + return $result + } - END {} - } + END {} + } '@ - $functionDefinition>$functionDefinitionFile + $functionDefinition>$functionDefinitionFile $PowerShell = [powershell]::Create() $PowerShell.AddScript(". $functionDefinitionFile").Invoke() @@ -85,63 +84,54 @@ Describe "ParserTests (admin\monad\tests\monad\src\engine\core\ParserTests.cs)" } } } - BeforeEach { - $testfile = Join-Path -Path $TestDrive -ChildPath "testfile.ps1" - $shellfile = Join-Path -Path $TestDrive -ChildPath "testfile.cmd" - $testfolder1 = Join-Path -Path $TestDrive -ChildPath "dir1" - $testfolder2 = Join-Path -Path $testfolder1 -ChildPath "dir2" - if(-Not(Test-Path $testfolder1)) - { - New-Item $testfolder1 -Type Directory - } - if(-Not(Test-Path $testfolder2)) - { - New-Item $testfolder2 -Type Directory - } - $testdirfile1 = Join-Path -Path $testfolder2 -ChildPath "testdirfile1.txt" - $testdirfile2 = Join-Path -Path $testfolder2 -ChildPath "testdirfile2.txt" - "">$testdirfile1 - "">$testdirfile2 - } + BeforeEach { + $testfile = Join-Path -Path $TestDrive -ChildPath "testfile.ps1" + $shellfile = Join-Path -Path $TestDrive -ChildPath "testfile.cmd" + $testfolder1 = Join-Path -Path $TestDrive -ChildPath "dir1" + $testfolder2 = Join-Path -Path $testfolder1 -ChildPath "dir2" + if (-Not(Test-Path $testfolder1)) { + New-Item $testfolder1 -Type Directory + } + if (-Not(Test-Path $testfolder2)) { + New-Item $testfolder2 -Type Directory + } + $testdirfile1 = Join-Path -Path $testfolder2 -ChildPath "testdirfile1.txt" + $testdirfile2 = Join-Path -Path $testfolder2 -ChildPath "testdirfile2.txt" + "">$testdirfile1 + "">$testdirfile2 + } AfterEach { - if(Test-Path $testfile) - { - Remove-Item $testfile - } - if(Test-Path $shellfile) - { - Remove-Item $shellfile - } - if(Test-Path $testdirfile1) - { - Remove-Item $testdirfile1 - } - if(Test-Path $testdirfile2) - { - Remove-Item $testdirfile2 - } - if(Test-Path $testfolder2) - { - Remove-Item $testfolder2 - } - if(Test-Path $testfolder1) - { - Remove-Item $testfolder1 - } + if (Test-Path $testfile) { + Remove-Item $testfile + } + if (Test-Path $shellfile) { + Remove-Item $shellfile + } + if (Test-Path $testdirfile1) { + Remove-Item $testdirfile1 + } + if (Test-Path $testdirfile2) { + Remove-Item $testdirfile2 + } + if (Test-Path $testfolder2) { + Remove-Item $testfolder2 + } + if (Test-Path $testfolder1) { + Remove-Item $testfolder1 + } } - AfterAll { - if(Test-Path $functionDefinitionFile) - { - Remove-Item $functionDefinitionFile - } - } + AfterAll { + if (Test-Path $functionDefinitionFile) { + Remove-Item $functionDefinitionFile + } + } - It "Throws a syntax error when parsing a string without a closing quote. (line 164)" { + It "Throws a syntax error when parsing a string without a closing quote. (line 164)" { { ExecuteCommand '"This is a test' } | Should -Throw -ErrorId "IncompleteParseException" - } + } - It "Throws an error if an open parenthesis is not closed (line 176)" { + It "Throws an error if an open parenthesis is not closed (line 176)" { { ExecuteCommand "(" } | Should -Throw -ErrorId "IncompleteParseException" } @@ -149,73 +139,73 @@ Describe "ParserTests (admin\monad\tests\monad\src\engine\core\ParserTests.cs)" { ExecuteCommand "| get-location" } | Should -Throw -ErrorId "ParseException" } - It "Throws an CommandNotFoundException exception if using a label in front of an if statement is not allowed. (line 225)"{ + It "Throws an CommandNotFoundException exception if using a label in front of an if statement is not allowed. (line 225)" { $PowerShell.Streams.Error.Clear() ExecuteCommand ":foo if ($x -eq 3) { 1 }" - $PowerShell.HadErrors | Should -BeTrue + $PowerShell.HadErrors | Should -BeTrue $PowerShell.Streams.Error.FullyQualifiedErrorId | Should -Be "CommandNotFoundException" } - It "Pipe an expression into a value expression. (line 237)" { + It "Pipe an expression into a value expression. (line 237)" { { ExecuteCommand "testcmd-parserbvt | 3" } | Should -Throw -ErrorId "ParseException" - { ExecuteCommand "testcmd-parserbvt | $(1 + 1)" } | Should -Throw -ErrorId "ParseException" + { ExecuteCommand "testcmd-parserbvt | $(1 + 1)" } | Should -Throw -ErrorId "ParseException" - { ExecuteCommand "testcmd-parserbvt | 'abc'" } | Should -Throw -ErrorId "ParseException" + { ExecuteCommand "testcmd-parserbvt | 'abc'" } | Should -Throw -ErrorId "ParseException" } It "Throws when you pipe into a value expression (line 238)" { - foreach($command in "1;2;3|3",'1;2;3|$(1+1)',"1;2;3|'abc'") { + foreach ($command in "1;2;3|3", '1;2;3|$(1+1)', "1;2;3|'abc'") { { ExecuteCommand $command } | Should -Throw -ErrorId "ParseException" } } - It "Throws an incomplete parse exception when a comma follows an expression (line 247)" { + It "Throws an incomplete parse exception when a comma follows an expression (line 247)" { { ExecuteCommand "(1+ 1)," } | Should -Throw -ErrorId "IncompleteParseException" } - It "Test that invoke has a higher precedence for a script than for an executable. (line 279)" { - "1">$testfile + It "Test that invoke has a higher precedence for a script than for an executable. (line 279)" { + "1">$testfile $result = ExecuteCommand ". $testfile" - $result | Should -Be 1 + $result | Should -Be 1 } - It "This test will check that a path is correctly interpreted when using '..' and '.' (line 364)" { + It "This test will check that a path is correctly interpreted when using '..' and '.' (line 364)" { $result = ExecuteCommand "set-location $TestDrive; get-childitem dir1\.\.\.\..\dir1\.\dir2\..\..\dir1\.\dir2" - $result.Count | Should -Be 2 - $result[0].Name | Should -BeExactly "testdirfile1.txt" - $result[1].Name | Should -BeExactly "testdirfile2.txt" + $result.Count | Should -Be 2 + $result[0].Name | Should -BeExactly "testdirfile1.txt" + $result[1].Name | Should -BeExactly "testdirfile2.txt" } - It "This test will check that the parser can handle a mix of forward slashes and back slashes in the path (line 417)" { + It "This test will check that the parser can handle a mix of forward slashes and back slashes in the path (line 417)" { $result = ExecuteCommand "get-childitem $TestDrive/dir1/./.\.\../dir1/.\dir2\../..\dir1\.\dir2" - $result.Count | Should -Be 2 - $result[0].Name | Should -BeExactly "testdirfile1.txt" - $result[1].Name | Should -BeExactly "testdirfile2.txt" + $result.Count | Should -Be 2 + $result[0].Name | Should -BeExactly "testdirfile1.txt" + $result[1].Name | Should -BeExactly "testdirfile2.txt" } - It "This test checks that the asterisk globs as expected. (line 545)" { + It "This test checks that the asterisk globs as expected. (line 545)" { $result = ExecuteCommand "get-childitem $TestDrive/dir1\dir2\*.txt" - $result.Count | Should -Be 2 - $result[0].Name | Should -BeExactly "testdirfile1.txt" - $result[1].Name | Should -BeExactly "testdirfile2.txt" + $result.Count | Should -Be 2 + $result[0].Name | Should -BeExactly "testdirfile1.txt" + $result[1].Name | Should -BeExactly "testdirfile2.txt" } - It "This test checks that we can use a range for globbing: [1-2] (line 557)" { + It "This test checks that we can use a range for globbing: [1-2] (line 557)" { $result = ExecuteCommand "get-childitem $TestDrive/dir1\dir2\testdirfile[1-2].txt" - $result.Count | Should -Be 2 - $result[0].Name | Should -BeExactly "testdirfile1.txt" - $result[1].Name | Should -BeExactly "testdirfile2.txt" + $result.Count | Should -Be 2 + $result[0].Name | Should -BeExactly "testdirfile1.txt" + $result[1].Name | Should -BeExactly "testdirfile2.txt" } - It "This test will check that escaping the $ sigil inside single quotes simply returns the $ character. (line 583)" { + It "This test will check that escaping the $ sigil inside single quotes simply returns the $ character. (line 583)" { $result = ExecuteCommand "'`$'" - $result | Should -BeExactly "`$" + $result | Should -BeExactly "`$" } - It "Test that escaping a space just returns that space. (line 593)" { + It "Test that escaping a space just returns that space. (line 593)" { $result = ExecuteCommand '"foo` bar"' - $result | Should -BeExactly "foo bar" + $result | Should -BeExactly "foo bar" } It "Test that escaping the character 'e' returns the ESC character (0x1b)." { @@ -279,7 +269,7 @@ foo``u{2195}abc } It "Test that a Unicode escape sequence can be used in a command name." { - function xyzzy`u{2195}($p) {$p} + function xyzzy`u{2195}($p) { $p } $cmd = Get-Command xyzzy`u{2195} -ErrorAction SilentlyContinue $cmd | Should -Not -BeNullOrEmpty $cmd.Name | Should -BeExactly 'xyzzy↕' @@ -299,162 +289,160 @@ foo``u{2195}abc } } - It "Test that escaping any character with no special meaning just returns that char. (line 602)" { + It "Test that escaping any character with no special meaning just returns that char. (line 602)" { $result = ExecuteCommand '"fo`obar"' - $result | Should -BeExactly "foobar" + $result | Should -BeExactly "foobar" } - Context "Test that we support all of the C# escape sequences. We use the ` instead of \. (line 613)" { - # the first two sequences are tricky, because we need to provide something to - # execute without causing an incomplete parse error - $tests = @{ sequence = "write-output ""`'"""; expected = ([char]39) }, - @{ sequence = 'write-output "`""'; expected = ([char]34) }, - # this is a string, of 2 "\", the initial backtick should essentially be ignored - @{ sequence = '"`\\"'; expected = '\\' }, - # control sequences - @{ sequence = '"`0"'; expected = ([char]0) }, # null - @{ sequence = '"`a"'; expected = ([char]7) }, - @{ sequence = '"`b"'; expected = ([char]8) }, # backspace - @{ sequence = '"`f"'; expected = ([char]12) }, # form - @{ sequence = '"`n"'; expected = ([char]10) }, # newline - @{ sequence = '"`r"'; expected = ([char]13) }, # return - @{ sequence = '"`t"'; expected = ([char]9) }, # tab - @{ sequence = '"`v"'; expected = ([char]11) } - It "C# escape sequence is supported using `` instead of \. (line 613)" -TestCases $tests { - param ( $sequence, $expected ) - $result = ExecuteCommand $sequence - $result | Should -BeExactly $expected - } + Context "Test that we support all of the C# escape sequences. We use the ` instead of \. (line 613)" { + # the first two sequences are tricky, because we need to provide something to + # execute without causing an incomplete parse error + $tests = @{ sequence = "write-output ""`'"""; expected = ([char]39) }, + @{ sequence = 'write-output "`""'; expected = ([char]34) }, + # this is a string, of 2 "\", the initial backtick should essentially be ignored + @{ sequence = '"`\\"'; expected = '\\' }, + # control sequences + @{ sequence = '"`0"'; expected = ([char]0) }, # null + @{ sequence = '"`a"'; expected = ([char]7) }, + @{ sequence = '"`b"'; expected = ([char]8) }, # backspace + @{ sequence = '"`f"'; expected = ([char]12) }, # form + @{ sequence = '"`n"'; expected = ([char]10) }, # newline + @{ sequence = '"`r"'; expected = ([char]13) }, # return + @{ sequence = '"`t"'; expected = ([char]9) }, # tab + @{ sequence = '"`v"'; expected = ([char]11) } + It "C# escape sequence is supported using `` instead of \. (line 613)" -TestCases $tests { + param ( $sequence, $expected ) + $result = ExecuteCommand $sequence + $result | Should -BeExactly $expected + } } - It "This test checks that array substitution occurs inside double quotes. (line 646)" { + It "This test checks that array substitution occurs inside double quotes. (line 646)" { $result = ExecuteCommand '$MyArray = "a","b";"Hello $MyArray"' - $result | Should -BeExactly "Hello a b" + $result | Should -BeExactly "Hello a b" } - It "This tests declaring an array in nested variable tables. (line 761)" { + It "This tests declaring an array in nested variable tables. (line 761)" { $result = ExecuteCommand "`$Variable:vtbl1:vtbl2:b=@(5,6);`$Variable:vtbl1:vtbl2:b" - $result.Count | Should -Be 2 - $result[0] | Should -Be 5 - $result[1] | Should -Be 6 + $result.Count | Should -Be 2 + $result[0] | Should -Be 5 + $result[1] | Should -Be 6 } - It "Test a simple multiple assignment. (line 773)" { + It "Test a simple multiple assignment. (line 773)" { $result = ExecuteCommand '$one,$two = 1,2,3; "One = $one"; "Two = $two"' - $result.Count | Should -Be 2 - $result[0] | Should -Be "One = 1" - $result[1] | Should -Be "Two = 2 3" + $result.Count | Should -Be 2 + $result[0] | Should -Be "One = 1" + $result[1] | Should -Be "Two = 2 3" } - It "Tests script, global and local scopes from a function inside a script. (line 824)" { - "`$var = 'script';function func { `$var; `$var = 'local'; `$local:var; `$script:var; `$global:var };func;`$var;">$testfile - ExecuteCommand "`$var = 'global'" + It "Tests script, global and local scopes from a function inside a script. (line 824)" { + "`$var = 'script';function func { `$var; `$var = 'local'; `$local:var; `$script:var; `$global:var };func;`$var;">$testfile + ExecuteCommand "`$var = 'global'" $result = ExecuteCommand "$testfile" - $result.Count | Should -Be 5 - $result[0] | Should -BeExactly "script" - $result[1] | Should -BeExactly "local" - $result[2] | Should -BeExactly "script" - $result[3] | Should -BeExactly "global" - $result[4] | Should -BeExactly "script" + $result.Count | Should -Be 5 + $result[0] | Should -BeExactly "script" + $result[1] | Should -BeExactly "local" + $result[2] | Should -BeExactly "script" + $result[3] | Should -BeExactly "global" + $result[4] | Should -BeExactly "script" } - It "Use break inside of a loop that is inside another loop. (line 945)" { - $commands = " while (1) { 1; while(1) { 2; break; 3; }; 4; break; 5; } ", - " for (;;) { 1; for(;;) { 2; break; 3; }; 4; break; 5; } ", - " foreach(`$a in 1,2,3) { 1; foreach( `$b in 1,2,3 ) { 2; break; 3; }; 4; break; 5; } " - $results = "1", "2", "4" + It "Use break inside of a loop that is inside another loop. (line 945)" { + $commands = " while (1) { 1; while(1) { 2; break; 3; }; 4; break; 5; } ", + " for (;;) { 1; for(;;) { 2; break; 3; }; 4; break; 5; } ", + " foreach(`$a in 1,2,3) { 1; foreach( `$b in 1,2,3 ) { 2; break; 3; }; 4; break; 5; } " + $results = "1", "2", "4" $i = 0 - for(;$i -lt $commands.Count;$i++) - { - $result = ExecuteCommand $commands[$i] - $result | Should -Be $results - } + for (; $i -lt $commands.Count; $i++) { + $result = ExecuteCommand $commands[$i] + $result | Should -Be $results + } } - It "Use break in two loops with same label. (line 967)" { - $commands = " :foo while (1) { 1; :foo while(1) { 2; break foo; 3; }; 4; break; 5; } ", - " :foo for (;;) { 1; :foo for(;;) { 2; break foo; 3; }; 4; break; 5; } ", - " :foo foreach(`$a in 1,2,3) { 1; :foo foreach( `$b in 1,2,3 ) { 2; break foo; 3; }; 4; break; 5; } " - $results = "1", "2", "4" + It "Use break in two loops with same label. (line 967)" { + $commands = " :foo while (1) { 1; :foo while(1) { 2; break foo; 3; }; 4; break; 5; } ", + " :foo for (;;) { 1; :foo for(;;) { 2; break foo; 3; }; 4; break; 5; } ", + " :foo foreach(`$a in 1,2,3) { 1; :foo foreach( `$b in 1,2,3 ) { 2; break foo; 3; }; 4; break; 5; } " + $results = "1", "2", "4" $i = 0 - for(;$i -lt $commands.Count;$i++) - { - $result = ExecuteCommand $commands[$i] - $result | Should -Be $results - } + for (; $i -lt $commands.Count; $i++) { + $result = ExecuteCommand $commands[$i] + $result | Should -Be $results + } } - It "Try continue inside of different loop statements. (line 1039)" { - $commands = " `$a = 0; while (`$a -lt 2) { `$a; `$a += 1; continue; 2; } ", - " for (`$a = 0;`$a -lt 2; `$a += 1) { 9; continue; 3; } ", - " foreach(`$a in 0,1) { `$a; continue; 2; } " - $result = ExecuteCommand $commands[0] - $result | Should -Be "0", "1" - $result = ExecuteCommand $commands[1] - $result | Should -Be "9", "9" - $result = ExecuteCommand $commands[2] - $result | Should -Be "0", "1" + It "Try continue inside of different loop statements. (line 1039)" { + $commands = " `$a = 0; while (`$a -lt 2) { `$a; `$a += 1; continue; 2; } ", + " for (`$a = 0;`$a -lt 2; `$a += 1) { 9; continue; 3; } ", + " foreach(`$a in 0,1) { `$a; continue; 2; } " + $result = ExecuteCommand $commands[0] + $result | Should -Be "0", "1" + $result = ExecuteCommand $commands[1] + $result | Should -Be "9", "9" + $result = ExecuteCommand $commands[2] + $result | Should -Be "0", "1" } - It "Use a label to continue an inner loop. (line 1059)" { - $commands = " `$x = 0; while (`$x -lt 1) { `$x += 1; `$x; `$a = 0; :foo while(`$a -lt 2) { `$a += 1; `$a; continue foo; 3; }; 4; continue; 5; } ", - " for (`$x = 0;`$x -lt 1;`$x += 1) { 1; :foo for(`$a = 0; `$a -lt 2; `$a += 1) { `$a; continue foo; 3; }; 4; continue; 5; } ", - " foreach(`$a in 1) { 1; :foo foreach( `$b in 1,2 ) { `$b; continue foo; 3; }; 4; continue; 5; } " - $result = ExecuteCommand $commands[0] - $result | Should -Be "1", "1", "2", "4" - $result = ExecuteCommand $commands[1] - $result | Should -Be "1", "0", "1", "4" - $result = ExecuteCommand $commands[2] - $result | Should -Be "1", "1", "2", "4" + It "Use a label to continue an inner loop. (line 1059)" { + $commands = " `$x = 0; while (`$x -lt 1) { `$x += 1; `$x; `$a = 0; :foo while(`$a -lt 2) { `$a += 1; `$a; continue foo; 3; }; 4; continue; 5; } ", + " for (`$x = 0;`$x -lt 1;`$x += 1) { 1; :foo for(`$a = 0; `$a -lt 2; `$a += 1) { `$a; continue foo; 3; }; 4; continue; 5; } ", + " foreach(`$a in 1) { 1; :foo foreach( `$b in 1,2 ) { `$b; continue foo; 3; }; 4; continue; 5; } " + $result = ExecuteCommand $commands[0] + $result | Should -Be "1", "1", "2", "4" + $result = ExecuteCommand $commands[1] + $result | Should -Be "1", "0", "1", "4" + $result = ExecuteCommand $commands[2] + $result | Should -Be "1", "1", "2", "4" } - It "Use continue with a label on a nested loop. (line 1059)" { - $commands = " `$x = 0; :foo while (`$x -lt 2) { `$x; `$x += 1; :bar while(1) { 2; continue foo; 3; }; 4; continue; 5; } ", - " :foo for (`$x = 0;`$x -lt 2;`$x += 1) { 1; :bar for(;;) { 2; continue foo; 3; }; 4; continue; 5; } ", - " :foo foreach(`$a in 1,2) { 1; :bar foreach( `$b in 1,2,3 ) { 2; continue foo; 3; }; 4; continue; 5; } " - $result = ExecuteCommand $commands[0] - $result | Should -Be "0", "2", "1", "2" - $result = ExecuteCommand $commands[1] - $result | Should -Be "1", "2", "1", "2" - $result = ExecuteCommand $commands[2] - $result | Should -Be "1", "2", "1", "2" + It "Use continue with a label on a nested loop. (line 1059)" { + $commands = " `$x = 0; :foo while (`$x -lt 2) { `$x; `$x += 1; :bar while(1) { 2; continue foo; 3; }; 4; continue; 5; } ", + " :foo for (`$x = 0;`$x -lt 2;`$x += 1) { 1; :bar for(;;) { 2; continue foo; 3; }; 4; continue; 5; } ", + " :foo foreach(`$a in 1,2) { 1; :bar foreach( `$b in 1,2,3 ) { 2; continue foo; 3; }; 4; continue; 5; } " + $result = ExecuteCommand $commands[0] + $result | Should -Be "0", "2", "1", "2" + $result = ExecuteCommand $commands[1] + $result | Should -Be "1", "2", "1", "2" + $result = ExecuteCommand $commands[2] + $result | Should -Be "1", "2", "1", "2" } - It "This test will check that it is a syntax error to use if without a code block. (line 1141)" { + It "This test will check that it is a syntax error to use if without a code block. (line 1141)" { { ExecuteCommand 'if ("true")' } | Should -Throw -ErrorId "IncompleteParseException" } - It "This test will check that it is a syntax error if the if condition is not complete. (line 1150)" { + It "This test will check that it is a syntax error if the if condition is not complete. (line 1150)" { { ExecuteCommand 'if (' } | Should -Throw -ErrorId "IncompleteParseException" } - It "This test will check that it is a syntax error to have an if condition without parentheses. (line 1159)" { + It "This test will check that it is a syntax error to have an if condition without parentheses. (line 1159)" { { ExecuteCommand 'if "true" { 1} else {2}' } | Should -Throw -ErrorId "ParseException" } - It "This test will check that the parser throws a syntax error when the if condition is missing the closing parentheses. (line 1168)" { + It "This test will check that the parser throws a syntax error when the if condition is missing the closing parentheses. (line 1168)" { { ExecuteCommand 'if ("true" { 1};' } | Should -Throw -ErrorId "ParseException" } - It "This test will check that it is a syntax error to have an else keyword without the corresponding code block. (line 1177)" { + It "This test will check that it is a syntax error to have an else keyword without the corresponding code block. (line 1177)" { { ExecuteCommand 'if ("true") {1} else' } | Should -Throw -ErrorId "IncompleteParseException" } - It "This test will check that the parser throws a syntax error when a foreach loop is not complete. (line 1238)" { + It "This test will check that the parser throws a syntax error when a foreach loop is not complete. (line 1238)" { { ExecuteCommand '$count=0;$files = $(get-childitem / -filter *.txt );foreach ($i ;$count' } | Should -Throw -ErrorId "ParseException" } - It "This test will check that the parser throws a syntax error if the foreach loop is not complete. (line 1248)" { + It "This test will check that the parser throws a syntax error if the foreach loop is not complete. (line 1248)" { { ExecuteCommand '$count=0;$files = $(get-childitem / -filter *.txt );foreach ($i in ;$count' } | Should -Throw -ErrorId "ParseException" } - It "This will test that the parser throws a syntax error if the foreach loop is missing a closing parentheses. (line 1258)" { + It "This will test that the parser throws a syntax error if the foreach loop is missing a closing parentheses. (line 1258)" { { ExecuteCommand '$count=0;$files = $(get-childitem / -filter *.txt );foreach ($i in $files ;$count' } | Should -Throw -ErrorId "ParseException" } - It "Test that if an exception is thrown from the try block it will be caught in the appropropriate catch block and that the finally block will run regardless of whether an exception is thrown. (line 1317)" { + It "Test that if an exception is thrown from the try block it will be caught in the appropropriate catch block and that the finally block will run regardless of whether an exception is thrown. (line 1317)" { $result = ExecuteCommand 'try { try { throw (new-object System.ArgumentException) } catch [System.DivideByZeroException] { } finally { "Finally" } } catch { $_.Exception.GetType().FullName }' - $result | Should -Be "Finally", "System.ArgumentException" + $result | Should -Be "Finally", "System.ArgumentException" } It "Test that a break statement in a finally block results in a ParseException" { @@ -465,46 +453,46 @@ foo``u{2195}abc ExecuteCommand 'try {"success"} finally {switch (1) {foo {break}}}' | Should -BeExactly 'success' } - It "Test that null can be passed to a method that expects a reference type. (line 1439)" { + It "Test that null can be passed to a method that expects a reference type. (line 1439)" { $result = ExecuteCommand '$test = "String";$test.CompareTo($())' - $result | Should -Be 1 + $result | Should -Be 1 } - It "Tests that command expansion operators can be used as a parameter to an object method. (line 1507)" { + It "Tests that command expansion operators can be used as a parameter to an object method. (line 1507)" { $result = ExecuteCommand '$test = "String";$test.SubString($("hello" | foreach-object { $_.length - 2 } ))' - $result | Should -Be "ing" + $result | Should -Be "ing" } - It "Test that & can be used as a parameter as long as it is quoted. (line 1606)" { + It "Test that & can be used as a parameter as long as it is quoted. (line 1606)" { $result = ExecuteCommand 'testcmd-parserbvt `&get-childitem' - $result | Should -Be "&get-childitem;unset;unset" - $result = ExecuteCommand 'testcmd-parserbvt `&*' - $result | Should -Be "&*;unset;unset" + $result | Should -Be "&get-childitem;unset;unset" + $result = ExecuteCommand 'testcmd-parserbvt `&*' + $result | Should -Be "&*;unset;unset" } - It "Run a command with parameters. (line 1621)" { + It "Run a command with parameters. (line 1621)" { $result = ExecuteCommand 'testcmd-parserBVT -Property1 set' - $result | Should -Be "set;unset;unset" + $result | Should -Be "set;unset;unset" } - It "Test that typing a number at the command line will return that number. (line 1630)" { + It "Test that typing a number at the command line will return that number. (line 1630)" { $result = ExecuteCommand '3' - $result | Should -Be "3" - $result.gettype() |should -Be ([int]) + $result | Should -Be "3" + $result.gettype() | should -Be ([int]) } - It "This test will check that an msh script can be run without invoking. (line 1641)" { + It "This test will check that an msh script can be run without invoking. (line 1641)" { "1">$testfile $result = ExecuteCommand ". $testfile" - $result | Should -Be 1 + $result | Should -Be 1 } - It "Test that an alias is resolved before a function. (line 1657)" { + It "Test that an alias is resolved before a function. (line 1657)" { $result = ExecuteCommand 'set-alias parserInvokeTest testcmd-parserBVT;function parserInvokeTest { 3 };parserInvokeTest' - $result | Should -Be "unset;unset;unset" + $result | Should -Be "unset;unset;unset" } - It "Test that functions are resolved before cmdlets. (line 1678)"{ + It "Test that functions are resolved before cmdlets. (line 1678)" { $result_cmdlet = $PowerShell.AddScript('function test-parserfunc { [CmdletBinding()] Param() PROCESS { "cmdlet" } };test-parserfunc').Invoke() $result_func = ExecuteCommand 'function test-parserfunc { "func" };test-parserfunc' $PowerShell.Commands.Clear() @@ -512,8 +500,8 @@ foo``u{2195}abc $result_func | Should -Be "func" } - It "Check that a command that uses shell execute can be run from the command line and that no exception is thrown. (line 1702)" { - if ( $IsLinux -or $IsMacOS ) { + It "Check that a command that uses shell execute can be run from the command line and that no exception is thrown. (line 1702)" { + if ( $IsLinux -or $IsMacOS ) { # because we execute on *nix based on executable bit, and the file name doesn't matter # so we can use the same filename as for windows, just make sure it's executable with chmod "#!/bin/sh`necho ""Hello World""" | out-file -encoding ASCII $shellfile @@ -525,31 +513,31 @@ foo``u{2195}abc { ExecuteCommand "$shellfile" } | Should -Not -Throw } - Context "Boolean Tests (starting at line 1723 to line 1772)" { + Context "Boolean Tests (starting at line 1723 to line 1772)" { $testData = @( @{ Script = '"False"'; Expected = $true } - @{ Script = 'if ("A") { $true } else { $false }'; Expected = $true } + @{ Script = 'if ("A") { $true } else { $false }'; Expected = $true } @{ Script = 'if (" ") { $true } else { $false }'; Expected = $true } @{ Script = 'if ("String with spaces") { $true } else { $false }'; Expected = $true } @{ Script = 'if ("DoubleQuoted") { $true } else { $false }'; Expected = $true } @{ Script = 'if ("StringWithNullVar$aEmbedded") { $true } else { $false }'; Expected = $true } - @{ Script = 'if (0) { $true } else { $false }'; Expected = $false } - @{ Script = '$a = $(0);if ($a) { $true } else { $false }'; Expected = $false } - @{ Script = '$obj = testcmd-parserBVT -ReturnType object;if ($obj) { $true } else { $false }'; Expected = $true } - ) + @{ Script = 'if (0) { $true } else { $false }'; Expected = $false } + @{ Script = '$a = $(0);if ($a) { $true } else { $false }'; Expected = $false } + @{ Script = '$obj = testcmd-parserBVT -ReturnType object;if ($obj) { $true } else { $false }'; Expected = $true } + ) It "