Skip to content
Browse files

Final pieces necessary to make it work on a real code base

  • Loading branch information...
1 parent ed8b15d commit b751c2d68c1a8b13bac32e3bbdc5115ff8e3ff2f @olabini olabini committed Nov 8, 2011
View
23 features/simple_function_line_coverage.feature
@@ -26,3 +26,26 @@ Feature: Simple function line coverage
| | } |
| 1 | t1(); t1(); t1(); t1(); |
+ Scenario: Function defined using variable that is never called
+ * I get the following line coverage for the JavaScript:
+ | coverage | code |
+ | 1 | var t1 = function() { |
+ | 0 | return 0; |
+ | | } |
+
+ Scenario: Function defined using variable that is called once
+ * I get the following line coverage for the JavaScript:
+ | coverage | code |
+ | 1 | var t1 = function() { |
+ | 1 | return 0; |
+ | | } |
+ | 1 | t1(); |
+
+ Scenario: Function defined using variable that is called several times
+ * I get the following line coverage for the JavaScript:
+ | coverage | code |
+ | 1 | var t1 = function() { |
+ | 4 | return 0; |
+ | | } |
+ | 1 | t1(); t1(); t1(); t1(); |
+
View
44 src/main/com/olabini/jescov/CoverageDebugger.java
@@ -52,29 +52,31 @@ public void handleCompilationDone(Context cx, DebuggableScript fnOrScript, Strin
private CoverageData coverageData;
void generateCoverageData(Scriptable scope) {
- Map<String, Map<Integer, LineCoverage>> coverageResults = new HashMap<String, Map<Integer, LineCoverage>>();
- Map<String, Collection<BranchCoverage>> coverageResults2 = new HashMap<String, Collection<BranchCoverage>>();
- NativeArray na = (NativeArray)(((Scriptable)scope.get("LCOV", scope)).get("collectedCoverageData", scope));
- for(Object coverage : na) {
- generateLineCoverage((Scriptable) coverage, scope, coverageResults);
- }
- NativeArray na2 = (NativeArray)(((Scriptable)scope.get("BCOV", scope)).get("collectedCoverageData", scope));
- for(Object coverage : na2) {
- generateBranchCoverage((Scriptable) coverage, scope, coverageResults2);
- }
+ if(configuration.isEnabled()) {
+ Map<String, Map<Integer, LineCoverage>> coverageResults = new HashMap<String, Map<Integer, LineCoverage>>();
+ Map<String, Collection<BranchCoverage>> coverageResults2 = new HashMap<String, Collection<BranchCoverage>>();
+ NativeArray na = (NativeArray)(((Scriptable)scope.get("LCOV", scope)).get("collectedCoverageData", scope));
+ for(Object coverage : na) {
+ generateLineCoverage((Scriptable) coverage, scope, coverageResults);
+ }
+ NativeArray na2 = (NativeArray)(((Scriptable)scope.get("BCOV", scope)).get("collectedCoverageData", scope));
+ for(Object coverage : na2) {
+ generateBranchCoverage((Scriptable) coverage, scope, coverageResults2);
+ }
- Set<String> allFileNames = new HashSet<String>();
- allFileNames.addAll(coverageResults.keySet());
- allFileNames.addAll(coverageResults2.keySet());
- List<FileCoverage> result = new ArrayList<FileCoverage>();
- for(String fileName : allFileNames) {
- Map<Integer, LineCoverage> lineCoverage = coverageResults.get(fileName);
- Collection<BranchCoverage> branchCoverage = coverageResults2.get(fileName);
- result.add(new FileCoverage(fileName,
- lineCoverage == null ? Collections.<LineCoverage>emptySet() : lineCoverage.values(),
- branchCoverage == null ? Collections.<BranchCoverage>emptySet() : branchCoverage));
+ Set<String> allFileNames = new HashSet<String>();
+ allFileNames.addAll(coverageResults.keySet());
+ allFileNames.addAll(coverageResults2.keySet());
+ List<FileCoverage> result = new ArrayList<FileCoverage>();
+ for(String fileName : allFileNames) {
+ Map<Integer, LineCoverage> lineCoverage = coverageResults.get(fileName);
+ Collection<BranchCoverage> branchCoverage = coverageResults2.get(fileName);
+ result.add(new FileCoverage(fileName,
+ lineCoverage == null ? Collections.<LineCoverage>emptySet() : lineCoverage.values(),
+ branchCoverage == null ? Collections.<BranchCoverage>emptySet() : branchCoverage));
+ }
+ coverageData = new CoverageData(result);
}
- coverageData = new CoverageData(result);
}
private void generateLineCoverage(Scriptable coverage, Scriptable scope, Map<String, Map<Integer, LineCoverage>> coverageResults) {
View
2 src/main/com/olabini/jescov/CoverageRewriter.java
@@ -17,7 +17,7 @@ public CoverageRewriter(CoverageNameMapper nameMapper, Context context) {
public void rewrite(DebuggableScript input, String source) {
CodeInstrumentor ci = new CodeInstrumentor(nameMapper);
String ic = ci.instrument(input.getSourceName(), source);
- // System.err.println("New code: " + ic);
+ // System.err.println("New code: " + ic);
Script s = context.compileString(ic, input.getSourceName(), 0, null);
MozillaPackageProxy.copyInterpreterInternals(s, input);
}
View
29 src/main/com/olabini/jescov/es3/ES3Instrument.g
@@ -277,7 +277,7 @@ public Token nextToken()
@parser::members
{
-public java.util.Map<String, java.util.List<Integer>> linesMap = new java.util.HashMap<String, java.util.List<Integer>>();
+public java.util.Map<String, java.util.Collection<Integer>> linesMap = new java.util.HashMap<String, java.util.Collection<Integer>>();
private final boolean isLeftHandSideAssign(RuleReturnScope lhs, Object[] cached)
{
@@ -1252,12 +1252,7 @@ scope {
boolean isBlock;
}
@init{
- boolean instrument = false;
-
- if ($start.getLine() > $program::stopLine) {
- $program::stopLine = $start.getLine();
- instrument = true;
- }
+ boolean instrument = !$program::executableLines.contains($start.getLine());
}
@after {
if (instrument && !$statement::isBlock) {
@@ -1645,12 +1640,10 @@ finallyClause
functionDeclaration
@init{
- boolean instrument = false;
- if ($start.getLine() > $program::stopLine) {
- $program::executableLines.add($start.getLine());
- $program::stopLine = $start.getLine();
- instrument = true;
- }
+ boolean instrument = true;
+ if(instrument) {
+ $program::executableLines.add($start.getLine());
+ }
}
: FUNCTION name=Identifier formalParameterList functionBody
-> {instrument}? instrument(stmt={$text}, ln={$start.getLine()}, hash = {$program::hash})
@@ -1659,7 +1652,7 @@ functionDeclaration
functionExpression
: FUNCTION name=Identifier? formalParameterList functionBody
- // -> ( FUNCTION $name? formalParameterList functionBody )
+ -> pass(stmt={$text})
;
formalParameterList
@@ -1680,21 +1673,19 @@ program
scope {
String name;
String hash;
- java.util.List<Integer> executableLines;
+ java.util.Collection<Integer> executableLines;
java.util.LinkedList executableBranches;
- int stopLine;
int branches;
}
@init {
String name = getSourceName();
$program::name = name;
$program::hash = Integer.toString(Math.abs(name.hashCode()), Character.MAX_RADIX);
- $program::executableLines = new java.util.LinkedList();
+ $program::executableLines = new java.util.TreeSet();
$program::executableBranches = new java.util.LinkedList();
- $program::stopLine = 0;
$program::branches = 0;
}
- : (sourceElement*) {java.util.Collections.sort($program::executableLines); linesMap.put($program::name, $program::executableLines);}
+ : (sourceElement*) {linesMap.put($program::name, $program::executableLines);}
-> init_instrument(stmt = {$text}, hash = {$program::hash}, name = {name}, lines = {$program::executableLines.toString()}, branches = {$program::executableBranches.toString()})
;
View
4 src/main/com/olabini/jescov/generators/FilesAndRelatedInformation.java
@@ -28,6 +28,10 @@ public String getFilename() {
return this.filename;
}
+ public String getPhysicalFilename() {
+ return this.filename.replaceAll("/", ".");
+ }
+
public String getLinesCoveragePercent() {
return coveragePercent(coveredLines, validLines);
}
View
2 src/main/com/olabini/jescov/generators/HtmlGenerator.java
@@ -133,7 +133,7 @@ private void generateFile(final ST template, String file, FileCoverage fc) throw
}
r.close();
- intoFile(file + ".html", new WriteAction() {
+ intoFile(file.replaceAll("/", ".") + ".html", new WriteAction() {
public void write(Writer w) throws IOException {
template.write(new NoIndentWriter(w));
}
View
4 src/resources/templates/cobertura-html.stg
@@ -22,7 +22,7 @@ sourcefiles(fileAndCoverage) ::= <<
sourcefilesCoverage(fc) ::= <<
<tr>
- <td nowrap="nowrap"><a target="summary" href="%fc.filename%.html">%fc.filename%</a> <i>(%fc.linesCoveragePercent%)</i></td>
+ <td nowrap="nowrap"><a target="summary" href="%fc.physicalFilename%.html">%fc.filename%</a> <i>(%fc.linesCoveragePercent%)</i></td>
</tr>
>>
@@ -65,7 +65,7 @@ fileTable.sort(0);
summaryItem(fc) ::= <<
<tr>
<td>
- %if(fc.link)% <a href="%fc.filename%.html">%fc.filename%</a>
+ %if(fc.link)% <a href="%fc.physicalFilename%.html">%fc.filename%</a>
%else% <b>%fc.filename%</b>
%endif%
</td>

0 comments on commit b751c2d

Please sign in to comment.
Something went wrong with that request. Please try again.