Permalink
Browse files

cache results of require('noodle-test')()

  • Loading branch information...
1 parent e924042 commit 7bc5d06a00af7ceafbc32ae9e68790a7f041cd8d @joelplane joelplane committed Jun 14, 2011
Showing with 104 additions and 42 deletions.
  1. +23 −1 assertions.js
  2. +54 −35 noodleTest.js
  3. +10 −6 system.js
  4. +17 −0 tests/noodleTestTest.js
View
@@ -1,16 +1,38 @@
module.exports = function(Assertion) {
var sys = require('sys');
+ Assertion._valueForOutput = function(value) {
+ if (value === null) {
+ return 'null';
+ }
+ if (typeof value == 'object') {
+ return value.toString();
+ }
+ return sys.inspect(value);
+ };
+
Assertion.addAssertion('assert', function(condition) {
return condition;
}, function(condition) {
return "Expected first argument to evaluate to true";
});
+ Assertion.addAssertion('assertFalse', function(condition) {
+ return !condition;
+ }, function(condition) {
+ return "Expected first argument to evaluate to false";
+ });
+
Assertion.addAssertion('assertEqual', function(expected, actual) {
return expected == actual;
}, function(expected, actual) {
- return "Expected " + sys.inspect(expected) + ", but got " + sys.inspect(actual);
+ return "Expected " + Assertion._valueForOutput(expected) + ", but got " + Assertion._valueForOutput(actual);
+ });
+
+ Assertion.addAssertion('assertNotEqual', function(notExpected, actual) {
+ return notExpected != actual;
+ }, function(notExpected, actual) {
+ return "Expected not " + Assertion._valueForOutput(notExpected) + ", but it was.";
});
var arraysEqual = function(a, b) {
View
@@ -1,42 +1,61 @@
/* ansi-color */
-module.exports = (function(config){
- if (!config) config = {};
- if (!config.timeout) config.timeout = 4000;
+module.exports = (function(){
+ var hashConfig = function(config) {
+ var keys = Object.keys(config).sort();
+ var pairs = [];
+ for(var i=0; i<keys.length;i++) {
+ var key = keys[i];
+ if (config.hasOwnProperty(key)) {
+ var string = key + '=' + config[key];
+ pairs.push(string);
+ }
+ }
+ return pairs.join(';');
+ };
+ var instances = {};
+ return (function(config){
+ if (!config) config = {};
+ if (!config.timeout) config.timeout = 4000;
- var events = require('./events');
- var EventEmitter = events.EventEmitter;
- var sys = require('sys');
- var addConsoleOutputConcerns = require('./noodleTestConsole');
- var addSystemConcerns = require('./system');
- 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();
+ var configHash = hashConfig(config);
+ if (instances[configHash]) return instances[configHash];
- /* Relay events emitted by Test and Context instances to the main object */
- Test.on('new', function(t){
- events.relayEvents(t, main, ['assertionPassed', 'assertionFailed', 'testFlunk', 'testStarted', 'testTimeout', 'testDone']);
- });
- Context.on('new', function(ctx){
- events.relayEvents(ctx, main, ['pushContext', 'popContext']);
- });
+ var events = require('./events');
+ var EventEmitter = events.EventEmitter;
+ var sys = require('sys');
+ var addConsoleOutputConcerns = require('./noodleTestConsole');
+ var addSystemConcerns = require('./system');
+ 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();
- var topLevelContexts = [];
+ /* Relay events emitted by Test and Context instances to the main object */
+ Test.on('new', function(t){
+ events.relayEvents(t, main, ['assertionPassed', 'assertionFailed', 'testFlunk', 'testStarted', 'testTimeout', 'testDone']);
+ });
+ Context.on('new', function(ctx){
+ events.relayEvents(ctx, main, ['pushContext', 'popContext']);
+ });
- main.context = function(name, callback) {
- var ctx = new Context(name, null);
- topLevelContexts.push(ctx);
- main.emit('pushContext', {name: name, context: ctx});
- callback.call(ctx, ctx);
- main.emit('popContext', {name: name, context: ctx});
- };
+ var topLevelContexts = [];
+
+ main.context = function(name, callback) {
+ var ctx = new Context(name, null);
+ topLevelContexts.push(ctx);
+ main.emit('pushContext', {name: name, context: ctx});
+ callback.call(ctx, ctx);
+ main.emit('popContext', {name: name, context: ctx});
+ };
- addSystemConcerns(main);
- if (!config['quiet']) {
- addConsoleOutputConcerns(main);
- }
+ addSystemConcerns(main);
+ if (!config['quiet']) {
+ addConsoleOutputConcerns(main);
+ }
- return main;
-});
+ instances[configHash] = main;
+ return main;
+ });
+})();
View
@@ -12,12 +12,16 @@ module.exports = function(main) {
main.on('assertionFailed', seenFailureCallback);
main.on('testFlunk', seenFailureCallback);
+ var calledOnFailureExitNonZero = false;
main.onFailureExitNonZero = function() {
- process.on('exit', function(a) {
- if (seenFailure) {
- /* Hack */
- process.kill(process.pid, 'SIGHUP');
- }
- });
+ if (!calledOnFailureExitNonZero) {
+ calledOnFailureExitNonZero = true;
+ process.on('exit', function(a) {
+ if (seenFailure) {
+ /* Hack */
+ process.kill(process.pid, 'SIGHUP');
+ }
+ });
+ }
};
};
View
@@ -5,6 +5,23 @@ test.onFailureExitNonZero();
test.context("NoodleTest dogfood test", function() {
+ this.context('Environment', function() {
+ this.context('instantiation', function() {
+ this.it('should return the same instance when given the same config', function() {
+ var t1 = require('../noodleTest')({quiet: true});
+ var t2 = require('../noodleTest')({quiet: true});
+ this.assertEqual(t1, t2);
+ this.done();
+ });
+ this.it('should return a different instance when given different configs', function() {
+ var t1 = require('../noodleTest')({quiet: true, timeout: 1000});
+ var t2 = require('../noodleTest')({quiet: true});
+ this.assertNotEqual(t1, t2);
+ this.done();
+ });
+ });
+ });
+
this.context('Basic Execution', function() {
this.it("should call the first context callback", function(outerTest) {
var t = require('../noodleTest')({quiet: true});

0 comments on commit 7bc5d06

Please sign in to comment.