Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fflaten committed Aug 3, 2024
1 parent 5fec70c commit 6560623
Showing 1 changed file with 186 additions and 27 deletions.
213 changes: 186 additions & 27 deletions tst/functions/Coverage.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -42,7 +44,7 @@ InPesterModuleScope {
function FunctionTwo
{
'I am function two. I never get called.'
'I am function two. I never get called.'
}
FunctionOne
Expand Down Expand Up @@ -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
'@
}

Expand All @@ -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) {
Expand All @@ -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' {
Expand Down Expand Up @@ -268,6 +323,28 @@ InPesterModuleScope {
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</class>
<class name="CommonRoot/TestScriptExit" sourcefilename="TestScriptExit.ps1">
<method name="&lt;script&gt;" desc="()" line="2">
<counter type="INSTRUCTION" missed="0" covered="2" />
<counter type="LINE" missed="0" covered="1" />
<counter type="METHOD" missed="0" covered="1" />
</method>
<counter type="INSTRUCTION" missed="0" covered="2" />
<counter type="LINE" missed="0" covered="1" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</class>
<class name="CommonRoot/TestScriptStatements" sourcefilename="TestScriptStatements.ps1">
<method name="&lt;script&gt;" desc="()" line="3">
<counter type="INSTRUCTION" missed="3" covered="16" />
<counter type="LINE" missed="3" covered="14" />
<counter type="METHOD" missed="0" covered="1" />
</method>
<counter type="INSTRUCTION" missed="3" covered="16" />
<counter type="LINE" missed="3" covered="14" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</class>
<sourcefile name="TestScript.ps1">
<line nr="5" mi="0" ci="1" mb="0" cb="0" />
<line nr="6" mi="0" ci="1" mb="0" cb="0" />
Expand Down Expand Up @@ -295,10 +372,40 @@ InPesterModuleScope {
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</sourcefile>
<counter type="INSTRUCTION" missed="3" covered="14" />
<counter type="LINE" missed="2" covered="13" />
<counter type="METHOD" missed="2" covered="6" />
<counter type="CLASS" missed="0" covered="2" />
<sourcefile name="TestScriptExit.ps1">
<line nr="2" mi="0" ci="2" mb="0" cb="0" />
<counter type="INSTRUCTION" missed="0" covered="2" />
<counter type="LINE" missed="0" covered="1" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</sourcefile>
<sourcefile name="TestScriptStatements.ps1">
<line nr="3" mi="0" ci="2" mb="0" cb="0" />
<line nr="6" mi="0" ci="1" mb="0" cb="0" />
<line nr="11" mi="0" ci="1" mb="0" cb="0" />
<line nr="12" mi="0" ci="1" mb="0" cb="0" />
<line nr="13" mi="0" ci="1" mb="0" cb="0" />
<line nr="14" mi="1" ci="0" mb="0" cb="0" />
<line nr="17" mi="0" ci="1" mb="0" cb="0" />
<line nr="18" mi="0" ci="1" mb="0" cb="0" />
<line nr="19" mi="0" ci="1" mb="0" cb="0" />
<line nr="21" mi="1" ci="0" mb="0" cb="0" />
<line nr="24" mi="0" ci="1" mb="0" cb="0" />
<line nr="25" mi="0" ci="1" mb="0" cb="0" />
<line nr="26" mi="0" ci="1" mb="0" cb="0" />
<line nr="28" mi="1" ci="0" mb="0" cb="0" />
<line nr="32" mi="0" ci="1" mb="0" cb="0" />
<line nr="33" mi="0" ci="2" mb="0" cb="0" />
<line nr="36" mi="0" ci="1" mb="0" cb="0" />
<counter type="INSTRUCTION" missed="3" covered="16" />
<counter type="LINE" missed="3" covered="14" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</sourcefile>
<counter type="INSTRUCTION" missed="6" covered="32" />
<counter type="LINE" missed="5" covered="28" />
<counter type="METHOD" missed="2" covered="8" />
<counter type="CLASS" missed="0" covered="4" />
</package>
<package name="CommonRoot/TestSubFolder">
<class name="CommonRoot/TestSubFolder/TestScript3" sourcefilename="TestSubFolder/TestScript3.ps1">
Expand All @@ -324,10 +431,10 @@ InPesterModuleScope {
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</package>
<counter type="INSTRUCTION" missed="3" covered="15" />
<counter type="LINE" missed="2" covered="14" />
<counter type="METHOD" missed="2" covered="7" />
<counter type="CLASS" missed="0" covered="3" />
<counter type="INSTRUCTION" missed="6" covered="33" />
<counter type="LINE" missed="5" covered="29" />
<counter type="METHOD" missed="2" covered="9" />
<counter type="CLASS" missed="0" covered="5" />
</report>
')
}
Expand Down Expand Up @@ -401,6 +508,28 @@ InPesterModuleScope {
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</class>
<class name="TestScriptExit" sourcefilename="TestScriptExit.ps1">
<method name="&lt;script&gt;" desc="()" line="2">
<counter type="INSTRUCTION" missed="0" covered="2" />
<counter type="LINE" missed="0" covered="1" />
<counter type="METHOD" missed="0" covered="1" />
</method>
<counter type="INSTRUCTION" missed="0" covered="2" />
<counter type="LINE" missed="0" covered="1" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</class>
<class name="TestScriptStatements" sourcefilename="TestScriptStatements.ps1">
<method name="&lt;script&gt;" desc="()" line="3">
<counter type="INSTRUCTION" missed="3" covered="16" />
<counter type="LINE" missed="3" covered="14" />
<counter type="METHOD" missed="0" covered="1" />
</method>
<counter type="INSTRUCTION" missed="3" covered="16" />
<counter type="LINE" missed="3" covered="14" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</class>
<sourcefile name="TestScript.ps1">
<line nr="5" mi="0" ci="1" mb="0" cb="0" />
<line nr="6" mi="0" ci="1" mb="0" cb="0" />
Expand Down Expand Up @@ -428,10 +557,40 @@ InPesterModuleScope {
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</sourcefile>
<counter type="INSTRUCTION" missed="3" covered="14" />
<counter type="LINE" missed="2" covered="13" />
<counter type="METHOD" missed="2" covered="6" />
<counter type="CLASS" missed="0" covered="2" />
<sourcefile name="TestScriptExit.ps1">
<line nr="2" mi="0" ci="2" mb="0" cb="0" />
<counter type="INSTRUCTION" missed="0" covered="2" />
<counter type="LINE" missed="0" covered="1" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</sourcefile>
<sourcefile name="TestScriptStatements.ps1">
<line nr="3" mi="0" ci="2" mb="0" cb="0" />
<line nr="6" mi="0" ci="1" mb="0" cb="0" />
<line nr="11" mi="0" ci="1" mb="0" cb="0" />
<line nr="12" mi="0" ci="1" mb="0" cb="0" />
<line nr="13" mi="0" ci="1" mb="0" cb="0" />
<line nr="14" mi="1" ci="0" mb="0" cb="0" />
<line nr="17" mi="0" ci="1" mb="0" cb="0" />
<line nr="18" mi="0" ci="1" mb="0" cb="0" />
<line nr="19" mi="0" ci="1" mb="0" cb="0" />
<line nr="21" mi="1" ci="0" mb="0" cb="0" />
<line nr="24" mi="0" ci="1" mb="0" cb="0" />
<line nr="25" mi="0" ci="1" mb="0" cb="0" />
<line nr="26" mi="0" ci="1" mb="0" cb="0" />
<line nr="28" mi="1" ci="0" mb="0" cb="0" />
<line nr="32" mi="0" ci="1" mb="0" cb="0" />
<line nr="33" mi="0" ci="2" mb="0" cb="0" />
<line nr="36" mi="0" ci="1" mb="0" cb="0" />
<counter type="INSTRUCTION" missed="3" covered="16" />
<counter type="LINE" missed="3" covered="14" />
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</sourcefile>
<counter type="INSTRUCTION" missed="6" covered="32" />
<counter type="LINE" missed="5" covered="28" />
<counter type="METHOD" missed="2" covered="8" />
<counter type="CLASS" missed="0" covered="4" />
</package>
<package name="TestSubFolder">
<class name="TestSubFolder/TestScript3" sourcefilename="TestScript3.ps1">
Expand All @@ -457,10 +616,10 @@ InPesterModuleScope {
<counter type="METHOD" missed="0" covered="1" />
<counter type="CLASS" missed="0" covered="1" />
</package>
<counter type="INSTRUCTION" missed="3" covered="15" />
<counter type="LINE" missed="2" covered="14" />
<counter type="METHOD" missed="2" covered="7" />
<counter type="CLASS" missed="0" covered="3" />
<counter type="INSTRUCTION" missed="6" covered="33" />
<counter type="LINE" missed="5" covered="29" />
<counter type="METHOD" missed="2" covered="9" />
<counter type="CLASS" missed="0" covered="5" />
</report>
')
}
Expand Down Expand Up @@ -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' {
Expand Down Expand Up @@ -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' {
Expand Down

0 comments on commit 6560623

Please sign in to comment.