Mathias Panzenböck avatar Mathias Panzenböck committed 6f1d05e

pass a few more tests

Comments (0)

Files changed (5)

 // http://wiki.commonjs.org/wiki/Unit_Testing/1.0
 
-function AssertionError(args, fileName, lineNumber) {
+var AssertionError = function (args, fileName, lineNumber) {
 	if (args.messages === undefined) {
 		if (args.operator === undefined) {
 			this.message = 'Expected '+JSON.stringify(args.expected)+
 	var err = new Error();
 
 	if (err.stack) {
+		this.stack = err.stack;
+
 		// remove one stack level:
-		if (typeof(Components) != 'undefined') {
-			// Mozilla:
-			this.stack = err.stack.substring(err.stack.indexOf('\n')+1);
+		if (typeof(window) != 'undefined') {
+			if (window.Components) {
+				// Mozilla:
+				this.stack = err.stack.substring(err.stack.indexOf('\n')+1);
+			}
+			else if (window.chrome) {
+				// Google Chrome:
+				this.stack = err.stack.replace(/\n[^\n]*/,'');
+			}
 		}
-		else if (typeof(chrome) != 'undefined' || typeof(process) != 'undefined') {
-			// Google Chrome/Node.js:
+		else if (typeof(process) != 'undefined') {
+			// 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) {
+var fail = function (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);
 	if (!(actual != expected)) fail(actual, expected, message, '!=');
 };
 
-function keysOf (obj) {
+var keysOf = function (obj) {
 	var keys = [];
 	for (key in obj) {
 		keys.push(key);
 	}
 	return keys;
-}
+};
 
-function eq (lhs, rhs) {
+var eq = function (lhs, rhs) {
 	if (lhs === rhs) {
 		return true;
 	}
 	else {
 		return rhs == lhs;
 	}
-}
+};
 
 exports.deepEqual = function (actual, expected, message) {
 	if (!eq(actual, expected)) fail(actual, expected, message, 'deep equals');
 	if (!(actual !== expected)) fail(actual, expected, message, '!==');
 };
 
-function isErrorClass (obj) {
+var isErrorClass = function (obj) {
 	return obj instanceof Function && (obj === Error || obj.prototype instanceof Error);
-}
+};
 
 exports.throws = function (block, error, message) {
 	try {
 	if (e === undefined) {
 		e = new Error();
 	}
-	if (typeof(Components) != 'undefined') {
+	if (typeof(window) != 'undefined' && window.Components) {
 		exports.error('Taraceback (most recent call last):');
 		stack = e.stack.split('\n');
 		for (var i = stack.length - 2; i > 0; -- i) {
 			exports.error('  File '+JSON.stringify(m[2])+', line '+m[3]+', in '+m[1]);
 		}
 	}
-	else if (typeof(chrome) != 'undefined' || typeof(process) != 'undefined') {
+	else if ((typeof(window) != 'undefined' && window.chrome)
+			|| typeof(process) != 'undefined') {
 		exports.error('Taraceback (most recent call last):');
 		stack = e.stack.split('\n');
 		for (var i = stack.length - 1; i > 0; -- i) {
 	exports.lang = system.env.LANG || system.env.LANGUAGE;
 }
 
-function _load (moduleName, ext) {
+var _load = function (moduleName, ext) {
 	var resolver = new require.Resolver(moduleName, ext, require.main);
 	var err = null;
 	var data;
 
 	do {
 		try {
-			data = fs.read(resolver.reluri);
+			data = fs.read(resolver.path);
 		}
 		catch (e) {
 			err = e;
 	}
 
 	return JSON.parse(data);
-}
+};
 
 exports.load = function (moduleName, lang) {
 	if (!lang) {
 		location.pathname,
 		location.protocol + '//' + location.host + location.pathname,
 		location.pathname);
+
+	loaded[main.id] = new Module();
 	
 	function normpath (path) {
 		var newpath = [];
 	}
 
 	function Resolver (moduleName, ext, parent) {
+		if (moduleName instanceof ModuleInfo) {
+			moduleName = moduleName.id;
+		}
+
 		var search = false;
 		var pathIndex = 0;
 		var prefix =  location.protocol + '//' + location.host;
 
 		if (moduleName[0] == '/') {
 			this.id = '/' + normpath(moduleName);
-			this.reluri = this.id + ext;
+			this.path = this.id + ext;
 		}
 		else if (moduleName.substring(0,2) == './'
 				|| moduleName.substring(0,3) == '../') {
 			this.id = '/' + normpath(parent.path + '/../' + moduleName);
-			this.reluri = this.id + ext;
+			this.path = this.id + ext;
 		}
 		else {
 			search = true;
 			resolvePaths = paths.slice();
-			resolvePaths.splice(0,0,parent.path+'/..');
+			if (resolvePaths.length == 0) {
+				throw new Error('cannot resove module path: '+moduleName);
+			}
 			this.id = '/' + normpath(resolvePaths[0] + '/' + moduleName);
-			this.reluri = this.id + ext;
+			this.path = this.id + ext;
 		}
 			
-		this.uri = prefix + this.reluri;
+		this.uri = prefix + this.path;
 
 		this.next = function () {
 			if (search && (pathIndex+1) < resolvePaths.length) {
 				this.id = '/' + normpath(resolvePaths[++pathIndex] + '/' + moduleName);
-				this.reluri = this.id + ext;
-				this.uri = prefix + this.reluri;
+				this.path = this.id + ext;
+				this.uri = prefix + this.path;
 				return true;
 			}
 			else {
 			if (search) {
 				pathIndex = 0;
 				this.id = '/' + normpath(resolvePaths[0] + '/' + moduleName);
-				this.reluri = this.id + ext;
-				this.uri = prefix + this.reluri;
+				this.path = this.id + ext;
+				this.uri = prefix + this.path;
 			}
 		};
 	}
 				var resolver = new Resolver(id, '.js', parent);
 
 				do {
-					if (resolver.id in loaded) { // || id in loading; ?
+					if (resolver.id in loaded || resolver.id in loading) {
 						return true;
 					}
 				} while (resolver.next());
 			var newModule = new ModuleInfo(
 				resolver.id,
 				resolver.uri,
-				resolver.reluri,
+				resolver.path,
 				parent);
+
 			var globals = {
 				exports: exports,
 				include: include,
 					globals[argname] = opts.globals[argname];
 				}
 			}
+
+			if (!('document' in globals)) {
+				globals.document = document;
+			}
+
+			if (!('window' in globals)) {
+				globals.window = window;
+			}
+
+			for (name in window) {
+				if (!(name in globals)) {
+					globals[name] = undefined;
+				}
+			}
 	
 			var xhr = new XMLHttpRequest();
 			var err = null;
 						globals.module = newModule = new ModuleInfo(
 							resolver.id,
 							resolver.uri,
-							resolver.reluri,
+							resolver.path,
 							parent);
 						globals.require = make_require(newModule);
-						xhr.open('GET', resolver.reluri, opts.async);
+						xhr.open('GET', resolver.path, opts.async);
 						xhr.send(null);
 					}
 					return;
 				}
 	
 				try {
-					_eval(
+					var program = _eval(
 						'(function (globals) { with (globals) { ' +
-						xhr.responseText+' }})')(globals);
+						xhr.responseText+' }})');
+					new program(globals);
 							
 					delete loading[resolver.id];
 					loaded[resolver.id] = exports;
 				}
 			}, false);
 	
-			xhr.open('GET', resolver.reluri, opts.async);
+			xhr.open('GET', resolver.path, opts.async);
 			xhr.send(null);
 	
 			if (err != null) {
 	return make_require(main, {
 		clear: function () {
 			return function () {
+				var exports = loaded[main.id];
 				loading = {};
 				loaded  = {};
+				loaded[main.id] = exports;
 			};
 		}
 	});
 	// be careful so that as few as possible is bound to the closure made by eval:
 })(function (code) { return eval(code); });
+const exports = require(require.main);
 const module  = require.main;
 const include = function (moduleName, opts) {
 	if (opts) {
 	}
 }
 
-function illegalWrite () {
+var illegalWrite = function () {
 	throw new Error('stream not writeable');
-}
+};
 
-function illegalRead () {
+var illegalRead = function () {
 	throw new Error('stream not readeable');
-}
+};
 
-function print () {
+var print = function () {
 	var buf = [];
 	for (var i = 0; i < arguments.length; ++ i) {
 		buf.push(arguments[i]);
 	}
 	this.writeLine(buf.join(this.delimiter));
-}
+};
 
-function writeLine (line) {
+var writeLine = function (line) {
 	this.write(line+'\n');
-}
+};
 
-function substr (buf, begin, end) {
+var substr = function (buf, begin, end) {
 	if (begin === undefined) {
 		if (end === undefined) {
 			return buf;
 	else {
 		return bug.substring(begin, end);
 	}
-}
+};
 
 exports.stdin = {
 	charset:   'UTF-8',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.