-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for coverage instrumenter
- Loading branch information
Nicholas
committed
Mar 15, 2011
1 parent
6615b89
commit cc43e85
Showing
12 changed files
with
318 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
java/tests/com/yahoo/platform/yuitest/coverage/CoverageInstrumentationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* YUI Test | ||
* Author: Nicholas C. Zakas <nzakas@yahoo-inc.com> | ||
* Copyright (c) 2009, Yahoo! Inc. All rights reserved. | ||
* Code licensed under the BSD License: | ||
* http://developer.yahoo.net/yui/license.txt | ||
*/ | ||
|
||
package com.yahoo.platform.yuitest.coverage; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.io.StringReader; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.antlr.runtime.RecognitionException; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* This test case tests whether instrumentation is working correctly for various | ||
* scenarios. The tests are done by comparing plain-text files against the actual | ||
* output of the instrumenter. | ||
* @author nzakas | ||
*/ | ||
public class CoverageInstrumentationTest { | ||
|
||
BufferedReader actual; | ||
BufferedReader expected; | ||
|
||
public CoverageInstrumentationTest() { | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
if (actual != null){ | ||
actual.close(); | ||
} | ||
if (expected != null){ | ||
expected.close(); | ||
} | ||
} | ||
|
||
private String getFileContents(String filename) throws IOException { | ||
Reader in = new InputStreamReader(CoverageInstrumentationTest.class.getResourceAsStream(filename)); | ||
StringBuilder builder = new StringBuilder(); | ||
int c; | ||
|
||
while((c = in.read()) != -1){ | ||
builder.append((char)c); | ||
} | ||
|
||
in.close(); | ||
return builder.toString(); | ||
} | ||
|
||
private void assertResultsMatch(String sourceFilename, String resultFilename) throws IOException{ | ||
Reader in = new InputStreamReader(CoverageInstrumentationTest.class.getResourceAsStream(sourceFilename)); | ||
Writer out = new StringWriter(); | ||
JavaScriptInstrumenter instrumenter = new JavaScriptInstrumenter(in, sourceFilename); | ||
|
||
try { | ||
instrumenter.instrument(out, false); | ||
} catch (RecognitionException ex){ | ||
throw new RuntimeException(ex); | ||
} | ||
|
||
String actualResult = out.toString(); | ||
String expectedResult = getFileContents(resultFilename); | ||
|
||
actual = new BufferedReader(new StringReader(actualResult)); | ||
expected = new BufferedReader(new StringReader(expectedResult)); | ||
String actualLine, expectedLine; | ||
|
||
int i=0; | ||
while((actualLine = actual.readLine()) != null){ | ||
i++; | ||
expectedLine = expected.readLine(); | ||
assertEquals("Line " + i + " doesn't match", expectedLine, actualLine); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testArrayItem() throws IOException { | ||
assertResultsMatch("array-item.js", "array-item-covered.js"); | ||
} | ||
|
||
@Test | ||
public void testEmptyFile() throws IOException { | ||
assertResultsMatch("empty.js", "empty-covered.js"); | ||
} | ||
|
||
@Test | ||
public void testStringProperty() throws IOException { | ||
assertResultsMatch("string-property.js", "string-property-covered.js"); | ||
} | ||
|
||
@Test | ||
public void testNoBraces() throws IOException { | ||
assertResultsMatch("nobraces.js", "nobraces-covered.js"); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
java/tests/com/yahoo/platform/yuitest/coverage/array-item-covered.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
if (typeof _yuitest_coverage == "undefined"){ | ||
_yuitest_coverage = {}; | ||
_yuitest_coverline = function(src, line){ | ||
var coverage = _yuitest_coverage[src]; | ||
if (!coverage.lines[line]){ | ||
coverage.calledLines++; | ||
} | ||
coverage.lines[line]++; | ||
}; | ||
_yuitest_coverfunc = function(src, name, line){ | ||
var coverage = _yuitest_coverage[src], | ||
funcId = name + ":" + line; | ||
if (!coverage.functions[funcId]){ | ||
coverage.calledFunctions++; | ||
} | ||
coverage.functions[funcId]++; | ||
}; | ||
} | ||
_yuitest_coverage["array-item.js"] = { | ||
lines: {}, | ||
functions: {}, | ||
coveredLines: 0, | ||
calledLines: 0, | ||
coveredFunctions: 0, | ||
calledFunctions: 0, | ||
path: "array-item.js", | ||
code: [] | ||
}; | ||
_yuitest_coverage["array-item.js"].code=["var foo = [",""," hello","","];"]; | ||
_yuitest_coverage["array-item.js"].lines = {"1":0}; | ||
_yuitest_coverage["array-item.js"].functions = {}; | ||
_yuitest_coverage["array-item.js"].coveredLines = 1; | ||
_yuitest_coverage["array-item.js"].coveredFunctions = 0; | ||
_yuitest_coverline("array-item.js", 1); var foo = [ | ||
|
||
hello | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var foo = [ | ||
|
||
hello | ||
|
||
]; |
29 changes: 29 additions & 0 deletions
29
java/tests/com/yahoo/platform/yuitest/coverage/empty-covered.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
if (typeof _yuitest_coverage == "undefined"){ | ||
_yuitest_coverage = {}; | ||
_yuitest_coverline = function(src, line){ | ||
var coverage = _yuitest_coverage[src]; | ||
if (!coverage.lines[line]){ | ||
coverage.calledLines++; | ||
} | ||
coverage.lines[line]++; | ||
}; | ||
_yuitest_coverfunc = function(src, name, line){ | ||
var coverage = _yuitest_coverage[src], | ||
funcId = name + ":" + line; | ||
if (!coverage.functions[funcId]){ | ||
coverage.calledFunctions++; | ||
} | ||
coverage.functions[funcId]++; | ||
}; | ||
} | ||
_yuitest_coverage["empty.js"] = { | ||
lines: {}, | ||
functions: {}, | ||
coveredLines: 0, | ||
calledLines: 0, | ||
coveredFunctions: 0, | ||
calledFunctions: 0, | ||
path: "empty.js", | ||
code: [] | ||
}; | ||
_yuitest_coverage["empty.js"].code=[]; |
Empty file.
46 changes: 46 additions & 0 deletions
46
java/tests/com/yahoo/platform/yuitest/coverage/nobraces-covered.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
if (typeof _yuitest_coverage == "undefined"){ | ||
_yuitest_coverage = {}; | ||
_yuitest_coverline = function(src, line){ | ||
var coverage = _yuitest_coverage[src]; | ||
if (!coverage.lines[line]){ | ||
coverage.calledLines++; | ||
} | ||
coverage.lines[line]++; | ||
}; | ||
_yuitest_coverfunc = function(src, name, line){ | ||
var coverage = _yuitest_coverage[src], | ||
funcId = name + ":" + line; | ||
if (!coverage.functions[funcId]){ | ||
coverage.calledFunctions++; | ||
} | ||
coverage.functions[funcId]++; | ||
}; | ||
} | ||
_yuitest_coverage["nobraces.js"] = { | ||
lines: {}, | ||
functions: {}, | ||
coveredLines: 0, | ||
calledLines: 0, | ||
coveredFunctions: 0, | ||
calledFunctions: 0, | ||
path: "nobraces.js", | ||
code: [] | ||
}; | ||
_yuitest_coverage["nobraces.js"].code=["function test(){",""," if(o)"," for(var prop in o)"," doSomething();","",""," for(var x=initX;x<str_arr.length;x++)"," str_arr[x]=str_arr[x].charAt(0).toUpperCase()+str_arr[x].substring(1);","","","}"]; | ||
_yuitest_coverage["nobraces.js"].lines = {"1":0,"3":0,"4":0,"5":0,"8":0,"9":0}; | ||
_yuitest_coverage["nobraces.js"].functions = {"test:1":0}; | ||
_yuitest_coverage["nobraces.js"].coveredLines = 6; | ||
_yuitest_coverage["nobraces.js"].coveredFunctions = 1; | ||
_yuitest_coverline("nobraces.js", 1); function test(){ | ||
|
||
_yuitest_coverfunc("nobraces.js", "test", 1); | ||
_yuitest_coverline("nobraces.js", 3); if(o) | ||
{_yuitest_coverline("nobraces.js", 4); for(var prop in o) | ||
{_yuitest_coverline("nobraces.js", 5); doSomething();}} | ||
|
||
|
||
_yuitest_coverline("nobraces.js", 8); for(var x=initX;x<str_arr.length;x++) | ||
{_yuitest_coverline("nobraces.js", 9); str_arr[x]=str_arr[x].charAt(0).toUpperCase()+str_arr[x].substring(1);} | ||
|
||
|
||
} |
12 changes: 12 additions & 0 deletions
12
java/tests/com/yahoo/platform/yuitest/coverage/nobraces.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function test(){ | ||
|
||
if(o) | ||
for(var prop in o) | ||
doSomething(); | ||
|
||
|
||
for(var x=initX;x<str_arr.length;x++) | ||
str_arr[x]=str_arr[x].charAt(0).toUpperCase()+str_arr[x].substring(1); | ||
|
||
|
||
} |
51 changes: 51 additions & 0 deletions
51
java/tests/com/yahoo/platform/yuitest/coverage/string-property-covered.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
if (typeof _yuitest_coverage == "undefined"){ | ||
_yuitest_coverage = {}; | ||
_yuitest_coverline = function(src, line){ | ||
var coverage = _yuitest_coverage[src]; | ||
if (!coverage.lines[line]){ | ||
coverage.calledLines++; | ||
} | ||
coverage.lines[line]++; | ||
}; | ||
_yuitest_coverfunc = function(src, name, line){ | ||
var coverage = _yuitest_coverage[src], | ||
funcId = name + ":" + line; | ||
if (!coverage.functions[funcId]){ | ||
coverage.calledFunctions++; | ||
} | ||
coverage.functions[funcId]++; | ||
}; | ||
} | ||
_yuitest_coverage["string-property.js"] = { | ||
lines: {}, | ||
functions: {}, | ||
coveredLines: 0, | ||
calledLines: 0, | ||
coveredFunctions: 0, | ||
calledFunctions: 0, | ||
path: "string-property.js", | ||
code: [] | ||
}; | ||
_yuitest_coverage["string-property.js"].code=["var foo = {"," "," \"test\": function(){"," alert(\"hello\");"," },"," "," foo: function(){"," alert(\"goodbye\");"," },"," "," 'bar': function(){"," alert(\"Woohoo\");"," }","","};"]; | ||
_yuitest_coverage["string-property.js"].lines = {"1":0,"4":0,"8":0,"12":0}; | ||
_yuitest_coverage["string-property.js"].functions = {"\"test\":3":0,"foo:7":0,"\'bar\':11":0}; | ||
_yuitest_coverage["string-property.js"].coveredLines = 4; | ||
_yuitest_coverage["string-property.js"].coveredFunctions = 3; | ||
_yuitest_coverline("string-property.js", 1); var foo = { | ||
|
||
"test": function(){ | ||
_yuitest_coverfunc("string-property.js", "\"test\"", 3); | ||
_yuitest_coverline("string-property.js", 4); alert("hello"); | ||
}, | ||
|
||
foo: function(){ | ||
_yuitest_coverfunc("string-property.js", "foo", 7); | ||
_yuitest_coverline("string-property.js", 8); alert("goodbye"); | ||
}, | ||
|
||
'bar': function(){ | ||
_yuitest_coverfunc("string-property.js", "\'bar\'", 11); | ||
_yuitest_coverline("string-property.js", 12); alert("Woohoo"); | ||
} | ||
|
||
}; |
15 changes: 15 additions & 0 deletions
15
java/tests/com/yahoo/platform/yuitest/coverage/string-property.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
var foo = { | ||
|
||
"test": function(){ | ||
alert("hello"); | ||
}, | ||
|
||
foo: function(){ | ||
alert("goodbye"); | ||
}, | ||
|
||
'bar': function(){ | ||
alert("Woohoo"); | ||
} | ||
|
||
}; |