From 6560623b3c5e0cb6475892424be998e33e481c13 Mon Sep 17 00:00:00 2001 From: Frode Flaten <3436158+fflaten@users.noreply.github.com> Date: Sat, 3 Aug 2024 22:01:54 +0000 Subject: [PATCH] Update tests --- tst/functions/Coverage.Tests.ps1 | 213 +++++++++++++++++++++++++++---- 1 file changed, 186 insertions(+), 27 deletions(-) diff --git a/tst/functions/Coverage.Tests.ps1 b/tst/functions/Coverage.Tests.ps1 index ae6b9edd3..42f2367db 100644 --- a/tst/functions/Coverage.Tests.ps1 +++ b/tst/functions/Coverage.Tests.ps1 @@ -17,6 +17,8 @@ InPesterModuleScope { $testScriptPath = Join-Path -Path $root -ChildPath TestScript.ps1 $testScript2Path = Join-Path -Path $root -ChildPath TestScript2.ps1 $testScript3Path = Join-Path -Path $rootSubFolder -ChildPath TestScript3.ps1 + $testScriptStatementsPath = Join-Path -Path $root -ChildPath TestScriptStatements.ps1 + $testScriptExitPath = Join-Path -Path $root -ChildPath TestScriptExit.ps1 $null = New-Item -Path $testScriptPath -ItemType File -ErrorAction SilentlyContinue @@ -42,7 +44,7 @@ InPesterModuleScope { function FunctionTwo { - 'I am function two. I never get called.' + 'I am function two. I never get called.' } FunctionOne @@ -119,6 +121,54 @@ InPesterModuleScope { -f ` 'other' +'@ + + $null = New-Item -Path $testScriptStatementsPath -ItemType File -ErrorAction SilentlyContinue + + Set-Content -Path $testScriptStatementsPath -Value @' + try { + try { + throw 'omg' + } + catch { + throw + } + } + catch { } + + switch (1,2,3) { + 1 { continue; } + 2 { break } + 3 { 'I was skipped because 2 called break in switch.' } + } + + :myBreakLabel foreach ($i in 1..100) { + foreach ($o in 1) { + break myBreakLabel + } + 'I was skipped by a labeled break.' + } + + :myLoopLabel foreach ($i in 1) { + foreach ($o in 1..100) { + continue myLoopLabel + } + 'I was skipped by a labeled contiune.' + } + + # These should not be included in code coverage + & { return } + & { return 123 } + + # will exit the script + exit +'@ + + $null = New-Item -Path $testScriptExitPath -ItemType File -ErrorAction SilentlyContinue + + Set-Content -Path $testScriptExitPath -Value @' + # will exit the script, so keep in own file + exit 123 '@ } @@ -129,14 +179,16 @@ InPesterModuleScope { BeforeAll { # TODO: renaming, breakpoints mean "code point of interests" in most cases here, not actual breakpoints # Path deliberately duplicated to make sure the code doesn't produce multiple breakpoints for the same commands - $breakpoints = Enter-CoverageAnalysis -CodeCoverage $testScriptPath, $testScriptPath, $testScript2Path, $testScript3Path -UseBreakpoints $UseBreakpoints + $breakpoints = Enter-CoverageAnalysis -CodeCoverage $testScriptPath, $testScriptPath, $testScript2Path, $testScript3Path, $testScriptStatementsPath, $testScriptExitPath -UseBreakpoints $UseBreakpoints - @($breakpoints).Count | Should -Be 18 -Because 'it has the proper number of breakpoints defined' + @($breakpoints).Count | Should -Be 39 -Because 'it has the proper number of breakpoints defined' $sb = { $null = & $testScriptPath $null = & $testScript2Path $null = & $testScript3Path + $null = & $testScriptStatementsPath + $null = & $testScriptExitPath } if ($UseBreakpoints) { @@ -153,29 +205,32 @@ InPesterModuleScope { } It 'Reports the proper number of executed commands' { - $coverageReport.NumberOfCommandsExecuted | Should -Be 15 + $coverageReport.NumberOfCommandsExecuted | Should -Be 33 } It 'Reports the proper number of analyzed commands' { - $coverageReport.NumberOfCommandsAnalyzed | Should -Be 18 + $coverageReport.NumberOfCommandsAnalyzed | Should -Be 39 } It 'Reports the proper number of analyzed files' { - $coverageReport.NumberOfFilesAnalyzed | Should -Be 3 + $coverageReport.NumberOfFilesAnalyzed | Should -Be 5 } It 'Reports the proper number of missed commands' { - $coverageReport.MissedCommands.Count | Should -Be 3 + $coverageReport.MissedCommands.Count | Should -Be 6 } It 'Reports the correct missed command' { $coverageReport.MissedCommands[0].Command | Should -Be "'I cannot get called.'" - $coverageReport.MissedCommands[1].Command | Should -Be "'I am function two. I never get called.'" + $coverageReport.MissedCommands[1].Command | Should -Be "'I am function two. I never get called.'" $coverageReport.MissedCommands[2].Command | Should -Be "'I am method two. I never get called.'" + $coverageReport.MissedCommands[3].Command | Should -Be "'I was skipped because 2 called break in switch.'" + $coverageReport.MissedCommands[4].Command | Should -Be "'I was skipped by a labeled break.'" + $coverageReport.MissedCommands[5].Command | Should -Be "'I was skipped by a labeled contiune.'" } It 'Reports the proper number of hit commands' { - $coverageReport.HitCommands.Count | Should -Be 15 + $coverageReport.HitCommands.Count | Should -Be 33 } It 'Reports the correct hit command' { @@ -268,6 +323,28 @@ InPesterModuleScope { + + + + + + + + + + + + + + + + + + + + + + @@ -295,10 +372,40 @@ InPesterModuleScope { - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -324,10 +431,10 @@ InPesterModuleScope { - - - - + + + + ') } @@ -401,6 +508,28 @@ InPesterModuleScope { + + + + + + + + + + + + + + + + + + + + + + @@ -428,10 +557,40 @@ InPesterModuleScope { - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -457,10 +616,10 @@ InPesterModuleScope { - - - - + + + + ') } @@ -523,7 +682,7 @@ InPesterModuleScope { } It 'Reports the correct missed command' { - $coverageReport.MissedCommands[0].Command | Should -Be "'I am function two. I never get called.'" + $coverageReport.MissedCommands[0].Command | Should -Be "'I am function two. I never get called.'" } It 'Reports the proper number of hit commands' { @@ -674,7 +833,7 @@ InPesterModuleScope { It 'Reports the correct missed command' { $coverageReport.MissedCommands[0].Command | Should -Be "'I cannot get called.'" - $coverageReport.MissedCommands[1].Command | Should -Be "'I am function two. I never get called.'" + $coverageReport.MissedCommands[1].Command | Should -Be "'I am function two. I never get called.'" } It 'Reports the proper number of hit commands' {