Permalink
Browse files

Prepend methods on Test & Context with underscores

to indicate that they are private / internal.
  • Loading branch information...
1 parent 9152fb2 commit e9240423d28b76e8a918eb9c69bb1aafabf15e7c @joelplane joelplane committed Jun 14, 2011
Showing with 53 additions and 43 deletions.
  1. +14 −9 context.js
  2. +16 −16 noodleTestConsole.js
  3. +19 −14 test.js
  4. +4 −4 tests/noodleTestTest.js
View
@@ -3,30 +3,35 @@ module.exports = (function(Test){
var EventEmitter = events.EventEmitter;
var sys = require('sys');
function Context(name, parentContext) {
- this.name = name;
- this.parentContext = parentContext;
- this.subContexts = [];
- this.tests = [];
+ this._name = name;
+ this._parentContext = parentContext;
+ this._subContexts = [];
+ this._tests = [];
EventEmitter.call(this);
+
+ /* Rename emit() method from EventEmitter to indicate that it is private */
+ this._emit = this.emit;
+ delete this['emit'];
+
Context.emit('new', this);
}
sys.inherits(Context, EventEmitter);
events.classEvents(Context);
Context.prototype.context = function(name, callback) {
var ctx = new Context(name, this);
- this.subContexts.push(ctx);
- this.emit('pushContext', {name: name, context: ctx});
+ this._subContexts.push(ctx);
+ this._emit('pushContext', {name: name, context: ctx});
callback.call(ctx, ctx);
- this.emit('popContext', {name: name, context: ctx});
+ this._emit('popContext', {name: name, context: ctx});
};
Context.prototype.it = function(name, callback) {
- this.tests.push(new Test(this, name, callback));
+ this._tests.push(new Test(this, name, callback));
};
Context.prototype._depth = function() {
var parent = this;
var i = 0;
while(parent) {
- parent = parent.parentContext;
+ parent = parent._parentContext;
i++;
}
return i;
View
@@ -49,7 +49,7 @@ module.exports = (function(test){
test.on('pushContext', function(o){
var contextIndent = o.context._depth();
- log(contextIndent, color(o.context.name, "yellow"));
+ log(contextIndent, color(o.context._name, "yellow"));
last = null;
});
@@ -58,24 +58,24 @@ module.exports = (function(test){
// });
test.on('testStarted', function(t){
- var contextIndent = t.context._depth();
- log(contextIndent+1, "it " + color(t.name, "yellow"));
+ var contextIndent = t._context._depth();
+ log(contextIndent+1, "it " + color(t._name, "yellow"));
last = 'testStarted';
});
test.on('testTimeout', function(t){
- var contextIndent = t.context._depth();
- log(contextIndent, 'Did you remember to call done() for test "'+t.name+'"?', 'magenta');
+ var contextIndent = t._context._depth();
+ log(contextIndent, 'Did you remember to call done() for test "'+t._name+'"?', 'magenta');
log(contextIndent, 'Make sure you do. We don\'t print out the stack traces until done() has been called' +"\n", 'magenta');
last = null;
});
test.on('testDone', function(t){
- var contextIndent = t.context._depth();
- var n = t.failures.length || t.passes.length;
- var message = (t.failures.length == 0) ? color(n + ' assertion'+(n==1?'':'s')+' passed','green') : color(n + ' assertion'+(n==1?'':'s')+" failed\n",'red');
+ var contextIndent = t._context._depth();
+ var n = t._failures.length || t._passes.length;
+ var message = (t._failures.length == 0) ? color(n + ' assertion'+(n==1?'':'s')+' passed','green') : color(n + ' assertion'+(n==1?'':'s')+" failed\n",'red');
log(contextIndent+2, message);
- t.failures.forEach(function(assertion){
+ t._failures.forEach(function(assertion){
var callString = assertion.callString();
var failureMessage = assertion.failureMessage();
log(contextIndent+2, callString + ' : ' + failureMessage, 'red+bold');
@@ -85,20 +85,20 @@ module.exports = (function(test){
last = null;
});
- test.on('testFlunk', function(t){
- var contextIndent = t.context._depth();
- log(contextIndent+2, t.context + ': '+color('Failed','red')+': ' + t.message);
+ test.on('testFlunk', function(o){
+ var contextIndent = o.context._depth();
+ log(contextIndent+2, o.context + ': '+color('Failed','red')+': ' + o.message);
last = null;
});
- test.on('assertionPassed', function(t){
- var contextIndent = t.context._depth();
+ test.on('assertionPassed', function(o){
+ var contextIndent = o.context._depth();
dot(contextIndent+2, color('+','green'));
last = 'assertionPassed';
});
- test.on('assertionFailed', function(t){
- var contextIndent = t.context._depth();
+ test.on('assertionFailed', function(o){
+ var contextIndent = o.context._depth();
dot(contextIndent+2, color('x','red'));
last = 'assertionFailed';
});
View
33 test.js
@@ -3,36 +3,41 @@ module.exports = (function(Assertion, testQueue, timeout){
var EventEmitter = events.EventEmitter;
var sys = require('sys');
var Test = function(context, name, testFunction) {
- this.context = context;
- this.name = name;
- this.testFunction = testFunction;
- this.failures = [];
- this.passes = [];
+ this._context = context;
+ this._name = name;
+ this._testFunction = testFunction;
+ this._failures = [];
+ this._passes = [];
EventEmitter.call(this);
+
+ /* Rename emit() method from EventEmitter to indicate that it is private */
+ this._emit = this.emit;
+ delete this['emit'];
+
Test.emit('new', this);
testQueue.put(this);
};
sys.inherits(Test, EventEmitter);
events.classEvents(Test);
Test.prototype._call = function() {
var test = this;
- this.emit('testStarted', this);
+ this._emit('testStarted', this);
this.done = function() {
clearTimeout(timer);
- test.emit('testDone', test);
+ test._emit('testDone', test);
};
var timer = setTimeout(function(){
- test.emit('testTimeout', test);
+ test._emit('testTimeout', test);
}, timeout);
try {
- this.testFunction.call(this, this);
+ this._testFunction.call(this, this);
} catch(error) {
this.flunk(error.toString(), {error: error, test: this});
}
};
Test.prototype.flunk = function(message, options) {
options = options ? options : {};
- this.emit('testFlunk', {context: this.context, message: message, options: options});
+ this._emit('testFlunk', {context: this._context, message: message, options: options});
};
Assertion.on('assertionAdded', function(definition){
@@ -48,11 +53,11 @@ module.exports = (function(Assertion, testQueue, timeout){
});
a.execute();
if (a.passed) {
- this.passes.push(a);
- this.emit('assertionPassed', {context: this.context});
+ this._passes.push(a);
+ this._emit('assertionPassed', {context: this._context});
} else {
- this.failures.push(a);
- this.emit('assertionFailed', {context: this.context});
+ this._failures.push(a);
+ this._emit('assertionFailed', {context: this._context});
}
}
});
View
@@ -72,8 +72,8 @@ test.context("NoodleTest dogfood test", function() {
var t = require('../noodleTest')({quiet: true});
ee.on('setupDone', function(test){
- outerTest.assert(test.failures.length > 0);
- outerTest.assert(test.passes.length == 0);
+ outerTest.assert(test._failures.length > 0);
+ outerTest.assert(test._passes.length == 0);
outerTest.done();
});
@@ -156,9 +156,9 @@ test.context("NoodleTest dogfood test", function() {
this.it('test test', function(innerTest){
innerTest.assert(false);
- var firstStackLine = innerTest.failures[0].stack[0];
+ var firstStackLine = innerTest._failures[0].stack[0];
// client's test function will be called "testFunction" in the stack trace
- outerTest.assert(firstStackLine.indexOf('.testFunction'));
+ outerTest.assert(firstStackLine.indexOf('._testFunction'));
innerTest.done();
outerTest.done();

0 comments on commit e924042

Please sign in to comment.