Skip to content

Commit

Permalink
- Made missing test suite names undefined instead of empty string
Browse files Browse the repository at this point in the history
  - 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
atesgoral committed Mar 24, 2009
1 parent 39cffb7 commit 8f22f48
Show file tree
Hide file tree
Showing 3 changed files with 348 additions and 95 deletions.
7 changes: 7 additions & 0 deletions jsunity/CHANGES.txt
Original file line number Original file line Diff line number Diff line change
@@ -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") - Test cases are run within the scope of the test suite (as "this")
- Fixed multiple test suite name concatenation to work with AppJet - Fixed multiple test suite name concatenation to work with AppJet


Expand Down
87 changes: 51 additions & 36 deletions jsunity/jsunity.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jsUnity = (function () {
} }


return { return {
name: tokens[1], name: tokens[1].length ? tokens[1] : undefined,
body: tokens[2] body: tokens[2]
}; };
} }
Expand All @@ -109,18 +109,18 @@ jsUnity = (function () {


var probeInside = new Function( var probeInside = new Function(
splitFunction(probeOutside).body + str); splitFunction(probeOutside).body + str);

var tokenRe = /(\w+)/g; // todo: wiser regex var tokenRe = /(\w+)/g; // todo: wiser regex
var tokens; var tokens;

while ((tokens = tokenRe.exec(str))) { while ((tokens = tokenRe.exec(str))) {
var token = tokens[1]; var token = tokens[1];
var fn; var fn;


if (!obj[token] if (!obj[token]
&& (fn = probeInside(token)) && (fn = probeInside(token))
&& !probeOutside(token)) { && fn != probeOutside(token)) {

obj[token] = fn; obj[token] = fn;
} }
} }
Expand Down Expand Up @@ -163,41 +163,42 @@ jsUnity = (function () {
} }


function parseSuiteObject(obj) { function parseSuiteObject(obj) {
var suite = { var suite = new jsUnity.TestSuite(obj.suiteName, obj);
suiteName: obj.suiteName,
tests: []
};


for (var name in obj) { for (var name in obj) {
var fn = obj[name]; if (obj.hasOwnProperty(name)) {

var fn = obj[name];
if (obj.hasOwnProperty(name) && typeof fn === "function") {
if (/^test/.test(name)) { if (typeof fn === "function") {
suite.tests.push({ name: name, fn: fn }); if (/^test/.test(name)) {
} else if (/^setUp|tearDown$/.test(name)) { suite.tests.push({ name: name, fn: fn });
suite[name] = fn; } else if (/^setUp|tearDown$/.test(name)) {
suite[name] = fn;
}
} }
} }
} }


return suite; 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 { 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, assertions: defaultAssertions,


env: { env: {
Expand All @@ -220,18 +221,32 @@ jsUnity = (function () {


error: function (s) { this.log("[ERROR] " + s); }, 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 () { run: function () {
var results = { var results = new jsUnity.TestResults();
total: 0,
passed: 0
};


var suiteNames = []; var suiteNames = [];
var start = jsUnity.env.getDate(); var start = jsUnity.env.getDate();


for (var i = 0; i < arguments.length; i++) { for (var i = 0; i < arguments.length; i++) {
try { try {
var suite = parseSuite(arguments[i]); var suite = jsUnity.compile(arguments[i]);
} catch (e) { } catch (e) {
this.error("Invalid test suite: " + e); this.error("Invalid test suite: " + e);
return false; return false;
Expand All @@ -251,7 +266,7 @@ jsUnity = (function () {


try { try {
suite.setUp && suite.setUp(); suite.setUp && suite.setUp();
test.fn.call(suite); test.fn.call(suite.scope);
suite.tearDown && suite.tearDown(); suite.tearDown && suite.tearDown();


results.passed++; results.passed++;
Expand Down
Loading

0 comments on commit 8f22f48

Please sign in to comment.