TestCoverageReporter.st
changeset 507 ec48d46dce83
child 510 df4b9666403b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TestCoverageReporter.st	Wed Nov 07 15:19:46 2012 +0100
@@ -0,0 +1,154 @@
+"{ Package: 'stx:goodies/sunit' }"
+
+Object subclass:#TestCoverageReporter
+	instanceVariableNames:'packages stream'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'SUnit-Report'
+!
+
+!TestCoverageReporter class methodsFor:'documentation'!
+
+documentation
+"
+    Currently supported formats are:
+        #xml_cobertura      - a cobertura compatible format
+
+    public API entry:
+        |aTestResult|
+
+        InstrumentingCompiler compilePackage:'mypackageID'.
+        InstrumentationContext new run:[
+            aTestResult := aUnitTest suite run.
+        ].
+        'testResult.xml' asFilename writingFileDo:[:stream |
+            TestResultReporter report:aTestResult format:#xml_jUnit on:stream.
+        ].
+        'testCoverage.xml' asFilename writingFileDo:[:stream |
+                TestCoverageReporter reportPackages:{ 'mypackageID' } format:#xml_cobertura on:stream.
+        ].
+
+    [author:]
+        Claus Gittinger
+
+    [see also:]
+        cobertura
+            http://wiki.hudson-ci.org/display/HUDSON/PerfPublisher+Plugin
+            https://raw.github.com/jenkinsci/cobertura-plugin/master/src/test/resources/hudson/plugins/cobertura/coverage-with-data.xml    
+"
+!
+
+examples
+"
+    Smalltalk loadPackage:'exept:regression'
+
+                                                                               [exBegin]
+    |suite result testResult|
+
+    InstrumentingCompiler compilePackage:'stx:libjavascript'.
+    InstrumentationContext new run:[
+        testResult := RegressionTests::JavaScriptTests suite run.
+    ].
+    'testResult.xml' asFilename writingFileDo:[:stream |
+        TestResultReporter report:testResult format:#xml_jUnit on:stream.
+    ].
+    'testCoverage.xml' asFilename writingFileDo:[:stream |
+        TestCoverageReporter reportPackages:{ 'stx:libjavascript' } format:#xml_cobertura on:stream.
+    ].
+
+                                                                               [exEnd]
+"
+! !
+
+!TestCoverageReporter class methodsFor:'queries'!
+
+supportedFormats
+   "return a list of formats and short-info-string, as per supported format symbol"
+
+    ^ #(
+        (#'xml_cobertura'       'a cobertura-like xml format')
+    )
+
+    "Created: / 30-07-2011 / 10:18:18 / cg"
+! !
+
+!TestCoverageReporter class methodsFor:'reporting'!
+
+reportPackages: aCollectionOfPackages format: format on: stream
+    self new 
+        reportPackages: aCollectionOfPackages format: format on: stream
+! !
+
+!TestCoverageReporter methodsFor:'reporting'!
+
+reportPackages: aCollectionOfPackages format: aSymbol on: aStream
+
+    packages := aCollectionOfPackages.
+    stream := aStream.
+    self report: aSymbol
+! !
+
+!TestCoverageReporter methodsFor:'reporting - xml-cobertura'!
+
+reportXml_coberturaForPackage:aPackageID
+"
+ self new 
+        stream:Transcipt;
+        reportXml_coberturaForPackage:'stx:libbasic'
+"
+    "generate cobertura compatible XML coverage report format"
+
+    |classes instrumentedClasses uninstrumentedClasses lineRate branchRate|
+
+    instrumentedClasses := OrderedCollection new.    
+    uninstrumentedClasses := OrderedCollection new.    
+
+    Smalltalk allClassesInPackage:aPackageID do:[:eachClass |
+        ((eachClass methodDictionary values contains:[:someMethod | someMethod isInstrumented])
+        or:[ (eachClass class methodDictionary values contains:[:someMethod | someMethod isInstrumented]) ])
+        ifTrue:[
+            instrumentedClasses add:eachClass
+        ] ifFalse:[
+            uninstrumentedClasses add:eachClass
+        ].
+    ].
+
+    stream
+        nextPutLine: '<?xml version="1.0"?>';
+        nextPutLine: '<!!--DOCTYPE coverage SYSTEM "http://cobertura.sourceforge.net/xml/coverage-03.dtd"-->';
+        nextPutAll: '<coverage';
+        nextPutAll:(' line-rate="%1"' bindWith:lineRate);
+        nextPutAll:(' branch-rate="%1"' bindWith:branchRate);
+        nextPutAll:(' version="%1"' bindWith:'0.1' "dummy");
+        nextPutAll:(' timestamp="%1>"' bindWith:Timestamp now utcSecondsSince1970 * 1000 "millis").
+! !
+
+!TestCoverageReporter methodsFor:'reporting-private'!
+
+report:formatSymbol
+    "currently supported formatSymbols:
+            xml_cobertura"
+
+    |reportFormatSelector|
+
+    reportFormatSelector := self reportFormatSelector:formatSymbol.
+    (self respondsTo: reportFormatSelector)
+        ifTrue:[self perform: reportFormatSelector]
+        ifFalse:[self error:'Unsupported format: ', formatSymbol].
+
+    "Modified (comment): / 03-08-2011 / 12:57:54 / cg"
+!
+
+reportFormatSelector:format
+    ^ ('report' , format asString capitalized) asSymbol
+! !
+
+!TestCoverageReporter class methodsFor:'documentation'!
+
+version
+    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestCoverageReporter.st,v 1.1 2012-11-07 14:19:46 cg Exp $'
+!
+
+version_CVS
+    ^ '$Header: /cvs/stx/stx/goodies/sunit/TestCoverageReporter.st,v 1.1 2012-11-07 14:19:46 cg Exp $'
+! !