Improvements in coverage - exclude accessors and metadata methods.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Fri, 28 Jun 2013 03:54:44 +0200
changeset 185 f1415a086e05
parent 184 5f75ffc9642b
child 186 b53f4cc947e1
Improvements in coverage - exclude accessors and metadata methods.
reports/Builder__CoverageReport.st
--- a/reports/Builder__CoverageReport.st	Fri Jun 28 02:14:45 2013 +0200
+++ b/reports/Builder__CoverageReport.st	Fri Jun 28 03:54:44 2013 +0200
@@ -88,13 +88,15 @@
 
     class := method mclass.
     selector := method selector.
-    method isInstrumented ifFalse:[
-        InstrumentingCompiler compileMethod: method.
-    ].
-    instrumentedMethods add: (class compiledMethodAt: selector)
+    (self excludedFromCoverage: method) ifFalse:[
+        method isInstrumented ifFalse:[
+            InstrumentingCompiler compileMethod: method.
+        ].
+        instrumentedMethods add: (class compiledMethodAt: selector)
+    ]
 
     "Created: / 25-06-2013 / 01:44:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 25-06-2013 / 13:21:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 28-06-2013 / 02:45:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 instrumentPackage:pkg
@@ -108,6 +110,69 @@
     "Modified: / 25-06-2013 / 17:51:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!CoverageReport methodsFor:'queries'!
+
+excludedFromCoverage: aMethod
+    "Returns true, if given method should be excluded from
+     coverage report.
+
+     Here, certain 'metadata' method like documentation, copyright,
+     version methods, printOn: and so on are exluded. Also, testcases
+     and test resources are ommited"
+
+    | mclass mselector parser tree pkgdef |
+
+    mclass := aMethod mclass.
+    mselector := aMethod selector.
+
+    aMethod hasPrimitiveCode ifTrue:[ ^ true ].
+    (mclass inheritsFrom: TestCase) ifTrue:[ ^ true ].
+    (mclass inheritsFrom: TestResource) ifTrue:[ ^ true ].
+    (mclass inheritsFrom: ProjectDefinition) ifTrue:[ ^ true ].
+    mclass isMetaclass ifTrue:[
+        (#(
+                copyright 
+                documentation
+        ) includes: mselector) ifTrue:[ ^ true ].
+        (AbstractSourceCodeManager isVersionMethodSelector: mselector) ifTrue:[ ^ true ].
+    ] ifFalse:[
+        (#(
+            printString printOn: 
+            inspectorExtraAttributes inspectorClass
+        ) includes: mselector) ifTrue:[ ^ true ].    
+    ].
+
+    tree := (parser := Parser parseMethod: aMethod source in: mclass) tree.
+    tree isNil ifTrue:[ ^ true ]. "/ empty method?
+    (tree isStatement and:[tree isReturnNode not and:[tree nextStatement isNil]]) ifTrue:[
+        tree := tree expression.
+    ].
+
+    "/ Exclude all getters/return constants...
+    (aMethod numArgs == 0 and:[tree isReturnNode and:[tree expression isVariable or:[tree expression isConstant]]]) ifTrue:[ ^ true ].
+
+    "/ Exclude all setters
+    (aMethod numArgs == 1 
+        and:[tree isAssignment 
+        and:[tree variable type == #InstanceVariable
+        and:[tree expression isVariable
+        and:[tree expression type == #MethodArg]]]]) ifTrue:[ ^ true ].
+
+    pkgdef := ProjectDefinition definitionClassForPackage: aMethod package.
+    pkgdef notNil ifTrue:[
+        pkgdef excludedFromCoverage do:[:spec|
+            spec isArray ifTrue:[
+                (spec first = mclass name and:[spec second == mselector]) ifTrue:[ ^ true ].
+            ].
+            spec = mclass name ifTrue:[ ^ true ].                
+        ].
+        (pkgdef excludedFromCoverage: aMethod) ifTrue:[ ^ true ].
+    ].
+    ^false
+
+    "Created: / 28-06-2013 / 02:20:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
 !CoverageReport methodsFor:'running'!
 
 runReport