|
| 1 | +package scales.reporters |
| 2 | + |
| 3 | +import scales._ |
| 4 | +import scala.xml.Node |
| 5 | +import scales.MeasuredFile |
| 6 | +import java.util.Date |
| 7 | +import java.io.File |
| 8 | +import org.apache.commons.io.{FilenameUtils, FileUtils} |
| 9 | + |
| 10 | +/** @author Stephen Samuel */ |
| 11 | +object ScalesHtmlWriter extends CoverageWriter { |
| 12 | + |
| 13 | + def write(coverage: Coverage, dir: File): Unit = { |
| 14 | + val indexFile = new File(dir.getAbsolutePath + "/index.html") |
| 15 | + val packageFile = new File(dir.getAbsolutePath + "/packages.html") |
| 16 | + val overviewFile = new File(dir.getAbsolutePath + "/overview.html") |
| 17 | + |
| 18 | + FileUtils.copyInputStreamToFile(getClass.getResourceAsStream("/org/scalescc/reporters/index.html"), indexFile) |
| 19 | + FileUtils.write(packageFile, packages(coverage).toString()) |
| 20 | + FileUtils.write(overviewFile, overview(coverage).toString()) |
| 21 | + |
| 22 | + coverage.packages.foreach(write(_, dir)) |
| 23 | + } |
| 24 | + |
| 25 | + def write(pack: MeasuredPackage, dir: File) { |
| 26 | + val file = new File(dir.getAbsolutePath + "/" + pack.name.replace('.', '/') + "/package.html") |
| 27 | + file.getParentFile.mkdirs() |
| 28 | + FileUtils.write(file, _package(pack).toString()) |
| 29 | + pack.files.foreach(write(_, file.getParentFile)) |
| 30 | + } |
| 31 | + |
| 32 | + def write(mfile: MeasuredFile, dir: File) { |
| 33 | + val file = new File(dir.getAbsolutePath + "/" + FilenameUtils.getBaseName(mfile.source) + ".html") |
| 34 | + file.getParentFile.mkdirs() |
| 35 | + FileUtils.write(file, _file(mfile).toString()) |
| 36 | + } |
| 37 | + |
| 38 | + def _file(mfile: MeasuredFile): Node = { |
| 39 | + new SourceHighlighter().print(mfile) |
| 40 | + } |
| 41 | + |
| 42 | + def _package(pack: MeasuredPackage): Node = { |
| 43 | + <html> |
| 44 | + <head> |
| 45 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| 46 | + <title id='title'>Scales Code Coverage</title> |
| 47 | + <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css"/> |
| 48 | + </head> |
| 49 | + <body> |
| 50 | + <table class="pure-table pure-table-bordered pure-table-striped" style="font-size:12px"> |
| 51 | + <thead> |
| 52 | + <tr> |
| 53 | + <th>Class</th> |
| 54 | + <th>Source</th> |
| 55 | + <th>Lines</th> |
| 56 | + <th>Methods</th> |
| 57 | + <th>Statements</th> |
| 58 | + <th>Statements Invoked</th> |
| 59 | + <th>Statement Coverage</th> |
| 60 | + <th>Branches</th> |
| 61 | + <th>Branches Invoked</th> |
| 62 | + <th>Branch Coverage</th> |
| 63 | + </tr> |
| 64 | + </thead> |
| 65 | + <tbody> |
| 66 | + {pack.classes.map(_class)} |
| 67 | + </tbody> |
| 68 | + </table> |
| 69 | + </body> |
| 70 | + </html> |
| 71 | + } |
| 72 | + |
| 73 | + def _class(klass: MeasuredClass): Node = { |
| 74 | + val filename = FilenameUtils.getBaseName(klass.source) + ".html" |
| 75 | + val simpleClassName = klass.name.split('.').last |
| 76 | + <tr> |
| 77 | + <td> |
| 78 | + <a href={filename}> |
| 79 | + {simpleClassName} |
| 80 | + </a> |
| 81 | + </td> |
| 82 | + <td> |
| 83 | + {klass.statements.headOption.map(_.source.split('/').last).getOrElse("")} |
| 84 | + </td> |
| 85 | + <td> |
| 86 | + {klass.loc.toString} |
| 87 | + </td> |
| 88 | + <td> |
| 89 | + {klass.methodCount.toString} |
| 90 | + </td> |
| 91 | + <td> |
| 92 | + {klass.statementCount.toString} |
| 93 | + </td> |
| 94 | + <td> |
| 95 | + {klass.invokedStatementCount.toString} |
| 96 | + </td> |
| 97 | + <td> |
| 98 | + {klass.statementCoverageFormatted} |
| 99 | + % |
| 100 | + </td> |
| 101 | + <td> |
| 102 | + {klass.branchCount.toString} |
| 103 | + </td> |
| 104 | + <td> |
| 105 | + {klass.invokedBranchesCount.toString} |
| 106 | + </td> |
| 107 | + <td> |
| 108 | + {klass.branchCoverageFormatted} |
| 109 | + % |
| 110 | + </td> |
| 111 | + </tr> |
| 112 | + } |
| 113 | + |
| 114 | + def packages(coverage: Coverage): Node = { |
| 115 | + <html> |
| 116 | + <head> |
| 117 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| 118 | + <title id='title'>Scales Code Coverage</title> |
| 119 | + <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css"/> |
| 120 | + </head> |
| 121 | + <body> |
| 122 | + <table class="pure-table pure-table-bordered pure-table-striped" style="font-size: 12px"> |
| 123 | + <tbody> |
| 124 | + <tr> |
| 125 | + <td> |
| 126 | + <a href="overview.html" target="mainFrame"> |
| 127 | + All packages |
| 128 | + </a>{coverage.statementCoverageFormatted} |
| 129 | + % |
| 130 | + </td> |
| 131 | + </tr>{coverage.packages.map(arg => |
| 132 | + <tr> |
| 133 | + <td> |
| 134 | + <a href={arg.name.replace('.', '/') + "/package.html"} target="mainFrame"> |
| 135 | + {arg.name} |
| 136 | + </a>{arg.statementCoverageFormatted} |
| 137 | + % |
| 138 | + </td> |
| 139 | + </tr> |
| 140 | + )} |
| 141 | + </tbody> |
| 142 | + </table> |
| 143 | + </body> |
| 144 | + </html> |
| 145 | + } |
| 146 | + |
| 147 | + def risks(coverage: Coverage, limit: Int) = { |
| 148 | + <table class="pure-table pure-table-bordered pure-table-striped" style="font-size: 12px"> |
| 149 | + <caption>Top 20 Class Risks</caption> |
| 150 | + <thead> |
| 151 | + <tr> |
| 152 | + <th>Class</th> |
| 153 | + <th>Lines</th> |
| 154 | + <th>Methods</th> |
| 155 | + <th>Statements</th> |
| 156 | + <th>Statement Rate</th> |
| 157 | + <th>Branches</th> |
| 158 | + <th>Branch Rate</th> |
| 159 | + </tr> |
| 160 | + </thead> |
| 161 | + <tbody> |
| 162 | + {coverage.risks(limit).map(klass => |
| 163 | + <tr> |
| 164 | + <td> |
| 165 | + {klass.simpleName} |
| 166 | + </td> |
| 167 | + <td> |
| 168 | + {klass.loc.toString} |
| 169 | + </td> |
| 170 | + <td> |
| 171 | + {klass.methodCount.toString} |
| 172 | + </td> |
| 173 | + <td> |
| 174 | + {klass.statementCount.toString} |
| 175 | + </td> |
| 176 | + <td> |
| 177 | + {klass.statementCoverageFormatted} |
| 178 | + % |
| 179 | + </td> |
| 180 | + <td> |
| 181 | + {klass.branchCount.toString} |
| 182 | + </td> |
| 183 | + <td> |
| 184 | + {klass.branchCoverageFormatted} |
| 185 | + % |
| 186 | + </td> |
| 187 | + </tr>)} |
| 188 | + </tbody> |
| 189 | + </table> |
| 190 | + } |
| 191 | + |
| 192 | + def packages2(coverage: Coverage) = { |
| 193 | + val rows = coverage.packages.map(arg => { |
| 194 | + <tr> |
| 195 | + <td> |
| 196 | + {arg.name} |
| 197 | + </td> |
| 198 | + <td> |
| 199 | + {arg.invokedClasses.toString} |
| 200 | + / |
| 201 | + {arg.classCount} |
| 202 | + ( |
| 203 | + {arg.classCoverage.toString} |
| 204 | + %) |
| 205 | + </td> |
| 206 | + <td> |
| 207 | + {arg.invokedStatements.toString()} |
| 208 | + / |
| 209 | + {arg.statementCount} |
| 210 | + ( |
| 211 | + {arg.statementCoverageFormatted} |
| 212 | + %) |
| 213 | + </td> |
| 214 | + </tr> |
| 215 | + }) |
| 216 | + <table> |
| 217 | + {rows} |
| 218 | + </table> |
| 219 | + } |
| 220 | + |
| 221 | + def overview(coverage: Coverage): Node = { |
| 222 | + <html> |
| 223 | + <head> |
| 224 | + <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
| 225 | + <title id='title'>Scales Code Coverage</title> |
| 226 | + <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css"/> |
| 227 | + </head> |
| 228 | + <body> |
| 229 | + <div class="overview"> |
| 230 | + {stats(coverage)}{risks(coverage, 20)} |
| 231 | + </div> |
| 232 | + </body> |
| 233 | + </html> |
| 234 | + } |
| 235 | + |
| 236 | + def stats(coverage: Coverage): Node = { |
| 237 | + <table> |
| 238 | + <caption>Statistics generated at |
| 239 | + {new Date().toString} |
| 240 | + </caption> |
| 241 | + <tr> |
| 242 | + <td>Lines of code:</td> |
| 243 | + <td> |
| 244 | + {coverage.loc.toString} |
| 245 | + </td> |
| 246 | + <td>Statements:</td> |
| 247 | + <td> |
| 248 | + {coverage.statementCount} |
| 249 | + </td> |
| 250 | + <td>Clases per package:</td> |
| 251 | + <td> |
| 252 | + {coverage.avgClassesPerPackageFormatted} |
| 253 | + </td> |
| 254 | + <td>Methods per class:</td> |
| 255 | + <td> |
| 256 | + {coverage.avgMethodsPerClassFormatted} |
| 257 | + </td> |
| 258 | + </tr> |
| 259 | + <tr> |
| 260 | + <td>to be completed</td> |
| 261 | + <td> |
| 262 | + </td> |
| 263 | + <td>Packages:</td> |
| 264 | + <td> |
| 265 | + {coverage.classCount.toString} |
| 266 | + </td> |
| 267 | + <td>Classes:</td> |
| 268 | + <td> |
| 269 | + {coverage.packageCount.toString} |
| 270 | + </td> |
| 271 | + <td>Methods:</td> |
| 272 | + <td> |
| 273 | + {coverage.methodCount.toString} |
| 274 | + </td> |
| 275 | + </tr> |
| 276 | + </table> |
| 277 | + } |
| 278 | + |
| 279 | +} |
| 280 | + |
0 commit comments