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, int num
Branches) { |
| 29 if(Coverage.totalFunctions == null){ |
| 30 Coverage.init(); |
| 31 } |
| 32 Coverage.totalFunctions[unit] = numFunctions; |
| 33 Coverage.totalStatements[unit] = numStatements; |
| 34 Coverage.totalBranches[unit] = numBranches; |
| 35 } |
| 36 |
| 37 void coverFunction(String unitName, String funcName) { |
| 38 if(Coverage.coveredFunctions == null) { |
| 39 Coverage.init(); |
| 40 } |
| 41 if(Coverage.coveredFunctions[unitName] == null) { |
| 42 Coverage.coveredFunctions[unitName] = new Set<String>(); |
| 43 } |
| 44 Coverage.coveredFunctions[unitName].add(funcName); |
| 45 } |
| 46 |
| 47 void coverStatement(String unitName, int lineNum) { |
| 48 if(Coverage.coveredStatements == null) { |
| 49 Coverage.init(); |
| 50 } |
| 51 if(Coverage.coveredStatements[unitName] == null) { |
| 52 Coverage.coveredStatements[unitName] = new Set<int>(); |
| 53 } |
| 54 Coverage.coveredStatements[unitName].add(lineNum); |
| 55 } |
| 56 |
| 57 void coverBranch(String unitName, int lineNum, int startPos) { |
| 58 if(Coverage.coveredBranches == null) { |
| 59 Coverage.init(); |
| 60 } |
| 61 if(Coverage.coveredBranches[unitName] == null) { |
| 62 Coverage.coveredBranches[unitName] = new HashMap<int, Set<int>>(); |
| 63 } |
| 64 if(Coverage.coveredBranches[unitName][lineNum] == null) { |
| 65 Coverage.coveredBranches[unitName][lineNum] = new Set<int>(); |
| 66 } |
| 67 Coverage.coveredBranches[unitName][lineNum].add(startPos); |
| 68 } |
| 69 |
| 70 void loopBranchBefore(String unitName, int lineNum, int startPos) { |
| 71 Coverage.loopBranchTracker.remove('$unitName,$lineNum,$startPos'); |
| 72 } |
| 73 |
| 74 void loopBranchInside(String unitName, int lineNum, int startPos) { |
| 75 Coverage.loopBranchTracker.add('$unitName,$lineNum,$startPos'); |
| 76 } |
| 77 |
| 78 void coverLoopBranch(String unitName, int lineNum, int startPos, int endPos) { |
| 79 if(Coverage.coveredBranches == null) { |
| 80 Coverage.init(); |
| 81 } |
| 82 if(Coverage.coveredBranches[unitName] == null) { |
| 83 Coverage.coveredBranches[unitName] = new HashMap<int, Set<int>>(); |
| 84 } |
| 85 if(Coverage.coveredBranches[unitName][lineNum] == null) { |
| 86 Coverage.coveredBranches[unitName][lineNum] = new Set<int>(); |
| 87 } |
| 88 |
| 89 if(Coverage.loopBranchTracker.contains('$unitName,$lineNum,$startPos')) { |
| 90 Coverage.coveredBranches[unitName][lineNum].add(startPos); // Branch that en
ters loop |
| 91 } else { |
| 92 Coverage.coveredBranches[unitName][lineNum].add(endPos); // Branch that does
n't enter loop |
| 93 } |
| 94 |
| 95 } |
| 96 |
| 97 void printCoverageSummary() { |
| 98 print(getCoverageSummary()); |
| 99 } |
| 100 |
| 101 String getCoverageSummary() { |
| 102 int covFunc = 0, totFunc = 0, covStmt = 0, totStmt = 0, covBr = 0, totBr = 0; |
| 103 if(Coverage.totalFunctions != null) { |
| 104 for(String unit in Coverage.totalFunctions.getKeys()) { |
| 105 if(Coverage.coveredFunctions.containsKey(unit)) { |
| 106 covFunc += Coverage.coveredFunctions[unit].length; |
| 107 } |
| 108 if(Coverage.totalFunctions.containsKey(unit)) { |
| 109 totFunc += Coverage.totalFunctions[unit]; |
| 110 } |
| 111 } |
| 112 } |
| 113 |
| 114 if(Coverage.totalStatements != null) { |
| 115 for(String unit in Coverage.totalStatements.getKeys()) { |
| 116 if(Coverage.coveredStatements.containsKey(unit)) { |
| 117 covStmt += Coverage.coveredStatements[unit].length; |
| 118 } |
| 119 if(Coverage.totalStatements.containsKey(unit)) { |
| 120 totStmt += Coverage.totalStatements[unit]; |
| 121 } |
| 122 } |
| 123 } |
| 124 |
| 125 if(Coverage.totalBranches != null) { |
| 126 for(String unit in Coverage.totalBranches.getKeys()) { |
| 127 if(Coverage.coveredBranches.containsKey(unit)) { |
| 128 Map<int, Set<int>> brn = Coverage.coveredBranches[unit]; |
| 129 for(int line in brn.getKeys()) { |
| 130 for(int st in brn[line]) { |
| 131 covBr++; |
| 132 } |
| 133 } |
| 134 } |
| 135 if(Coverage.totalBranches.containsKey(unit)) { |
| 136 totBr += Coverage.totalBranches[unit]; |
| 137 } |
| 138 } |
| 139 } |
| 140 |
| 141 StringBuffer output = new StringBuffer('COVERAGE SUMMARY:\n'); |
| 142 output.add('Function Coverage = $covFunc/$totFunc (${calculatePercent(covFunc,
totFunc)}%)\n'); |
| 143 output.add('Statement Coverage = $covStmt/$totStmt (${calculatePercent(covStmt
,totStmt)}%)\n'); |
| 144 output.add('Branch Coverage = $covBr/$totBr (${calculatePercent(covBr,totBr)}%
)'); |
| 145 return output.toString(); |
| 146 } |
| 147 |
| 148 String getCoverageDetails(){ |
| 149 Set<String> allUnits = new HashSet<String>(); |
| 150 if(Coverage.totalFunctions != null) { |
| 151 allUnits.addAll(Coverage.totalFunctions.getKeys()); |
| 152 } |
| 153 if(Coverage.totalStatements != null) { |
| 154 allUnits.addAll(Coverage.totalStatements.getKeys()); |
| 155 } |
| 156 if(Coverage.totalBranches != null) { |
| 157 allUnits.addAll(Coverage.totalBranches.getKeys()); |
| 158 } |
| 159 |
| 160 StringBuffer coverageDetails = new StringBuffer(); |
| 161 for(String unit in allUnits) { |
| 162 int covFunc = 0, totFunc = 0, covStmt = 0, totStmt = 0, covBr = 0, tot
Br = 0; |
| 163 if(Coverage.totalFunctions != null && Coverage.totalFunctions.contains
Key(unit)) { |
| 164 if(Coverage.coveredFunctions.containsKey(unit)){ |
| 165 covFunc = Coverage.coveredFunctions[unit].length; |
| 166 } |
| 167 totFunc = Coverage.totalFunctions[unit]; |
| 168 } |
| 169 if(Coverage.totalStatements != null && Coverage.totalStatements.contai
nsKey(unit)) { |
| 170 if(Coverage.coveredStatements.containsKey(unit)){ |
| 171 covStmt = Coverage.coveredStatements[unit].length; |
| 172 } |
| 173 totStmt = Coverage.totalStatements[unit]; |
| 174 } |
| 175 if(Coverage.totalBranches != null && Coverage.totalBranches.containsKe
y(unit)) { |
| 176 if(Coverage.coveredBranches.containsKey(unit)){ |
| 177 Map<int, Set<int>> brn = Coverage.coveredBranches[unit]; |
| 178 for(int line in brn.getKeys()) { |
| 179 for(int st in brn[line]) { |
| 180 covBr++; |
| 181 } |
| 182 } |
| 183 } |
| 184 totBr = Coverage.totalBranches[unit]; |
| 185 } |
| 186 coverageDetails.add('<tr> <td>$unit <td> $covFunc/$totFunc <td> $covSt
mt/$totStmt <td> $covBr/$totBr\n'); |
| 187 } |
| 188 return coverageDetails.toString(); |
| 189 } |
| 190 |
| 191 String calculatePercent(int val, int total){ |
| 192 return (val/total*100).toStringAsPrecision(3); |
| 193 } |
| 194 |
| 195 String getString(Map m){ |
| 196 StringBuffer o = new StringBuffer(); |
| 197 if(m != null) { |
| 198 for(String key in m.getKeys()){ |
| 199 o.add('$key:${m[key]}\n'); |
| 200 } |
| 201 } |
| 202 return o.toString(); |
| 203 } |
OLD | NEW |