Skip to content

Commit 4e33f7f

Browse files
committed
wrote a unit test for getcoveragetargetsforfile
1 parent d308588 commit 4e33f7f

File tree

5 files changed

+110
-6
lines changed

5 files changed

+110
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [3.1.2] - 2024-07-12
9-
10-
### Fixed
11-
- Now the code strips leading and trailing whitespace from coverage.list, so "PackageName.PKG " will still be loaded properly
128

139
## [3.1.1] - Unreleased
1410

1511
### Fixed
1612
- #39: Fixed bug where results viewer gave divide by zero error when there were 0 executed methods in the covered code
13+
- Now the code strips leading and trailing whitespace from coverage.list, so "PackageName.PKG " will still be loaded properly
1714

1815
## [3.1.0] - 2024-07-05
1916

cls/TestCoverage/Manager.cls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ Method AddCoverageRoutine(pRoutineName As %String) [ Private ]
362362
}
363363
}
364364

365-
ClassMethod GetCoverageTargetsForFile(pFileName As %String, Output pTargetArray) [ Private ]
365+
ClassMethod GetCoverageTargetsForFile(pFileName As %String, Output pTargetArray)
366366
{
367367
Kill pTargetArray
368368

@@ -393,7 +393,7 @@ ClassMethod GetCoverageTargetsForFile(pFileName As %String, Output pTargetArray)
393393
Set tExclude = 1
394394
Set tLine = $Extract(tLine,2,*)
395395
}
396-
396+
Set tLine = $zstrip(tLine, "<>W") // again in case there were extra spaces after the -
397397
Set tName = $Piece(tLine,".",1,*-1)
398398
Set tExtension = $ZConvert($Piece(tLine,".",*),"U")
399399

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
Class UnitTest.TestCoverage.Unit.TestCoverageList Extends %UnitTest.TestCase
2+
{
3+
4+
/// helper function to find the samplecovlist.list's path
5+
ClassMethod FindCoverageList(directory As %String = "") As %String
6+
{
7+
set stmt = ##class(%SQL.Statement).%New()
8+
set status = stmt.%PrepareClassQuery("%File", "FileSet")
9+
if $$$ISERR(status) {write "%Prepare failed:" do $SYSTEM.Status.DisplayError(status) quit}
10+
11+
set rset = stmt.%Execute(directory)
12+
if (rset.%SQLCODE '= 0) {write "%Execute failed:", !, "SQLCODE ", rset.%SQLCODE, ": ", rset.%Message quit}
13+
14+
while rset.%Next()
15+
{
16+
set name = rset.%Get("Name")
17+
set type = rset.%Get("Type")
18+
19+
if (type = "F") {
20+
if ($piece(name, "\", *) = "samplecovlist.list") {
21+
return name
22+
}
23+
} elseif (type = "D"){
24+
do ..FindCoverageList(name)
25+
}
26+
}
27+
if (rset.%SQLCODE < 0) {write "%Next failed:", !, "SQLCODE ", rset.%SQLCODE, ": ", rset.%Message quit}
28+
}
29+
30+
Method TestGettingCoverageList()
31+
{
32+
set tFile = ..FindCoverageList(^UnitTestRoot) // finds the samplecovlist.list
33+
do ##class(TestCoverage.Manager).GetCoverageTargetsForFile(tFile, .tTargetArray)
34+
35+
Set CorrectCoverageTargets("CLS", "TestCoverage.Data.CodeSubUnit") = ""
36+
Set CorrectCoverageTargets("CLS", "TestCoverage.Data.CodeSubUnit.Method") = ""
37+
Set CorrectCoverageTargets("CLS","TestCoverage.Data.CodeUnit")=""
38+
Set CorrectCoverageTargets("CLS","TestCoverage.Data.CodeUnitMap")=""
39+
Set CorrectCoverageTargets("CLS","TestCoverage.Data.Coverage")=""
40+
Set CorrectCoverageTargets("CLS","TestCoverage.Data.Run")=""
41+
Set CorrectCoverageTargets("CLS","TestCoverage.Manager")=""
42+
Set CorrectCoverageTargets("MAC","UnitTest.TestCoverage.Unit.CodeUnit.G1")=""
43+
Set CorrectCoverageTargets("MAC","UnitTest.TestCoverage.Unit.sampleRoutine")=""
44+
Do $$$AssertEquals(..CompareArrays(.tTargetArray, .CorrectCoverageTargets, .pMessage), 1, "tTargetarray equals CorrectCoverageTargets")
45+
Do $$$LogMessage(pMessage)
46+
}
47+
48+
/// Taken from Tim's Developer Community post
49+
/// Returns true if arrays <var>first</var> and <var>second</var> have all the same subscripts and all
50+
/// the same values at those subscripts. <br />
51+
/// If <var>first</var> and <var>second</var> both happen to be either undefined or unsubscripted variables,
52+
/// returns true if they're both undefined or have the same value.<br />
53+
/// <var>pMessage</var> has details of the first difference found, if any.
54+
ClassMethod CompareArrays(ByRef first, ByRef second, Output pMessage) As %Boolean [ ProcedureBlock = 0 ]
55+
{
56+
New tEqual,tRef1,tRef2,tRef1Data,tRef1Value,tRef2Data,tRef2Value
57+
58+
Set pMessage = ""
59+
Set tEqual = 1
60+
Set tRef1 = "first"
61+
Set tRef2 = "second"
62+
While (tRef1 '= "") || (tRef2 '= "") {
63+
#; See if the subscript is the same for both arrays.
64+
#; If not, one of them has a subscript the other doesn't, and they're not equal.
65+
If ($Piece(tRef1,"first",2) '= $Piece(tRef2,"second",2)) {
66+
Set tEqual = 0
67+
Set pMessage = "Different subscripts encountered by $Query: "_
68+
$Case(tRef1,"":"<end>",:tRef1)_"; "_$Case(tRef2,"":"<end>",:tRef2)
69+
Quit
70+
}
71+
72+
Kill tRef1Value,tRef2Value
73+
Set tRef1Data = $Data(@tRef1,tRef1Value)
74+
Set tRef2Data = $Data(@tRef2,tRef2Value)
75+
#; See if the $Data values are the same for the two.
76+
#; This is really only useful to detect if one of the arrays is undefined on the first pass;
77+
#; $Query only returns subscripts with data.
78+
#; This will catch only one being defined, or one being an array and
79+
#; ​the other being a regular variable.
80+
If (tRef1Data '= tRef2Data) {
81+
Set tEqual = 0
82+
Set pMessage = "$Data("_tRef1_")="_tRef1Data_"; $Data("_tRef2_")="_tRef2Data
83+
Quit
84+
} ElseIf (tRef1Data#2) && (tRef2Data#2) {
85+
#; See if the value at the subscript is the same for both arrays.
86+
#; If not, they're not equal.
87+
If (tRef1Value '= tRef2Value) {
88+
Set tEqual = 0
89+
Set pMessage = tRef1_"="_@tRef1_"; "_tRef2_"="_@tRef2
90+
Quit
91+
}
92+
}
93+
94+
Set tRef1 = $Query(@tRef1)
95+
Set tRef2 = $Query(@tRef2)
96+
}
97+
Quit tEqual
98+
}
99+
100+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ROUTINE UnitTest.TestCoverage.Unit.sampleRoutine
2+
UnitTestTestCoverageUnitsampleRoutine ;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
TestCoverage.Data.PKG
2+
- TestCoverage.Data.Aggregate.PKG
3+
4+
UnitTest*.mac
5+
TestCoverage.Manager.cls

0 commit comments

Comments
 (0)