Permalink
Browse files

- Made missing test suite names undefined instead of empty string

  - Fixed function probing logic so that an outer function doesn't shield
    an inner function with the same name
  - Added compile
  - Introduced TestSuite and TestResults classes
  - Fixed test suite scope to use the object for object test suites instead
    of the generated test suite object
  • Loading branch information...
1 parent 39cffb7 commit 8f22f482f743d984cfcddd834546ba20c5c9b0eb @atesgoral committed Mar 24, 2009
Showing with 348 additions and 95 deletions.
  1. +7 −0 jsunity/CHANGES.txt
  2. +51 −36 jsunity/jsunity.js
  3. +290 −59 jsunity/test/coreTestSuite.js
View
@@ -1,3 +1,10 @@
+ - Made missing test suite names undefined instead of empty string
+ - Fixed function probing logic so that an outer function doesn't shield
+ an inner function with the same name
+ - Added compile
+ - Introduced TestSuite and TestResults classes
+ - Fixed test suite scope to use the object for object test suites instead
+ of the generated test suite object
- Test cases are run within the scope of the test suite (as "this")
- Fixed multiple test suite name concatenation to work with AppJet
View
@@ -90,7 +90,7 @@ jsUnity = (function () {
}
return {
- name: tokens[1],
+ name: tokens[1].length ? tokens[1] : undefined,
body: tokens[2]
};
}
@@ -109,18 +109,18 @@ jsUnity = (function () {
var probeInside = new Function(
splitFunction(probeOutside).body + str);
-
+
var tokenRe = /(\w+)/g; // todo: wiser regex
var tokens;
-
+
while ((tokens = tokenRe.exec(str))) {
var token = tokens[1];
var fn;
if (!obj[token]
&& (fn = probeInside(token))
- && !probeOutside(token)) {
-
+ && fn != probeOutside(token)) {
+
obj[token] = fn;
}
}
@@ -163,41 +163,42 @@ jsUnity = (function () {
}
function parseSuiteObject(obj) {
- var suite = {
- suiteName: obj.suiteName,
- tests: []
- };
+ var suite = new jsUnity.TestSuite(obj.suiteName, obj);
for (var name in obj) {
- var fn = obj[name];
-
- if (obj.hasOwnProperty(name) && typeof fn === "function") {
- if (/^test/.test(name)) {
- suite.tests.push({ name: name, fn: fn });
- } else if (/^setUp|tearDown$/.test(name)) {
- suite[name] = fn;
+ if (obj.hasOwnProperty(name)) {
+ var fn = obj[name];
+
+ if (typeof fn === "function") {
+ if (/^test/.test(name)) {
+ suite.tests.push({ name: name, fn: fn });
+ } else if (/^setUp|tearDown$/.test(name)) {
+ suite[name] = fn;
+ }
}
}
}
return suite;
}
- function parseSuite(v) {
- if (v instanceof Function) {
- return parseSuiteFunction(v);
- } else if (v instanceof Array) {
- return parseSuiteArray(v);
- } else if (v instanceof Object) {
- return parseSuiteObject(v);
- } else if (typeof v === "string") {
- return parseSuiteString(v);
- } else {
- throw "Must be a function, array, object or string.";
- }
- }
-
return {
+ TestSuite: function (suiteName, scope) {
+ this.suiteName = suiteName;
+ this.scope = scope;
+ this.tests = [];
+ this.setUp = undefined;
+ this.tearDown = undefined;
+ },
+
+ TestResults: function () {
+ this.suiteName = undefined;
+ this.total = 0;
+ this.passed = 0;
+ this.failed = 0;
+ this.duration = 0;
+ },
+
assertions: defaultAssertions,
env: {
@@ -220,18 +221,32 @@ jsUnity = (function () {
error: function (s) { this.log("[ERROR] " + s); },
+ compile: function (v) {
+ if (v instanceof jsUnity.TestSuite) {
+ return v;
+ } else if (v instanceof Function) {
+ return parseSuiteFunction(v);
+ } else if (v instanceof Array) {
+ return parseSuiteArray(v);
+ } else if (v instanceof Object) {
+ return parseSuiteObject(v);
+ } else if (typeof v === "string") {
+ return parseSuiteString(v);
+ } else {
+ throw "Argument must be a function, array, object, string or "
+ + "TestSuite instance.";
+ }
+ },
+
run: function () {
- var results = {
- total: 0,
- passed: 0
- };
+ var results = new jsUnity.TestResults();
var suiteNames = [];
var start = jsUnity.env.getDate();
for (var i = 0; i < arguments.length; i++) {
try {
- var suite = parseSuite(arguments[i]);
+ var suite = jsUnity.compile(arguments[i]);
} catch (e) {
this.error("Invalid test suite: " + e);
return false;
@@ -251,7 +266,7 @@ jsUnity = (function () {
try {
suite.setUp && suite.setUp();
- test.fn.call(suite);
+ test.fn.call(suite.scope);
suite.tearDown && suite.tearDown();
results.passed++;
Oops, something went wrong.

0 comments on commit 8f22f48

Please sign in to comment.