Commits

Mathias Panzenböck committed cc194c7

ifixed console.trace for Chrome/Node.js and added assert.js, test.js and xhr.js

Comments (0)

Files changed (4)

+// http://wiki.commonjs.org/wiki/Unit_Testing/1.0
+
+function AssertionError(args, fileName, lineNumber) {
+	if (args.messages === undefined) {
+		if (args.operator === undefined) {
+			this.message = 'Expected '+JSON.stringify(args.expected)+
+				', but got '+JSON.stringify(args.actual)+'.';
+		}
+		else {
+			this.message = 'Condition is not met: '+
+				JSON.stringify(args.expected)+' '+args.operator+' '+
+				JSON.stringify(args.actual);
+		}
+	}
+	else {
+		this.messages = args.messages;
+	}
+
+	var err = new Error();
+
+	if (err.stack) {
+		// remove one stack level:
+		if (typeof(Components) != 'undefined') {
+			// Mozilla:
+			this.stack = err.stack.substring(err.stack.indexOf('\n')+1);
+		}
+		else if (typeof(chrome) != 'undefined' || typeof(process) != 'undefined') {
+			// Google Chrome/Node.js:
+			this.stack = err.stack.replace(/\n[^\n]*/,'');
+		}
+		else {
+			this.stack = err.stack;
+		}
+	}
+	this.fileName   = fileName   === undefined ? err.fileName   : fileName;
+	this.lineNumber = lineNumber === undefined ? err.lineNumber : lineNumber;
+	this.expected = args.expected;
+	this.actual   = args.actual;
+	this.operator = args.operator;
+}
+
+AssertionError.prototype = new Error();
+AssertionError.prototype.constructor = AssertionError;
+AssertionError.prototype.name = 'AssertionError';
+
+function fail (actual, expected, message, operator) {
+	throw new AssertionError({
+		message:  message,
+		expected: expected,
+		actual:   actual,
+		operator: operator
+	});
+}
+
+exports.ok = function (ok, message) {
+	if (!ok) fail(ok, true, message);
+};
+
+exports.equal = function (actual, expected, message) {
+	if (!(actual == expected)) fail(actual, expected, message, '==');
+};
+
+exports.notEqual = function (actual, expected, message) {
+	if (!(actual != expected)) fail(actual, expected, message, '!=');
+};
+
+function keysOf (obj) {
+	var keys = [];
+	for (key in obj) {
+		keys.push(key);
+	}
+	return keys;
+}
+
+function eq (lhs, rhs) {
+	if (lhs === rhs) {
+		return true;
+	}
+	else if (lhs === null || lhs === undefined || rhs === null || rhs === undefined) {
+		return false;
+	}
+	else if (lhs instanceof Date) {
+		if (rhs instanceof Date) {
+			return lhs.getTime() == rhs.getTime();
+		}
+		else {
+			return false;
+		}
+	}
+	else if (rhs instanceof Date) {
+		return false;
+	}
+	else if (typeof(lhs) == 'object' && typeof(rhs) == 'object') {
+		// or is meant __proto__?
+		if (lhs.prototype != rhs.prototype) {
+			return false;
+		}
+
+		var lkeys = keysOf(lhs);
+		var rkeys = keysOf(rhs);
+
+		if (lkeys.length != rkeys.length) {
+			return false;
+		}
+
+		for (key in lkeys) {
+			if (!(key in rhs)) {
+				return false;
+			}
+			if (!eq(lhs[key], rhs[key])) {
+				return false;
+			}
+		}
+		return true;
+	}
+	else {
+		return rhs == lhs;
+	}
+}
+
+exports.deepEqual = function (actual, expected, message) {
+	if (!eq(actual, expected)) fail(actual, expected, message, 'deep equals');
+};
+
+exports.notDeepEqual = function (actual, expected, message) {
+	if (eq(actual, expected)) fail(actual, expected, message, 'not deep equals');
+};
+
+exports.strictEqual = function (actual, expected, message) {
+	if (!(actual === expected)) fail(actual, expected, message, '===');
+};
+
+exports.notStrictEqual = function (actual, expected, message) {
+	if (!(actual !== expected)) fail(actual, expected, message, '!==');
+};
+
+function isErrorClass (obj) {
+	return obj instanceof Function && (obj === Error || obj.prototype instanceof Error);
+}
+
+exports.throws = function (block, error, message) {
+	try {
+		block();
+	}
+	catch (e) {
+		if (error === undefined || isErrorClass(error) ?
+				!(e instanceof error) :
+				e !== error) {
+			fail(e, error, message || 'Got unwanted exception: '+e);
+		}
+		else {
+			return;
+		}
+	}
+	fail(undefined, error, message || 'Missing expected exception: '+
+		(typeof(error) == 'string' ? error : error.name));
+};
+
+exports.notThrows = function (block, error, message) {
+	try {
+		block();
+	}
+	catch (e) {
+		if (error === undefined || isErrorClass(error) ?
+				e instanceof error :
+				e === error) {
+			fail(e, error, message || 'Got unwanted exception: '+e);
+		}
+	}
+}
+
+exports.fail = fail;
+exports.AssertionError = AssertionError;
 	if (e === undefined) {
 		e = new Error();
 	}
-	exports.error('Taraceback (most recent call last):');
-	stack = e.stack.split('\n');
-	for (var i = stack.length - 2; i > 0; -- i) {
-		var m = /^(.*)@([^@]*):(\d+)$/.exec(stack[i]);
-		exports.error('  File '+JSON.stringify(m[2])+', line '+m[3]+', in '+m[1]);
+	if (typeof(Components) != 'undefined') {
+		exports.error('Taraceback (most recent call last):');
+		stack = e.stack.split('\n');
+		for (var i = stack.length - 2; i > 0; -- i) {
+			var m = /^(.*)@([^@]*):(\d+)$/.exec(stack[i]);
+			exports.error('  File '+JSON.stringify(m[2])+', line '+m[3]+', in '+m[1]);
+		}
+	}
+	else if (typeof(chrome) != 'undefined' || typeof(process) != 'undefined') {
+		exports.error('Taraceback (most recent call last):');
+		stack = e.stack.split('\n');
+		for (var i = stack.length - 1; i > 0; -- i) {
+			var m = /^\s*at (.*)\s*\((?:[^:]*|(.*):(\d+):\d+)\)\)?$/.exec(stack[i]);
+			var file = m[2] !== undefined ? JSON.stringify(m[2]) : '<unknown source>';
+			var line = m[3] !== undefined ? m[3] : '<unknown>';
+			exports.error('  File '+file+', line '+line+', in '+m[1]);
+		}
+	}
+	else {
+		exports.error(e.stack);
 	}
 };
 
+const console = require('console');
+const assert  = require('assert');
+
+exports.run = function (test) {
+	var startup  = test.startup  || function () {};
+	var teardown = test.teardown || function () {};
+	var count = 0;
+
+	for (name in test) {
+		if (name.substring(0,4) = 'test') {
+			startup();
+			try {
+				test[name]();
+			}
+			catch (e) {
+				if (e instanceof assert.AssertionError) {
+					console.error(e.message);
+				}
+				else {
+					console.error('Got unwanted exception: '+e);
+				}
+				++ count;
+			}
+			teardown();
+		}
+	}
+
+	return count;
+};
+exports.XMLHttpRequest = window.XMLHttpRequest;