@@ -53,37 +53,6 @@ function position_message
5353}
5454
5555
56- #
57- # Run the new parser, check the errors against the expected errors
58- #
59- function Test-Error
60- {
61- [CmdletBinding ()]
62- param ([string ]$src , [string []]$expectedErrors , [int []]$expectedOffsets )
63-
64- Assert ($expectedErrors.Count -eq $expectedOffsets.Count ) " Test case error"
65-
66- $errors = Get-ParseResults - Src $src
67-
68-
69- if ($null -ne $errors )
70- {
71- Assert ($errors.Count -eq $expectedErrors.Count ) " Expected $ ( $expectedErrors.Count ) errors, got $ ( $errors.Count ) "
72- for ($i = 0 ; $i -lt $errors.Count ; ++ $i )
73- {
74- $err = $errors [$i ]
75- Assert ($expectedErrors [$i ] -eq $err.ErrorId ) (" Unexpected error: {0,-30}{1}" -f (" $ ( $err.ErrorId ) :" ,
76- (position_message $err.Extent.StartScriptPosition )))
77- Assert ($expectedOffsets [$i ] -eq $err.Extent.StartScriptPosition.Offset ) `
78- " Expected position: $ ( $expectedOffsets [$i ]) , got $ ( $err.Extent.StartScriptPosition.Offset ) "
79- }
80- }
81- else
82- {
83- Assert $false " Expected errors but didn't receive any."
84- }
85- }
86-
8756#
8857# Pester friendly version of Test-Error
8958#
@@ -95,7 +64,9 @@ function ShouldBeParseError
9564 [string []]$expectedErrors ,
9665 [int []]$expectedOffsets ,
9766 # This is a temporary solution after moving type creation from parse time to runtime
98- [switch ]$SkipAndCheckRuntimeError
67+ [switch ]$SkipAndCheckRuntimeError ,
68+ # for test coverarage purpose, tests validate columnNumber or offset
69+ [switch ]$CheckColumnNumber
9970 )
10071
10172 Context " Parse error expected: <<$src >>" {
@@ -129,7 +100,9 @@ function ShouldBeParseError
129100 $errorId = $err.ErrorId
130101 }
131102 It " Error Id" { $errorId | Should Be $expectedErrors [$i ] }
132- It " Error position" - Pending:$SkipAndCheckRuntimeError { $err.Extent.StartScriptPosition.Offset | Should Be $expectedOffsets [$i ] }
103+ $acutalPostion = $err.Extent.StartScriptPosition.Offset
104+ if ( $CheckColumnNumber ) { $acutalPostion = $err.Extent.StartScriptPosition.ColumnNumber }
105+ It " Error position" - Pending:$SkipAndCheckRuntimeError { $acutalPostion | Should Be $expectedOffsets [$i ] }
133106 }
134107 }
135108}
@@ -149,30 +122,76 @@ function Flatten-Ast
149122function Test-ErrorStmt
150123{
151124 param ([string ]$src , [string ]$errorStmtExtent )
152-
153- $ast = Get-ParseResults $src - Ast
154- $asts = @ (Flatten- Ast $ast.EndBlock.Statements [0 ])
155-
156- Assert ($asts [0 ] -is [System.Management.Automation.Language.ErrorStatementAst ]) " Expected error statement"
157- Assert ($asts.Count -eq $args.Count + 1 ) " Incorrect number of nested asts"
158- Assert ($asts [0 ].Extent.Text -eq $errorStmtExtent ) " Error statement expected <$errorStmtExtent >, got <$ ( $asts [0 ].Extent.Text) >"
159- for ($i = 0 ; $i -lt $args.Count ; ++ $i )
160- {
161- Assert ($asts [$i + 1 ].Extent.Text -eq $args [$i ]) " Nested ast incorrect: <$ ( $asts [$i + 1 ].Extent.Text) >, expected <$ ( $args [$i ]) >"
125+ $a = $args
126+ Context " Error Statement expected: <<$src >>" {
127+ $ast = Get-ParseResults $src - Ast
128+ $asts = @ (Flatten- Ast $ast.EndBlock.Statements [0 ])
129+
130+ It ' Type is ErrorStatementAst' { $asts [0 ].GetType() | Should Be System.Management.Automation.Language.ErrorStatementAst }
131+ It " `$ asts.count" { $asts.Count | Should Be ($a.Count + 1 ) }
132+ It " `$ asts[0].Extent.Text" { $asts [0 ].Extent.Text | Should Be $errorStmtExtent }
133+ for ($i = 0 ; $i -lt $a.Count ; ++ $i )
134+ {
135+ It " `$ asts[$ ( $i + 1 ) ].Extent.Text" { $asts [$i + 1 ].Extent.Text | Should Be $a [$i ] }
136+ }
162137 }
163138}
164139
165140function Test-Ast
166141{
167142 param ([string ]$src )
168-
143+ $a = $args
169144 $ast = Get-ParseResults $src - Ast
170145 $asts = @ (Flatten- Ast $ast )
171- Assert ($asts.Count -eq $args.Count ) " Incorrect number of nested asts, got $ ( $asts.Count ) , expected $ ( $args.Count ) "
172- for ($i = 0 ; $i -lt $args.Count ; ++ $i )
173- {
174- Assert ($asts [$i ].Extent.Text -eq $args [$i ]) " Nested ast incorrect: <$ ( $asts [$i ].Extent.Text) >, expected <$ ( $args [$i ]) >"
146+ Context " Ast Validation: <<$src >>" {
147+ It " `$ asts.count" { $asts.Count | Should Be $a.Count }
148+ for ($i = 0 ; $i -lt $a.Count ; ++ $i )
149+ {
150+ It " `$ asts[$i ].Extent.Text" { $asts [$i ].Extent.Text | Should Be $a [$i ] }
151+ }
175152 }
176153}
177154
178- Export-ModuleMember - Function Test-Error , Test-ErrorStmt , Test-Ast , ShouldBeParseError, Get-ParseResults , Get-RuntimeError
155+ # # ErrorStatement is special for SwitchStatement
156+ function Test-ErrorStmtForSwitchFlag
157+ {
158+ param ([string ]$src , [string ]$flagName )
159+ $a = $args
160+ $ast = Get-ParseResults $src - Ast
161+ $ast = $ast.EndBlock.Statements [0 ]
162+ Context " Ast Validation: <<$src >>" {
163+ $ast.GetType () | Should Be System.Management.Automation.Language.ErrorStatementAst
164+ $ast.Flags.ContainsKey ($flagName ) | Should be $true
165+
166+ $asts = @ (Flatten- Ast $ast.Flags [$flagName ].Item2)
167+
168+ $asts.Count | Should Be $a.Count
169+ for ($i = 0 ; $i -lt $a.Count ; ++ $i )
170+ {
171+ $asts [$i ].Extent.Text | Should Be $a [$i ]
172+ }
173+ }
174+ }
175+
176+ function ShouldBeErrorId
177+ {
178+ param ([Parameter (ValueFromPipeline , Mandatory )]
179+ [ScriptBlock ]
180+ $sb ,
181+
182+ [Parameter (Mandatory , Position = 0 )]
183+ [string ]
184+ $FullyQualifiedErrorId )
185+
186+ try
187+ {
188+ & $sb
189+ throw " Unexpected"
190+ }
191+ catch
192+ {
193+ $_.FullyQualifiedErrorId | Should Be $FullyQualifiedErrorId
194+ }
195+ }
196+
197+ Export-ModuleMember - Function Test-ErrorStmt , Test-Ast , ShouldBeParseError, Get-ParseResults , Get-RuntimeError , Test-ErrorStmtForSwitchFlag , ShouldBeErrorId
0 commit comments