Created
April 3, 2021 10:38
-
-
Save johlju/0024dea16f80e9cb61a9099aae6abef1 to your computer and use it in GitHub Desktop.
Get expected missed & covered instructions from code coverage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Source: | |
SourceLineNumber HitCount | |
---------------- -------- | |
20 219 | |
22 219 | |
22 219 | |
22 219 | |
27 10 | |
27 10 | |
45 0 | |
64 0 | |
64 0 | |
65 1 | |
65 0 | |
65 0 | |
Expected result: | |
Line Covered Missed | |
---- ------- ------ | |
20 1 0 | |
22 3 0 | |
27 2 0 | |
45 0 1 | |
64 0 2 | |
65 1 2 | |
#> | |
function Get-CoveredInstruction | |
{ | |
$codeCoverageResult = @( | |
[PSCustomObject] @{ | |
SourceLineNumber = 20 | |
HitCount = 219 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 22 | |
HitCount = 219 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 22 | |
HitCount = 219 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 22 | |
HitCount = 219 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 27 | |
HitCount = 10 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 27 | |
HitCount = 10 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 45 | |
HitCount = 0 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 64 | |
HitCount = 0 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 64 | |
HitCount = 0 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 65 | |
HitCount = 1 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 65 | |
HitCount = 0 | |
} | |
[PSCustomObject] @{ | |
SourceLineNumber = 65 | |
HitCount = 0 | |
} | |
) | |
$linesToReport = @() | |
# Get all instructions that was covered by grouping on 'SourceLineNumber'. | |
# Note: The hit count is not relevant, just that an instruction was hit on the line. | |
$linesCovered = $codeCoverageResult | | |
Sort-Object -Property 'SourceLineNumber' | | |
Where-Object { | |
$_.HitCount -ge 1 | |
} | | |
Group-Object -Property 'SourceLineNumber' -NoElement | |
# Add each covered line with its count of instructions covered. | |
$linesCovered | | |
ForEach-Object { | |
$linesToReport += [PSCustomObject] @{ | |
Line = [System.UInt32] $_.Name | |
Covered = $_.Count | |
Missed = 0 | |
} | |
} | |
# Get all instructions that was missed by grouping on 'SourceLineNumber'. | |
$linesMissed = $codeCoverageResult | | |
Sort-Object -Property 'SourceLineNumber' | | |
Where-Object { | |
$_.HitCount -eq 0 | |
} | | |
Group-Object -Property 'SourceLineNumber' -NoElement | |
# Add each missed line with its count of instructions missed. | |
$linesMissed | | |
ForEach-Object { | |
# Test if there are an existing line that is covered. | |
if ($linesToReport.Line -contains $_.Name) | |
{ | |
$lineNumberToLookup = $_.Name | |
$coveredLineItem = $linesToReport | | |
Where-Object -FilterScript { | |
$_.Line -eq $lineNumberToLookup | |
} | |
$coveredLineItem.Missed += $_.Count | |
} | |
else | |
{ | |
$linesToReport += [PSCustomObject] @{ | |
Line = [System.UInt32] $_.Name | |
Covered = 0 | |
Missed = $_.Count | |
} | |
} | |
} | |
return ($linesToReport | Sort-Object -Property 'Line') | |
} | |
Get-CoveredInstruction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment