8000 Add Tests to Language by bingbing8 · Pull Request #2043 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Add Tests to Language #2043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 68 additions & 49 deletions test/powershell/Language/LanguageTestSupport.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -53,37 +53,6 @@ function position_message
}


#
# Run the new parser, check the errors against the expected errors
#
function Test-Error
{
[CmdletBinding()]
param([string]$src, [string[]]$expectedErrors, [int[]]$expectedOffsets)

Assert ($expectedErrors.Count -eq $expectedOffsets.Count) "Test case error"

$errors = Get-ParseResults -Src $src


if ($null -ne $errors)
{
Assert ($errors.Count -eq $expectedErrors.Count) "Expected $($expectedErrors.Count) errors, got $($errors.Count)"
for ($i = 0; $i -lt $errors.Count; ++$i)
{
$err = $errors[$i]
Assert ($expectedErrors[$i] -eq $err.ErrorId) ("Unexpected error: {0,-30}{1}" -f ("$($err.ErrorId):",
(position_message $err.Extent.StartScriptPosition)))
Assert ($expectedOffsets[$i] -eq $err.Extent.StartScriptPosition.Offset) `
"Expected position: $($expectedOffsets[$i]), got $($err.Extent.StartScriptPosition.Offset)"
}
}
else
{
Assert $false "Expected errors but didn't receive any."
}
}

#
# Pester friendly version of Test-Error
#
Expand All @@ -95,7 +64,9 @@ function ShouldBeParseError
[string[]]$expectedErrors,
[int[]]$expectedOffsets,
# This is a temporary solution after moving type creation from parse time to runtime
[switch]$SkipAndCheckRuntimeError
[switch]$SkipAndCheckRuntimeError,
# for test coverarage purpose, tests validate columnNumber or offset
[switch]$CheckColumnNumber
)

Context "Parse error expected: <<$src>>" {
Expand Down Expand Up @@ -129,7 +100,9 @@ function ShouldBeParseError
$errorId = $err.ErrorId
}
It "Error Id" { $errorId | Should Be $expectedErrors[$i] }
It "Error position" -Pending:$SkipAndCheckRuntimeError { $err.Extent.StartScriptPosition.Offset | Should Be $expectedOffsets[$i] }
$acutalPostion = $err.Extent.StartScriptPosition.Offset
if ( $CheckColumnNumber ) { $acutalPostion = $err.Extent.StartScriptPosition.ColumnNumber }
It "Error position" -Pending:$SkipAndCheckRuntimeError { $acutalPostion | Should Be $expectedOffsets[$i] }
}
}
}
Expand All @@ -149,30 +122,76 @@ function Flatten-Ast
function Test-ErrorStmt
{
param([string]$src, [string]$errorStmtExtent)

$ast = Get-ParseResults $src -Ast
$asts = @(Flatten-Ast $ast.EndBlock.Statements[0])

Assert ($asts[0] -is [System.Management.Automation.Language.ErrorStatementAst]) "Expected error statement"
Assert ($asts.Count -eq $args.Count + 1) "Incorrect number of nested asts"
Assert ($asts[0].Extent.Text -eq $errorStmtExtent) "Error statement expected <$errorStmtExtent>, got <$($asts[0].Extent.Text)>"
for ($i = 0; $i -lt $args.Count; ++$i)
{
Assert ($asts[$i + 1].Extent.Text -eq $args[$i]) "Nested ast incorrect: <$($asts[$i+1].Extent.Text)>, expected <$($args[$i])>"
$a = $args
Context "Error Statement expected: <<$src>>" {
$ast = Get-ParseResults $src -Ast
$asts = @(Flatten-Ast $ast.EndBlock.Statements[0])

It 'Type is ErrorStatementAst' { $asts[0].GetType() | Should Be System.Management.Automation.Language.ErrorStatementAst }
It "`$asts.count" { $asts.Count | Should Be ($a.Count + 1) }
It "`$asts[0].Extent.Text" { $asts[0].Extent.Text | Should Be $errorStmtExtent }
for ($i = 0; $i -lt $a.Count; ++$i)
{
It "`$asts[$($i + 1)].Extent.Text" { $asts[$i + 1].Extent.Text | Should Be $a[$i] }
}
}
}

function Test-Ast
{
param([string]$src)

$a = $args
$ast = Get-ParseResults $src -Ast
$asts = @(Flatten-Ast $ast)
Assert ($asts.Count -eq $args.Count) "Incorrect number of nested asts, got $($asts.Count), expected $($args.Count)"
for ($i = 0; $i -lt $args.Count; ++$i)
{
Assert ($asts[$i].Extent.Text -eq $args[$i]) "Nested ast incorrect: <$($asts[$i].Extent.Text)>, expected <$($args[$i])>"
Context "Ast Validation: <<$src>>" {
It "`$asts.count" { $asts.Count | Should Be $a.Count }
for ($i = 0; $i -lt $a.Count; ++$i)
{
It "`$asts[$i].Extent.Text" { $asts[$i].Extent.Text | Should Be $a[$i] }
}
}
}

Export-ModuleMember -Function Test-Error, Test-ErrorStmt, Test-Ast, ShouldBeParseError, Get-ParseResults, Get-RuntimeError
## ErrorStatement is special for SwitchStatement
function Test-ErrorStmtForSwitchFlag
{
param([string]$src, [string]$flagName)
$a = $args
$ast = Get-ParseResults $src -Ast
$ast = $ast.EndBlock.Statements[0]
Context "Ast Validation: <<$src>>" {
$ast.GetType() | Should Be System.Management.Automation.Language.ErrorStatementAst
$ast.Flags.ContainsKey($flagName) | Should be $true

$asts = @(Flatten-Ast $ast.Flags[$flagName].Item2)

$asts.Count | Should Be $a.Count
for ($i = 0; $i -lt $a.Count; ++$i)
{
$asts[$i].Extent.Text | Should Be $a[$i]
}
}
}

function ShouldBeErrorId
{
param([Parameter(ValueFromPipeline, Mandatory)]
[ScriptBlock]
$sb,

[Parameter(Mandatory, Position=0)]
[string]
$FullyQualifiedErrorId)

try
{
& $sb
throw "Unexpected"
}
catch
{
$_.FullyQualifiedErrorId | Should Be $FullyQualifiedErrorId
}
}

Export-ModuleMember -Function Test-ErrorStmt, Test-Ast, ShouldBeParseError, Get-ParseResults, Get-RuntimeError, Test-ErrorStmtForSwitchFlag, ShouldBeErrorId
Loading
0