Permalink
Browse files

Merge branch 'master' of yuisource.corp.yahoo.com:yuitest

  • Loading branch information...
2 parents 2417360 + cc43e85 commit 49202778dd75223b7f34ced4fe3b3366745b0047 nzakas committed Mar 15, 2011
View
@@ -1,4 +1,7 @@
nbproject/
tmp/
+_tmp/
+_tests/
src/com/yahoo/platform/yuitest/coverage/ES3YUITestLexer.java
src/com/yahoo/platform/yuitest/coverage/ES3YUITestParser.java
+junit*.properties
View
@@ -10,7 +10,7 @@ tmp.dir = tmp
tests.dir = tests
#Version information
-version.number = 0.6.4
+version.number = 0.6.6
#Code paths
codepath.root.dir = com/yahoo/platform/yuitest
View
@@ -46,6 +46,11 @@
<pathelement location="${lib.dir}/${selenium.jar}"/>
</classpath>
</javac>
+
+ <!-- copy over XML/JSON/JS files for testing-->
+ <copy todir="${tmp.dir}/classes/${codepath.root.dir}">
+ <fileset dir="${src.dir}/${codepath.root.dir}" includes="**/*.stg"/>
+ </copy>
</target>
@@ -166,10 +171,11 @@
</classpath>
</javac>
- <!-- copy over XML/JSON files for testing-->
+ <!-- copy over XML/JSON/JS files for testing-->
<copy todir="${tmp.dir}/testclasses/${codepath.root.dir}">
<fileset dir="${tests.dir}/${codepath.root.dir}" includes="**/*.xml"/>
<fileset dir="${tests.dir}/${codepath.root.dir}" includes="**/*.json"/>
+ <fileset dir="${tests.dir}/${codepath.root.dir}" includes="**/*.js"/>
</copy>
</target>
@@ -183,6 +189,7 @@
<pathelement location="${tmp.dir}/classes"/>
<pathelement location="${tmp.dir}/testclasses"/>
<pathelement location="${lib.dir}/${jargs.jar}"/>
+ <pathelement location="${lib.dir}/${antlr.jar}"/>
<pathelement location="${lib.dir}/${selenium.jar}"/>
<pathelement location="${lib.dir}/${junit.jar}"/>
</classpath>
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -58,14 +58,21 @@ private void createFileLines() throws JSONException {
*/
private void createFileFunctions() throws JSONException {
JSONObject functionData = report1.getJSONObject("functions");
+
String[] keys = JSONObject.getNames(functionData);
- functions = new FileFunction[keys.length];
+
+ //keys is null when there are no instrumented functions
+ if (keys != null){
+ functions = new FileFunction[keys.length];
- for (int i=0; i < keys.length; i++){
- functions[i] = new FileFunction(keys[i], functionData.optInt(keys[i], -1));
- }
+ for (int i=0; i < keys.length; i++){
+ functions[i] = new FileFunction(keys[i], functionData.optInt(keys[i], -1));
+ }
- Arrays.sort(functions, new FileFunctionComparator());
+ Arrays.sort(functions, new FileFunctionComparator());
+ } else {
+ functions = new FileFunction[0];
+ }
}
/**
@@ -70,16 +70,28 @@ public SummaryCoverageReport(File[] files) throws IOException, JSONException {
public SummaryCoverageReport(Reader in) throws IOException, JSONException {
StringBuilder builder = new StringBuilder();
int c;
-
+
while((c = in.read()) != -1){
builder.append((char)c);
}
-
+
this.data = new JSONObject(builder.toString());
this.directories = new HashMap<String,DirectoryCoverageReport>();
generateFileReports();
}
-
+
+ /**
+ * Creates a new report object from a reader.
+ * @param in The reader containing JSON information.
+ * @throws java.io.IOException
+ * @throws org.json.JSONException
+ */
+ public SummaryCoverageReport(String input) throws IOException, JSONException {
+ this.data = new JSONObject(input);
+ this.directories = new HashMap<String,DirectoryCoverageReport>();
+ generateFileReports();
+ }
+
/**
* Creates a new report object from a JSON object.
* @param data The JSON object containing coverage data.
@@ -387,7 +387,7 @@ private SessionResult runTestPage(Selenium selenium, String browser,
SummaryCoverageReport coverageReport = null;
if (coverageResults != null){
- coverageReport = new SummaryCoverageReport(new StringReader(coverageResults));
+ coverageReport = new SummaryCoverageReport(coverageResults);
result.setCoverageReport(coverageReport);
}
@@ -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");
+ }
+
+}
@@ -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
+
+];
@@ -0,0 +1,5 @@
+var foo = [
+
+ hello
+
+];
@@ -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=[];
@@ -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);}
+
+
+}
@@ -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);
+
+
+}
Oops, something went wrong.

0 comments on commit 4920277

Please sign in to comment.