| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 //#library("coverage"); | |
| 6 | |
| 7 // Dart coverage test | |
| 8 class Coverage{ | |
| 9 static Map<String, Set<String>> coveredFunctions; | |
| 10 static Map<String, Set<int>> coveredStatements; | |
| 11 static Map<String, Map<int, Set<int>>> coveredBranches; | |
| 12 static Set<String> loopBranchTracker; | |
| 13 static Map<String, int> totalFunctions, totalStatements, totalBranches; | |
| 14 | |
| 15 static void init() { | |
| 16 coveredFunctions = new HashMap<String, Set<String>>(); | |
| 17 coveredStatements = new HashMap<String, Set<int>>(); | |
| 18 coveredBranches = new HashMap<String, Map<int, Set<int>>>(); | |
| 19 loopBranchTracker = new Set<String>(); | |
| 20 totalFunctions = new HashMap<String, int>(); | |
| 21 totalStatements = new HashMap<String, int>(); | |
| 22 totalBranches = new HashMap<String, int>(); | |
| 23 } | |
| 24 | |
| 25 } | |
| 26 | |
| 27 // Call for each Dart Unit | |
| 28 void setCoverageTotals(String unit, int numFunctions, int numStatements, | |
| 29 int numBranches) { | |
| 30 if(Coverage.totalFunctions == null){ | |
| 31 Coverage.init(); | |
| 32 } | |
| 33 Coverage.totalFunctions[unit] = numFunctions; | |
| 34 Coverage.totalStatements[unit] = numStatements; | |
| 35 Coverage.totalBranches[unit] = numBranches; | |
| 36 } | |
| 37 | |
| 38 void coverFunction(String unitName, String funcName) { | |
| 39 if(Coverage.coveredFunctions == null) { | |
| 40 Coverage.init(); | |
| 41 } | |
| 42 if(Coverage.coveredFunctions[unitName] == null) { | |
| 43 Coverage.coveredFunctions[unitName] = new Set<String>(); | |
| 44 } | |
| 45 Coverage.coveredFunctions[unitName].add(funcName); | |
| 46 } | |
| 47 | |
| 48 void coverStatement(String unitName, int lineNum) { | |
| 49 if(Coverage.coveredStatements == null) { | |
| 50 Coverage.init(); | |
| 51 } | |
| 52 if(Coverage.coveredStatements[unitName] == null) { | |
| 53 Coverage.coveredStatements[unitName] = new Set<int>(); | |
| 54 } | |
| 55 Coverage.coveredStatements[unitName].add(lineNum); | |
| 56 } | |
| 57 | |
| 58 void coverBranch(String unitName, int lineNum, int startPos) { | |
| 59 if(Coverage.coveredBranches == null) { | |
| 60 Coverage.init(); | |
| 61 } | |
| 62 if(Coverage.coveredBranches[unitName] == null) { | |
| 63 Coverage.coveredBranches[unitName] = new HashMap<int, Set<int>>(); | |
| 64 } | |
| 65 if(Coverage.coveredBranches[unitName][lineNum] == null) { | |
| 66 Coverage.coveredBranches[unitName][lineNum] = new Set<int>(); | |
| 67 } | |
| 68 Coverage.coveredBranches[unitName][lineNum].add(startPos); | |
| 69 } | |
| 70 | |
| 71 void loopBranchBefore(String unitName, int lineNum, int startPos) { | |
| 72 Coverage.loopBranchTracker.remove('$unitName,$lineNum,$startPos'); | |
| 73 } | |
| 74 | |
| 75 void loopBranchInside(String unitName, int lineNum, int startPos) { | |
| 76 Coverage.loopBranchTracker.add('$unitName,$lineNum,$startPos'); | |
| 77 } | |
| 78 | |
| 79 void coverLoopBranch(String unitName, int lineNum, int startPos, int endPos) { | |
| 80 if(Coverage.coveredBranches == null) { | |
| 81 Coverage.init(); | |
| 82 } | |
| 83 if(Coverage.coveredBranches[unitName] == null) { | |
| 84 Coverage.coveredBranches[unitName] = new HashMap<int, Set<int>>(); | |
| 85 } | |
| 86 if(Coverage.coveredBranches[unitName][lineNum] == null) { | |
| 87 Coverage.coveredBranches[unitName][lineNum] = new Set<int>(); | |
| 88 } | |
| 89 | |
| 90 if(Coverage.loopBranchTracker.contains('$unitName,$lineNum,$startPos')) { | |
| 91 // Branch that enters loop. | |
| 92 Coverage.coveredBranches[unitName][lineNum].add(startPos); | |
| 93 } else { | |
| 94 // Branch that doesn't enter loop. | |
| 95 Coverage.coveredBranches[unitName][lineNum].add(endPos); | |
| 96 } | |
| 97 | |
| 98 } | |
| 99 | |
| 100 void printCoverageSummary() { | |
| 101 print(getCoverageSummary()); | |
| 102 } | |
| 103 | |
| 104 String getCoverageSummary() { | |
| 105 int covFunc = 0, totFunc = 0, covStmt = 0, totStmt = 0, covBr = 0, totBr = 0; | |
| 106 if(Coverage.totalFunctions != null) { | |
| 107 for(String unit in Coverage.totalFunctions.getKeys()) { | |
| 108 if(Coverage.coveredFunctions.containsKey(unit)) { | |
| 109 covFunc += Coverage.coveredFunctions[unit].length; | |
| 110 } | |
| 111 if(Coverage.totalFunctions.containsKey(unit)) { | |
| 112 totFunc += Coverage.totalFunctions[unit]; | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 if(Coverage.totalStatements != null) { | |
| 118 for(String unit in Coverage.totalStatements.getKeys()) { | |
| 119 if(Coverage.coveredStatements.containsKey(unit)) { | |
| 120 covStmt += Coverage.coveredStatements[unit].length; | |
| 121 } | |
| 122 if(Coverage.totalStatements.containsKey(unit)) { | |
| 123 totStmt += Coverage.totalStatements[unit]; | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 if(Coverage.totalBranches != null) { | |
| 129 for(String unit in Coverage.totalBranches.getKeys()) { | |
| 130 if(Coverage.coveredBranches.containsKey(unit)) { | |
| 131 Map<int, Set<int>> brn = Coverage.coveredBranches[unit]; | |
| 132 for(int line in brn.getKeys()) { | |
| 133 for(int st in brn[line]) { | |
| 134 covBr++; | |
| 135 } | |
| 136 } | |
| 137 } | |
| 138 if(Coverage.totalBranches.containsKey(unit)) { | |
| 139 totBr += Coverage.totalBranches[unit]; | |
| 140 } | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 StringBuffer output = new StringBuffer('COVERAGE SUMMARY:\n'); | |
| 145 output.add('Function Coverage = '); | |
| 146 output.add('$covFunc/$totFunc (${calculatePercent(covFunc,totFunc)}%)\n'); | |
| 147 output.add('Statement Coverage = '); | |
| 148 output.add('$covStmt/$totStmt (${calculatePercent(covStmt,totStmt)}%)\n'); | |
| 149 output.add('Branch Coverage = '); | |
| 150 output.add('$covBr/$totBr (${calculatePercent(covBr,totBr)}%)'); | |
| 151 return output.toString(); | |
| 152 } | |
| 153 | |
| 154 String getCoverageDetails(){ | |
| 155 Set<String> allUnits = new HashSet<String>(); | |
| 156 if(Coverage.totalFunctions != null) { | |
| 157 allUnits.addAll(Coverage.totalFunctions.getKeys()); | |
| 158 } | |
| 159 if(Coverage.totalStatements != null) { | |
| 160 allUnits.addAll(Coverage.totalStatements.getKeys()); | |
| 161 } | |
| 162 if(Coverage.totalBranches != null) { | |
| 163 allUnits.addAll(Coverage.totalBranches.getKeys()); | |
| 164 } | |
| 165 | |
| 166 StringBuffer coverageDetails = new StringBuffer(); | |
| 167 for(String unit in allUnits) { | |
| 168 int covFunc = 0, totFunc = 0, covStmt = 0, totStmt = 0, covBr = 0, totBr = 0
; | |
| 169 if(Coverage.totalFunctions != null && | |
| 170 Coverage.totalFunctions.containsKey(unit)) { | |
| 171 if(Coverage.coveredFunctions.containsKey(unit)){ | |
| 172 covFunc = Coverage.coveredFunctions[unit].length; | |
| 173 } | |
| 174 totFunc = Coverage.totalFunctions[unit]; | |
| 175 } | |
| 176 if(Coverage.totalStatements != null && | |
| 177 Coverage.totalStatements.containsKey(unit)) { | |
| 178 if(Coverage.coveredStatements.containsKey(unit)){ | |
| 179 covStmt = Coverage.coveredStatements[unit].length; | |
| 180 } | |
| 181 totStmt = Coverage.totalStatements[unit]; | |
| 182 } | |
| 183 if(Coverage.totalBranches != null && | |
| 184 Coverage.totalBranches.containsKey(unit)) { | |
| 185 if(Coverage.coveredBranches.containsKey(unit)){ | |
| 186 Map<int, Set<int>> brn = Coverage.coveredBranches[unit]; | |
| 187 for(int line in brn.getKeys()) { | |
| 188 for(int st in brn[line]) { | |
| 189 covBr++; | |
| 190 } | |
| 191 } | |
| 192 } | |
| 193 totBr = Coverage.totalBranches[unit]; | |
| 194 } | |
| 195 coverageDetails.add('<tr> <td>$unit <td> $covFunc/$totFunc '); | |
| 196 coverageDetails.add('<td> $covStmt/$totStmt <td> $covBr/$totBr\n'); | |
| 197 } | |
| 198 return coverageDetails.toString(); | |
| 199 } | |
| 200 | |
| 201 String calculatePercent(int val, int total){ | |
| 202 return (val/total*100).toStringAsPrecision(3); | |
| 203 } | |
| 204 | |
| 205 String getString(Map m){ | |
| 206 StringBuffer o = new StringBuffer(); | |
| 207 if(m != null) { | |
| 208 for(String key in m.getKeys()){ | |
| 209 o.add('$key:${m[key]}\n'); | |
| 210 } | |
| 211 } | |
| 212 return o.toString(); | |
| 213 } | |
| OLD | NEW |