Skip to content

Commit

Permalink
Version 0.1.4 y'all!
Browse files Browse the repository at this point in the history
  • Loading branch information
keeto committed Mar 18, 2010
1 parent 9cab8fc commit 33cd68e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 46 deletions.
28 changes: 21 additions & 7 deletions Source/lib/case.js
Expand Up @@ -34,11 +34,15 @@ var Case = function(desc, test, context, callback){
this.$test = test;
this.$context = context || {};

callback = callback || {};
this.$callbacks = {
before: function(){},
after: function(){}
};
callback = callback || {};
this.setCallbacks({
before: callback.before,
after: callback.after
};
});

this.$testCount = countExpect(test);
this.$doneCount = 0;
Expand All @@ -48,15 +52,28 @@ var Case = function(desc, test, context, callback){
this.$results = [];
};

Case.setMatcher = function(name, fn){
Expectation.setMatcher(name, fn);
};

Case.prototype.count = function(){
return this.$testCount;
};

Case.prototype.setCallback = function(type, fn){
if (fn === undefined || !(fn instanceof Function))
throw new Error('Case.setCallback requires a function as its second argument.');
this.$callbacks[type] = fn;
return this;
};

Case.prototype.setCallbacks = function(keys){
for (var key in keys){
if (keys[key] !== undefined && keys[key] instanceof Function) this.setCallback(key, keys[key]);
}
return this;
};

Case.prototype.done = function(){
return (!(this.$testCount - this.$doneCount) || this.$finished);
};
Expand All @@ -79,9 +96,7 @@ var addResult = function(results, success, done){
this.$results.push(results);
this.$doneCount++;
this[success ? '$passes' : '$failures']++;
if (this.done() && this.$callbacks.after instanceof Function){
this.$callbacks.after.call(this, this.results(), (this.$failures === 0));
}
if (this.done()) this.$callbacks.after.call(this, this.results(), (this.$failures === 0));
};

var expectCallback = function(){
Expand All @@ -103,8 +118,7 @@ Case.prototype.$expect = function(received){

Case.prototype.run = function(){
var self = this, error;
if (this.$callbacks.before instanceof Function)
this.$callbacks.before.call(self, this.desc, this.$testCount);
this.$callbacks.before.call(self, this.desc, this.$testCount);
try {
var expectProto = function(){
return self.$expect.apply(self, Array.prototype.slice.call(arguments));
Expand Down
2 changes: 1 addition & 1 deletion Source/lib/expectation.js
Expand Up @@ -36,7 +36,7 @@ var Expectation = function(value, callback, bound){
Expectation.setMatcher = function(name, func){
if (func === undefined || !(func instanceof Function))
throw new Error('Expectation.setMatcher requires a function as its second argument.');
this.prototype[matcher] = function(expected){
Expectation.prototype[name] = function(expected){
var result = func.call(null, this.$received, expected);
this.$callback.call(this.$bound, result, this.$received, expected, matcher);
return result;
Expand Down
61 changes: 41 additions & 20 deletions Source/lib/suite.js
Expand Up @@ -24,23 +24,49 @@ var Suite = function(name, body, callbacks){
this.$results = [];
this.$context = {x: 1};

callbacks = callbacks || {};
this.$callbacks = {
before: function(){},
beforeEach: function(){},
after: function(){},
afterEach: function(){}
};
callbacks = callbacks || {};
this.setCallbacks({
before: callbacks.before,
beforeEach: callbacks.beforeEach,
after: callbacks.after,
afterEach: callbacks.afterEach
};
});

this.$testCount = 0;
this.$doneCount = 0;
this.$passes = 0;
this.$failures = 0;

this.$before = function(){};
this.$beforeEach = function(){};
this.$after = function(){};
this.$afterEach = function(){};
this.$scaffolds = {
before: function(){},
beforeEach: function(){},
after: function(){},
afterEach: function(){}
};
};

Suite.setMatcher = function(name, fn){
Case.setMatcher(name, fn);
};

Suite.prototype.setCallback = function(type, fn){
if (fn === undefined || !(fn instanceof Function))
throw new Error('Suite.setCallback requires a function as its second argument.');
this.$callbacks[type] = fn;
return this;
};

Suite.prototype.setCallbacks = function(keys){
for (var key in keys){
if (keys[key] !== undefined && keys[key] instanceof Function) this.setCallback(key, keys[key]);
}
return this;
};

Suite.prototype.done = function(){
Expand All @@ -64,12 +90,11 @@ Suite.prototype.results = function(){
var callNext = function(){
var current = this.$tests.shift();
if (current){
this.$beforeEach.call(this.$context);
this.$scaffolds.beforeEach.call(this.$context);
current.run();
} else {
this.$after.call(this.$context);
if (this.$callbacks.after instanceof Function)
this.$callbacks.after.call(null, this.name, (this.$failures === 0), this.results());
this.$scaffolds.after.call(this.$context);
this.$callbacks.after.call(null, this.name, (this.$failures === 0), this.results());
}
};

Expand All @@ -79,10 +104,8 @@ var itCallback = function(){
self.$doneCount++;
self.$results.push(results);
self[success ? '$passes' : '$failures']++;
self.$afterEach.call(self.$context);
if (self.$callbacks.afterEach instanceof Function){
self.$callbacks.afterEach.call(null, self.name, results.description, results);
}
self.$scaffolds.afterEach.call(self.$context);
self.$callbacks.afterEach.call(null, self.name, results.description, results);
callNext.call(self);
};
};
Expand All @@ -92,16 +115,15 @@ Suite.prototype.$it = function(desc, fn){
this.$testCount++;
var test = new Case(desc, fn, this.$context, {
before: function(desc, count){
if (self.$callbacks.beforeEach instanceof Function)
self.$callbacks.beforeEach.call(null, self.name, desc, count);
self.$callbacks.beforeEach.call(null, self.name, desc, count);
},
after: itCallback.call(this)
});
this.$tests.push(test);
};

Suite.prototype.$setup = function(type, fn){
if (fn instanceof Function) this['$' + type] = fn;
if (fn !== undefined && fn instanceof Function) this.$scaffolds[type] = fn;
return this;
};

Expand All @@ -113,14 +135,13 @@ Suite.prototype.prepare = function(){
return self.$setup.apply(self, Array.prototype.slice.call(arguments));
});
this.$prepared = true;
if (this.$callbacks.before instanceof Function)
this.$callbacks.before.call(null, this.name, this.$testCount);
this.$callbacks.before.call(null, this.name, this.$testCount);
return this;
};

Suite.prototype.run = function(){
if (!this.$prepared) this.prepare();
this.$before.call(this.$context);
this.$scaffolds.before.call(this.$context);
callNext.call(this);
return this;
};
Expand Down
46 changes: 28 additions & 18 deletions Source/testigo.js
Expand Up @@ -24,26 +24,38 @@ var Testigo = function(callbacks){
this.$passes = 0;
this.$failures = 0;

callbacks = callbacks || {};
this.$callbacks = {
before: function(){},
beforeSuite: function(){},
beforeTest: function(){},
after: function(){},
afterSuite: function(){},
afterTest: function(){}
};
callbacks = callbacks || {};
this.setCallbacks({
before: callbacks.before,
beforeSuite: callbacks.beforeSuite,
beforeTest: callbacks.beforeTest,
after: callbacks.after,
afterSuite: callbacks.afterSuite,
afterTest: callbacks.afterTest
};
});
};

Testigo.prototype.setCallback = function(name, fn){
if (fn === undefined || !(fn instanceof Function))
throw new Error('Testigo.setCallback requires a function as its second argument.');
this.$callbacks[name] = fn;
return this;
};

Testigo.prototype.setCallbacks = function(keys){
for (var key in keys) this.setCallback(key, keys[key]);
for (var key in keys){
if (keys[key] !== undefined && keys[key] instanceof Function) this.setCallback(key, keys[key]);
}
return this;
}
};

Testigo.prototype.done = function(){
return !(this.$suiteCount - this.$suitesDone);
Expand All @@ -69,25 +81,21 @@ Testigo.prototype.describe = function(name, fn){
var suite = new Suite(name, fn, {
before: function(suite, count){
self.$testCount += count;
if (self.$callbacks.beforeSuite instanceof Function)
self.$callbacks.beforeSuite.call(null, suite, count);
self.$callbacks.beforeSuite.call(null, suite, count);
},
after: function(suite, success, results){
self.$suitesDone++;
self.$passes += results.tests.passes;
self.$failures += results.tests.failures;
self.results[suite] = results;
if (self.$callbacks.afterSuite instanceof Function)
self.$callbacks.afterSuite.call(null, suite, success, results);
self.$callbacks.afterSuite.call(null, suite, success, results);
callNext.call(self);
},
beforeEach: function(suite, test, count){
if (self.$callbacks.beforeTest instanceof Function)
self.$callbacks.beforeTest.call(null, suite, test, count);
self.$callbacks.beforeTest.call(null, suite, test, count);
},
afterEach: function(suite, test, results){
if (self.$callbacks.afterTest instanceof Function)
self.$callbacks.afterTest.call(null, suite, test, results);
self.$callbacks.afterTest.call(null, suite, test, results);
}
});
this.$suites.push(suite);
Expand All @@ -98,24 +106,26 @@ var callNext = function(){
if (current){
current.run();
} else {
if (this.$callbacks.after instanceof Function)
this.$callbacks.after.call(null, (this.$failures === 0), this.results());
this.$callbacks.after.call(null, (this.$failures === 0), this.results());
}
};

Testigo.prototype.run = function(){
if (this.$callbacks.before instanceof Function)
this.$callbacks.before.call(null, this.$suiteCount);
this.$callbacks.before.call(null, this.$suiteCount);
callNext.call(this);
};

Testigo.version = [0,1,2];
Testigo.versionText = "0.1.2";
Testigo.version = [0,1,4];
Testigo.versionText = "0.1.4";

Testigo.Runners = {
Simple: require('./runners/simple').SimpleRunner
};

Testigo.setMatcher = Testigo.prototype.setMatcher = function(name, fn){
Suite.setMatcher(name, fn);
};

exports.Testigo = Testigo;

})();

0 comments on commit 33cd68e

Please sign in to comment.