Permalink
Browse files

- Added results stream and wired it to log and TAP streams.

  - Added TAP 13 stream.
  - Added log stream with error, warn, info and debug levels.
  • Loading branch information...
1 parent 25e9de7 commit 605550f9f0b1a041e3bf4255bb812f01e9e63536 @atesgoral committed Jun 2, 2009
Showing with 136 additions and 53 deletions.
  1. +3 −0 jsunity/CHANGES.txt
  2. +71 −14 jsunity/jsunity.js
  3. +36 −38 jsunity/test/core.js
  4. +10 −1 jsunity/test/run.html
  5. +16 −0 jsunity/test/run_tap.html
View
@@ -1,3 +1,6 @@
+ - Added results stream and wired it to log and TAP streams.
+ - Added TAP 13 stream.
+ - Added log stream with error, warn, info and debug levels.
- Pass test name to test function and setUp/tearDown
- Fixed failing test case tearDown scope
- Removed "invalid function" exception that doesn't seem to be possible, for
View
@@ -276,6 +276,66 @@ jsUnity = (function () {
return suite;
}
+ var logLevels = {
+ error: 1,
+ warn: 2,
+ info: 3,
+ debug: 4
+ };
+
+ var logStream = {
+ write: empty,
+ level: "info"
+ };
+
+ for (var level in logLevels) {
+ logStream[level] = (function () {
+ var strLevel = level;
+ var numLevel = logLevels[level];
+
+ return function (s) {
+ if (numLevel >= logLevels[this.level] ) {
+ this.write(s, strLevel);
+ }
+ };
+ })();
+ }
+
+ var tapStream = {
+ write: empty
+ };
+
+ var resultsStream = {
+ begin: function (total, suiteName) {
+ jsUnity.tap.write("TAP version 13");
+ jsUnity.tap.write("# " + suiteName);
+ jsUnity.tap.write("1.." + total);
+
+ jsUnity.log.info("Running "
+ + (suiteName || "unnamed test suite"));
+ jsUnity.log.info(plural(total, "test") + " found");
+ },
+
+ pass: function (index, testName) {
+ jsUnity.tap.write(fmt("ok ? - ?", index, testName));
+ jsUnity.log.info("[PASSED] " + testName);
+ },
+
+ fail: function (index, testName, message) {
+ jsUnity.tap.write(fmt("not ok ? - ?", index, testName));
+ jsUnity.tap.write(" ---");
+ jsUnity.tap.write(" " + message);
+ jsUnity.tap.write(" ...");
+ jsUnity.log.info(fmt("[FAILED] ?: ?", testName, message));
+ },
+
+ end: function (passed, failed, duration) {
+ jsUnity.log.info(plural(passed, "test") + " passed");
+ jsUnity.log.info(plural(failed, "test") + " failed");
+ jsUnity.log.info(plural(duration, "millisecond") + " elapsed");
+ }
+ };
+
return {
TestSuite: function (suiteName, scope) {
this.suiteName = suiteName;
@@ -311,9 +371,9 @@ jsUnity = (function () {
}
},
- log: empty,
-
- error: function (s) { this.log("[ERROR] " + s); },
+ results: resultsStream,
+ log: logStream,
+ tap: tapStream,
compile: function (v) {
if (v instanceof jsUnity.TestSuite) {
@@ -342,15 +402,14 @@ jsUnity = (function () {
try {
var suite = jsUnity.compile(arguments[i]);
} catch (e) {
- this.error("Invalid test suite: " + e);
+ this.log.error("Invalid test suite: " + e);
return false;
}
var cnt = suite.tests.length;
- this.log("Running "
- + (suite.suiteName || "unnamed test suite"));
- this.log(plural(cnt, "test") + " found");
+ this.results.begin(cnt, suite.suiteName);
+ // when running multiple suites, report counts at end?
suiteNames.push(suite.suiteName);
results.total += cnt;
@@ -376,13 +435,13 @@ jsUnity = (function () {
test.fn.call(suite.scope, test.name);
tearDown(test.name);
- results.passed++;
+ this.results.pass(j + 1, test.name);
- this.log("[PASSED] " + test.name);
+ results.passed++;
} catch (e) {
- tearDown(test.name);
+ tearDown(test.name); // if tearDown above throws exc, will call again!
- this.log("[FAILED] " + test.name + ": " + e);
+ this.results.fail(j + 1, test.name, e);
}
}
}
@@ -391,9 +450,7 @@ jsUnity = (function () {
results.failed = results.total - results.passed;
results.duration = jsUnity.env.getDate() - start;
- this.log(plural(results.passed, "test") + " passed");
- this.log(plural(results.failed, "test") + " failed");
- this.log(plural(results.duration, "millisecond") + " elapsed");
+ this.results.end(results.passed, results.failed, results.duration);
return results;
}
View
@@ -7,9 +7,7 @@ function testGlobalFail() { throw "fail"; }
var global = {
setUp: setUp,
- tearDown: tearDown,
- setUps: 0,
- tearDowns: 0
+ tearDown: tearDown
};
var a = {};
@@ -34,16 +32,32 @@ function CoreTestSuite() {
}
function setUp() {
- origLog = jsUnity.log;
- jsUnity.log = function () {};
-
global.setUps = 0;
global.tearDowns = 0;
+
+ var sandbox = {
+ dateGetter: jsUnity.env.getDate,
+ logWriter: jsUnity.log.write,
+ tapWriter: jsUnity.tap.write,
+ logData: [],
+ tapData: [],
+ };
+
+ jsUnity.env.getDate = function () { return 0; }
+ jsUnity.log.write = function () {
+ sandbox.logData.push(Array.prototype.slice.call(arguments, 0));
+ };
+ jsUnity.tap.write = function () {
+ sandbox.tapData.push(arguments[0]);
+ };
+
+ this.sandbox = sandbox;
}
function tearDown() {
- jsUnity.log = origLog;
- delete origLog;
+ jsUnity.env.getDate = this.sandbox.dateGetter;
+ jsUnity.log.write = this.sandbox.logWriter;
+ jsUnity.tap.write = this.sandbox.tapWriter;
}
function testSetUpTearDownCalledPassing() {
@@ -121,48 +135,32 @@ function CoreTestSuite() {
}
function testLogCalled() {
- var hijackedLog = jsUnity.log;
- var hijackedGetDate = jsUnity.env.getDate;
-
- var logStrs = [];
-
- jsUnity.log = function (s) {
- logStrs.push(s);
- };
-
- jsUnity.env.getDate = function () {
- return 0;
- };
-
var results = jsUnity.run(function () {});
- jsUnity.log = hijackedLog;
- jsUnity.env.getDate = hijackedGetDate;
-
- a.assertIdentical(
- "Running unnamed test suite\n"
- + "0 tests found\n"
- + "0 tests passed\n"
- + "0 tests failed\n"
- + "0 milliseconds elapsed",
- logStrs.join("\n"));
+ a.assertEqual(
+ [
+ [ "Running unnamed test suite", "info" ],
+ [ "0 tests found", "info" ],
+ [ "0 tests passed", "info" ],
+ [ "0 tests failed", "info" ],
+ [ "0 milliseconds elapsed", "info" ]
+ ],
+ this.sandbox.logData);
}
function testErrorCalled() {
- var hijackedError = jsUnity.error;
+ var hijackedError = jsUnity.log.error;
- var errorStr;
+ var errorCalled = false;
- jsUnity.error = function (s) {
- errorStr = s;
- };
+ jsUnity.log.error = function () { errorCalled = true; };
var results = jsUnity.run(false);
- jsUnity.error = hijackedError;
+ jsUnity.log.error = hijackedError;
a.assertIdentical(false, results);
- a.assertTrue(errorStr.length > 0);
+ a.assertTrue(errorCalled);
}
function testAttachAssertionsDefaultScope() {
View
@@ -4,10 +4,19 @@
<script type="text/javascript" src="../jsunity.js"></script>
<script type="text/javascript" src="assertion.js"></script>
<script type="text/javascript" src="core.js"></script>
+ <style type="text/css">
+ .level_error { color: red }
+ .level_warn { color: orange }
+ .level_info { color: black }
+ .level_debug { color: darkgray }
+ </style>
</head>
<body>
<script type="text/javascript">
- jsUnity.log = function (s) { document.write("<div>" + s + "</div>"); }
+ jsUnity.log.write = function (s, level) {
+ document.write("<div class=\"level_" + level + "\">["
+ + level.toUpperCase() + "] " + s + "</div>");
+ };
jsUnity.run(AssertionTestSuite, CoreTestSuite);
</script>
</body>
View
@@ -0,0 +1,16 @@
+<html>
+<head>
+ <title>Core + Assertion Test Suite Runner</title>
+ <script type="text/javascript" src="../jsunity.js"></script>
+ <script type="text/javascript" src="assertion.js"></script>
+ <script type="text/javascript" src="core.js"></script>
+</head>
+<body>
+ <script type="text/javascript">
+ jsUnity.tap.write = function (s) {
+ document.write("<div>" + s + "</div>");
+ };
+ jsUnity.run(AssertionTestSuite, CoreTestSuite);
+ </script>
+</body>
+</html>

0 comments on commit 605550f

Please sign in to comment.