reports/Builder__CoverageReport.st
author Claus Gittinger <cg@exept.de>
Thu, 28 Mar 2019 13:54:38 +0100
changeset 542 aa25a71be62a
parent 290 c5fdcba3c682
permissions -rw-r--r--
#DOCUMENTATION by cg
class: stx_goodies_builder_quickSelfTest
class definition

class: stx_goodies_builder_quickSelfTest class
added:18 methods
     1 "{ Package: 'stx:goodies/builder/reports' }"
     2 
     3 "{ NameSpace: Builder }"
     4 
     5 Report subclass:#CoverageReport
     6 	instanceVariableNames:'instrumentedMethods'
     7 	classVariableNames:''
     8 	poolDictionaries:''
     9 	category:'Builder-Reports'
    10 !
    11 
    12 
    13 !CoverageReport methodsFor:'accessing-defaults'!
    14 
    15 defaultFileSuffix
    16 
    17     "Return a default filename suffix. Note that format suffix will be 
    18      appended too if not file is explicitely specified"
    19 
    20     ^'Coverage'
    21 
    22     "Created: / 25-06-2013 / 01:12:36 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    23 !
    24 
    25 defaultFormat
    26     "raise an error: must be redefined in concrete subclass(es)"
    27 
    28     ^ Builder::CoverageReportFormat::Cobertura new
    29 
    30     "Modified: / 25-06-2013 / 01:31:14 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    31 !
    32 
    33 defaultName
    34 
    35     packages isNil ifTrue:[
    36         ^'someClasses'.
    37     ].
    38 
    39     ^String streamContents:[:s|
    40         packages do:[:packageOrClass|
    41             s nextPutAll: packageOrClass printString 
    42         ] separatedBy:[
    43             s nextPut:$,;space
    44         ]
    45     ]
    46 
    47     "Created: / 25-06-2013 / 01:29:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    48 ! !
    49 
    50 !CoverageReport methodsFor:'initialization'!
    51 
    52 initialize
    53     instrumentedMethods := Set new.
    54 
    55     "Created: / 25-06-2013 / 01:40:45 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    56     "Modified: / 25-06-2013 / 13:21:26 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    57 !
    58 
    59 setupForClasses:classes
    60     "raise an error: must be redefined in concrete subclass(es)"
    61 
    62     classes do:[:each | self instrumentClass: each].
    63 
    64     "Modified: / 25-06-2013 / 16:22:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    65 !
    66 
    67 setupForPackages:pkgs
    68     "raise an error: must be redefined in concrete subclass(es)"
    69 
    70     pkgs do:[:each | self instrumentPackage: each ].
    71 
    72     "Modified: / 25-06-2013 / 16:22:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    73 ! !
    74 
    75 !CoverageReport methodsFor:'instrumentation'!
    76 
    77 instrumentClass:class
    78     class instAndClassMethods do:[:method|
    79         self instrumentMethod: method 
    80     ]
    81 
    82     "Created: / 25-06-2013 / 01:42:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    83 !
    84 
    85 instrumentMethod:method
    86 
    87     | class selector |
    88 
    89     class := method mclass.
    90     selector := method selector.
    91     (self excludedFromCoverage: method) ifFalse:[
    92         method isInstrumented ifFalse:[
    93             InstrumentingCompiler compileMethod: method.
    94         ].
    95         instrumentedMethods add: (class compiledMethodAt: selector)
    96     ]
    97 
    98     "Created: / 25-06-2013 / 01:44:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    99     "Modified: / 28-06-2013 / 02:45:57 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   100 !
   101 
   102 instrumentPackage:pkg
   103     Smalltalk allMethodsDo:[:method|
   104         method package asSymbol == pkg asSymbol ifTrue:[
   105             self instrumentMethod: method.
   106         ]
   107     ]
   108 
   109     "Created: / 25-06-2013 / 01:42:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   110     "Modified: / 25-06-2013 / 17:51:17 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   111 ! !
   112 
   113 !CoverageReport methodsFor:'queries'!
   114 
   115 excludedFromCoverage: aMethod
   116     "Returns true, if given method should be excluded from
   117      coverage report.
   118 
   119      Here, certain 'metadata' method like documentation, copyright,
   120      version methods, printOn: and so on are exluded. Also, testcases
   121      and test resources are ommited"
   122 
   123     | mclass mselector tree pkgdef |
   124 
   125     mclass := aMethod mclass.
   126     mselector := aMethod selector.
   127 
   128     aMethod hasPrimitiveCode ifTrue:[ ^ true ].
   129     (mclass inheritsFrom: TestCase) ifTrue:[ ^ true ].
   130     (mclass inheritsFrom: TestResource) ifTrue:[ ^ true ].
   131     (mclass inheritsFrom: ProjectDefinition) ifTrue:[ ^ true ].
   132     mclass isMetaclass ifTrue:[
   133         (#(
   134                 copyright 
   135                 documentation
   136         ) includes: mselector) ifTrue:[ ^ true ].
   137         (AbstractSourceCodeManager isVersionMethodSelector: mselector) ifTrue:[ ^ true ].
   138     ] ifFalse:[
   139         (#(
   140             printString printOn: 
   141             inspectorExtraAttributes inspectorClass
   142         ) includes: mselector) ifTrue:[ ^ true ].    
   143     ].
   144 
   145     tree := (Parser parseMethod: aMethod source in: mclass) tree.
   146     tree isNil ifTrue:[ ^ true ]. "/ empty method?
   147     (tree isStatement and:[tree isReturnNode not and:[tree nextStatement isNil]]) ifTrue:[
   148         tree := tree expression.
   149     ].
   150 
   151     "/ Exclude all getters/return constants...
   152     (aMethod numArgs == 0 
   153         and:[tree isReturnNode 
   154         and:[tree expression isVariable or:[tree expression isConstant]]]) ifTrue:[ ^ true ].
   155 
   156     "/ Exclude all setters
   157     (aMethod numArgs == 1 
   158         and:[tree isAssignment 
   159         and:[(tree variable isInstanceVariable or:[tree variable isClassVariable])
   160         and:[tree expression isVariable
   161         and:[tree expression isMethodArg]]]]) ifTrue:[ ^ true ].
   162 
   163     pkgdef := ProjectDefinition definitionClassForPackage: aMethod package.
   164     pkgdef notNil ifTrue:[
   165         ^ pkgdef excludedFromCoverage:aMethod.
   166     ].
   167     ^ false
   168 
   169     "Created: / 28-06-2013 / 02:20:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   170     "Modified (comment): / 28-06-2013 / 11:31:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   171 ! !
   172 
   173 !CoverageReport methodsFor:'running'!
   174 
   175 runReport
   176     "Actually run the report. What to do (what classes/packages to check)
   177      must be stored instance variables"
   178 
   179     format write: instrumentedMethods.
   180 
   181     "Modified: / 25-06-2013 / 13:13:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   182 ! !
   183 
   184 !CoverageReport class methodsFor:'documentation'!
   185 
   186 version
   187     ^ '$Header$'
   188 !
   189 
   190 version_CVS
   191     ^ '$Header$'
   192 !
   193 
   194 version_SVN
   195     ^ '$Id$'
   196 ! !
   197