Permalink
Browse files

Move assertion defintions to own file

  • Loading branch information...
1 parent 779f9ce commit df1dbd10c80fff22d0c8c908a8bc975b299b623a @joelplane joelplane committed Jun 12, 2011
Showing with 52 additions and 0 deletions.
  1. +10 −0 assertion.js
  2. +16 −0 assertions.js
  3. +1 −0 noodleTest.js
  4. +25 −0 test.js
View
@@ -1,5 +1,6 @@
module.exports = (function(){
var sys = require('sys');
+ var events = require('./events');
function Assertion(testCase, assertMethod, args, tester, failureMessageFunction) {
this.testCase = testCase;
this.assertMethod = assertMethod;
@@ -23,5 +24,14 @@ module.exports = (function(){
Assertion.prototype.callString = function() {
return this.assertMethod;
};
+
+ events.classEvents(Assertion);
+ Assertion.assertions = [];
+ Assertion.addAssertion = function(methodName, assertionFunction, failureMessageFunction) {
+ var a = {methodName: methodName, assertionFunction: assertionFunction, failureMessageFunction: failureMessageFunction};
+ Assertion.assertions.push(a);
+ Assertion.emit('assertionAdded', a);
+ };
+
return Assertion;
})();
View
@@ -0,0 +1,16 @@
+module.exports = function(Assertion) {
+ var sys = require('sys');
+
+ Assertion.addAssertion('assert', function(condition) {
+ return condition;
+ }, function(condition) {
+ return "Expected first argument to evaluate to true";
+ });
+
+ Assertion.addAssertion('assertEqual', function(expected, actual) {
+ return expected == actual;
+ }, function(expected, actual) {
+ return "Expected " + sys.inspect(expected) + ", but got " + sys.inspect(actual);
+ });
+
+};
View
@@ -11,6 +11,7 @@ module.exports = (function(config){
var Assertion = require('./assertion');
var testQueue = require('./testQueue')();
var Test = require('./test')(Assertion, testQueue, config.timeout);
+ require('./assertions')(Assertion);
var Context = require('./context')(Test);
var main = new EventEmitter();
View
25 test.js
@@ -30,6 +30,7 @@ module.exports = (function(Assertion, testQueue, timeout){
this.flunk(error.toString(), {error: error, test: this});
}
};
+ /*
Test.prototype.assert = function(condition) {
var a = new Assertion(this, 'assert', [condition], function() {
return condition;
@@ -60,9 +61,33 @@ module.exports = (function(Assertion, testQueue, timeout){
this.emit('assertionFailed', {context: this.context});
}
};
+ */
Test.prototype.flunk = function(message, options) {
options = options ? options : {};
this.emit('testFlunk', {context: this.context, message: message, options: options});
};
+
+ Assertion.on('assertionAdded', function(definition){
+ var methodName = definition['methodName'];
+ var assertionFunction = definition['assertionFunction'];
+ var failureMessageFunction = definition['failureMessageFunction'];
+ Test.prototype[methodName] = function() {
+ var args = arguments;
+ var a = new Assertion(this, methodName, args, function() {
+ return assertionFunction.apply(this, args);
+ }, function() {
+ return failureMessageFunction.apply(this, args);
+ });
+ a.execute();
+ if (a.passed) {
+ this.passes.push(a);
+ this.emit('assertionPassed', {context: this.context});
+ } else {
+ this.failures.push(a);
+ this.emit('assertionFailed', {context: this.context});
+ }
+ }
+ });
+
return Test;
});

0 comments on commit df1dbd1

Please sign in to comment.