Skip to content

Commit bcd58fa

Browse files
committed
Added cyclomatic complexities for python subunits
1 parent 6837c62 commit bcd58fa

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

cls/TestCoverage/Data/CodeSubUnit.cls

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ Property Mask As TestCoverage.DataType.Bitstring;
99
/// Cyclomatic complexity of the code subunit
1010
Property Complexity As %Integer [ InitialExpression = 1 ];
1111

12+
/// 1 if it's a python class method, 0 if not
13+
Property IsPythonMethod As %Boolean;
14+
1215
Method UpdateComplexity() As %Status
1316
{
1417
Quit $$$OK
@@ -26,6 +29,9 @@ Storage Default
2629
<Value name="3">
2730
<Value>Complexity</Value>
2831
</Value>
32+
<Value name="4">
33+
<Value>IsPythonMethod</Value>
34+
</Value>
2935
</Data>
3036
<DataLocation>{%%PARENT}("SubUnits")</DataLocation>
3137
<DefaultData>CodeSubUnitDefaultData</DefaultData>
@@ -36,4 +42,3 @@ Storage Default
3642
}
3743

3844
}
39-

cls/TestCoverage/Data/CodeUnit.cls

+31-7
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,6 @@ ClassMethod GetCurrentByName(pInternalName As %String, pSourceNamespace As %Stri
163163
Set $Bit(tMethodMask,j) = 1
164164
}
165165
Set tMethodSignature = $list(pDocumentText, tStartLine)
166-
Set tSubUnit = ##class(TestCoverage.Data.CodeSubUnit.Method).%New()
167-
Set tSubUnit.Name = tMethod
168-
Set tSubUnit.DisplaySignature = tMethodSignature
169-
Set tSubUnit.Mask = tMethodMask
170-
Do pCodeUnit.SubUnits.Insert(tSubUnit)
171166
}
172167
}
173168
Else {
@@ -194,6 +189,8 @@ ClassMethod GetCurrentByName(pInternalName As %String, pSourceNamespace As %Stri
194189
Set tSubUnit.Name = tMethod
195190
Set tSubUnit.DisplaySignature = tMethodSignature
196191
Set tSubUnit.Mask = tMethodMask
192+
set NormalizedSignature = $zconvert($zstrip(tMethodSignature, "*W"), "l")
193+
set tSubUnit.IsPythonMethod = (NormalizedSignature [ "[language=python]")
197194
Do pCodeUnit.SubUnits.Insert(tSubUnit)
198195
Set tMethod = ""
199196
Set tMethodSignature = ""
@@ -506,15 +503,28 @@ Method UpdateComplexity() As %Status
506503
If (..Type '= "CLS") {
507504
Quit
508505
}
509-
506+
507+
// python methods
508+
If (##class(TestCoverage.Manager).HasPython(..Name)) {
509+
do ##class(TestCoverage.Data.CodeUnit).GetCurrentByName(..Name _ ".PY", , .pPyCodeUnit, )
510+
set tDocumentText = pPyCodeUnit.Lines.Serialize()
511+
set tMethodComplexities = ..GetPythonComplexities(tDocumentText)
512+
}
513+
510514
Set tKey = ""
511515
For {
512516
Set tSubUnit = ..SubUnits.GetNext(.tKey)
513517
If (tKey = "") {
514518
Quit
515519
}
516-
$$$ThrowOnError(tSubUnit.UpdateComplexity())
520+
If (tSubUnit.IsPythonMethod) {
521+
set tSubUnit.Complexity = tMethodComplexities."__getitem__"(tSubUnit.Name)
522+
$$$ThrowOnError(tSubUnit.%Save(0))
523+
} Else {
524+
$$$ThrowOnError(tSubUnit.UpdateComplexity())
525+
}
517526
}
527+
518528

519529
$$$ThrowOnError(..%Save())
520530
} Catch e {
@@ -523,6 +533,20 @@ Method UpdateComplexity() As %Status
523533
Quit tSC
524534
}
525535

536+
ClassMethod GetPythonComplexities(pDocumentText) [ Language = python ]
537+
{
538+
from radon.complexity import cc_visit
539+
import iris
540+
source_lines = iris.cls('%SYS.Python').ToList(pDocumentText)
541+
source_code = "\n".join(source_lines)
542+
visitor = cc_visit(source_code)
543+
class_info = visitor[0]
544+
method_complexities = {}
545+
for method in class_info.methods:
546+
method_complexities[method.name] = method.complexity
547+
return method_complexities
548+
}
549+
526550
Method GetMethodOffset(pAbsoluteLine As %Integer, Output pMethod As %String, Output pOffset As %Integer)
527551
{
528552
}

cls/TestCoverage/Utils.cls

-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ ClassMethod Snapshot(pIntRoutines As %List, Output pRelevantRoutines As %List =
122122
$$$ThrowOnError(##class(TestCoverage.Data.CodeUnit).GetCurrentByName(tName_".PY",,.tPyCodeUnit))
123123

124124
// update the executable lines for the .cls file's python
125-
set ^IRIS.TEMPCG($i(^IRIS.TEMPCG)) = "Calling update executable lines on " _ tName
126125
$$$ThrowOnError(tCodeUnit.UpdatePyExecutableLines(tName, .tPyCodeUnit))
127126

128127
} ElseIf '$BitCount(tCodeUnit.ExecutableLines,1) {

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
radon==6.*

0 commit comments

Comments
 (0)