Skip to content
Browse files

basic stub test works emitting json events

  • Loading branch information...
1 parent 1cc80b7 commit 6c78e602f3b8d5893b71c7025e5d1c0edfac0781 @substack committed Oct 27, 2011
Showing with 329 additions and 0 deletions.
  1. +6 −0 example/test.js
  2. +237 −0 index.js
  3. +86 −0 lib/deep_equal.js
View
6 example/test.js
@@ -0,0 +1,6 @@
+var test = require('testling');
+
+test('json parse', function (t) {
+ t.deepEqual(JSON.parse('[1,2]'), [1,2]);
+ t.end();
+});
View
237 index.js
@@ -0,0 +1,237 @@
+var EventEmitter = require('events').EventEmitter;
+var deepEqual = require('./lib/deep_equal');
+var pending = 0;
+
+function push (name, res) {
+ console.log(name + ': ' + JSON.stringify(res));
+}
+
+module.exports = function (name, cb) {
+ if (typeof name === 'function') {
+ cb = name;
+ name = undefined;
+ }
+
+ var t = new Test(name);
+ pending ++;
+
+ t.on('testEnd', function () {
+ pending --;
+ process.nextTick(function () {
+ if (pending <= 0) push('end', {});
+ });
+ });
+
+ cb(t);
+};
+
+var testId = 0;
+function Test (name, cb) {
+ this.id = testId ++;
+ push('testBegin', { name : name, testId : this.id });
+
+ this.counts = {
+ plan : undefined,
+ pass : 0,
+ fail : 0
+ };
+ this.windows = [];
+}
+
+Test.prototype = new EventEmitter;
+
+Test.prototype.assert = function (res) {
+ if (res.ok) this.counts.pass ++
+ else this.counts.fail ++
+
+ if (this.counts.plan !== undefined
+ && this.counts.pass + this.counts.fail > this.counts.plan) {
+ push('fail', {
+ type : 'fail',
+ ok : false,
+ found : this.counts.fail + this.counts.pass,
+ wanted : this.counts.plan,
+ name : 'more tests run than planned',
+ testId : this.id
+ });
+ }
+
+ res.testId = this.id;
+ push('assert', res);
+
+ if (this.counts.plan !== undefined
+ && this.counts.plan === this.counts.pass + this.counts.fail) {
+ this.end();
+ }
+};
+
+Test.prototype.ok = function (value, name) {
+ this.assert({
+ type : 'ok',
+ ok : !!value,
+ name : name,
+ found : Boolean(value),
+ wanted : true
+ });
+};
+
+Test.prototype.notOk = function (value, name) {
+ this.assert({
+ type : 'ok',
+ ok : !!!value,
+ name : name,
+ found : Boolean(value),
+ wanted : false
+ });
+};
+
+Test.prototype.fail = function (value, name) {
+ this.assert({
+ type : 'fail',
+ ok : false,
+ name : name,
+ found : value,
+ wanted : undefined
+ });
+};
+
+Test.prototype.equal = function (found, wanted, name) {
+ this.assert({
+ type : 'equal',
+ ok : found == wanted,
+ name : name,
+ found : found,
+ wanted : wanted
+ });
+};
+
+Test.prototype.notEqual = function (found, wanted, name) {
+ this.assert({
+ type : 'notEqual',
+ ok : found != wanted,
+ name : name,
+ found : found,
+ wanted : wanted
+ });
+};
+
+Test.prototype.deepEqual = function (found, wanted, name) {
+ this.assert({
+ type : 'deepEqual',
+ ok : deepEqual(found, wanted),
+ name : name,
+ found : found,
+ wanted : wanted
+ });
+};
+
+Test.prototype.notDeepEqual = function (found, wanted, name) {
+ this.assert({
+ type : 'notDeepEqual',
+ ok : !deepEqual(found, wanted),
+ name : name,
+ found : found,
+ wanted : wanted
+ });
+};
+
+Test.prototype.strictEqual = function (found, wanted, name) {
+ this.assert({
+ type : 'strictEqual',
+ ok : found === wanted,
+ name : name,
+ found : found,
+ wanted : wanted
+ });
+};
+
+Test.prototype.notStrictEqual = function (found, wanted, name) {
+ this.assert({
+ type : 'strictEqual',
+ ok : found !== wanted,
+ name : name,
+ found : found,
+ wanted : wanted
+ });
+};
+
+function checkThrows (shouldThrow, fn, expected, name) {
+ if (typeof expected === 'string') {
+ name = expected;
+ expected = null;
+ }
+ var ok = !shouldThrow, err = undefined;
+
+ try { fn() }
+ catch (e) {
+ ok = !ok;
+ err = e;
+ }
+
+ this.assert({
+ type : shouldThrow ? 'throws' : 'doesNotThrow',
+ ok : ok,
+ found : err,
+ expected : expected
+ });
+}
+
+Test.prototype['throws'] = function (fn, expected, name) {
+ checkThrows.call(this, true, fn, expected, name);
+};
+
+Test.prototype.doesNotThrow = function (fn, expected, name) {
+ checkThrows.call(this, false, fn, expected, name);
+};
+
+Test.prototype.ifError = function (err, name) {
+ this.assert({
+ type : 'ifError',
+ ok : !!!err,
+ name : name,
+ found : err,
+ wanted : undefined
+ });
+};
+
+Test.prototype.plan = function (n) {
+ if (this.counts.plan === undefined) {
+ this.counts.plan = n;
+ }
+ else {
+ this.counts.plan += n;
+ }
+ push('plan', { testId : this.id, n : n });
+};
+
+Test.prototype.log = function (msg) {
+ push('log', { testId : this.id, message : msg });
+};
+
+Test.prototype.end = function () {
+ if (this.counts.plan !== undefined
+ && this.counts.plan > this.counts.fail + this.counts.pass) {
+ push('planFail', {
+ type : 'fail',
+ ok : false,
+ found : this.counts.fail + this.counts.pass,
+ wanted : this.counts.plan,
+ name : 'more tests planned than run',
+ testId : this.id
+ });
+ }
+
+ if (!this.ended) {
+ this.ended = true;
+ push('testEnd', { testId : this.id });
+ this.emit('testEnd');
+ }
+};
+
+Test.prototype.createWindow = function (opts, cb) {
+ // ...
+};
+
+Test.prototype.submitForm = function (form, opts, cb) {
+ // ...
+};
View
86 lib/deep_equal.js
@@ -0,0 +1,86 @@
+// ripped from node's deepEqual implementation in assert
+
+var pSlice = Array.prototype.slice;
+var Object_keys = typeof Object.keys === 'function'
+ ? Object.keys
+ : function (obj) {
+ var keys = [];
+ for (var key in obj) keys.push(key);
+ return keys;
+ }
+;
+
+var deepEqual = module.exports = function (actual, expected) {
+ // 7.1. All identical values are equivalent, as determined by ===.
+ if (actual === expected) {
+ return true;
+
+ } else if (actual instanceof Date && expected instanceof Date) {
+ return actual.getTime() === expected.getTime();
+
+ // 7.3. Other pairs that do not both pass typeof value == 'object',
+ // equivalence is determined by ==.
+ } else if (typeof actual != 'object' && typeof expected != 'object') {
+ return actual == expected;
+
+ // 7.4. For all other Object pairs, including Array objects, equivalence is
+ // determined by having the same number of owned properties (as verified
+ // with Object.prototype.hasOwnProperty.call), the same set of keys
+ // (although not necessarily the same order), equivalent values for every
+ // corresponding key, and an identical 'prototype' property. Note: this
+ // accounts for both named and indexed properties on Arrays.
+ } else {
+ return objEquiv(actual, expected);
+ }
+}
+
+function isUndefinedOrNull(value) {
+ return value === null || value === undefined;
+}
+
+function isArguments(object) {
+ return Object.prototype.toString.call(object) == '[object Arguments]';
+}
+
+function objEquiv(a, b) {
+ if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
+ return false;
+ // an identical 'prototype' property.
+ if (a.prototype !== b.prototype) return false;
+ //~~~I've managed to break Object.keys through screwy arguments passing.
+ // Converting to array solves the problem.
+ if (isArguments(a)) {
+ if (!isArguments(b)) {
+ return false;
+ }
+ a = pSlice.call(a);
+ b = pSlice.call(b);
+ return deepEqual(a, b);
+ }
+ try {
+ var ka = Object_keys(a),
+ kb = Object_keys(b),
+ key, i;
+ } catch (e) {//happens when one is a string literal and the other isn't
+ return false;
+ }
+ // having the same number of owned properties (keys incorporates
+ // hasOwnProperty)
+ if (ka.length != kb.length)
+ return false;
+ //the same set of keys (although not necessarily the same order),
+ ka.sort();
+ kb.sort();
+ //~~~cheap key test
+ for (i = ka.length - 1; i >= 0; i--) {
+ if (ka[i] != kb[i])
+ return false;
+ }
+ //equivalent values for every corresponding key, and
+ //~~~possibly expensive deep test
+ for (i = ka.length - 1; i >= 0; i--) {
+ key = ka[i];
+ if (!deepEqual(a[key], b[key])) return false;
+ }
+ return true;
+}

0 comments on commit 6c78e60

Please sign in to comment.
Something went wrong with that request. Please try again.