2013-02-01 13:17:34 -08:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
let { Loader, main, unload, parseStack } = require('toolkit/loader');
|
|
|
|
|
2013-02-21 07:54:55 -08:00
|
|
|
let root = module.uri.substr(0, module.uri.lastIndexOf('/'))
|
2013-02-19 19:49:41 -08:00
|
|
|
|
2013-02-21 07:54:55 -08:00
|
|
|
exports['test dependency cycles'] = function(assert) {
|
|
|
|
let uri = root + '/fixtures/loader/cycles/';
|
2013-02-01 13:17:34 -08:00
|
|
|
let loader = Loader({ paths: { '': uri } });
|
|
|
|
|
|
|
|
let program = main(loader, 'main');
|
|
|
|
|
|
|
|
assert.equal(program.a.b, program.b, 'module `a` gets correct `b`');
|
|
|
|
assert.equal(program.b.a, program.a, 'module `b` gets correct `a`');
|
|
|
|
assert.equal(program.c.main, program, 'module `c` gets correct `main`');
|
|
|
|
|
|
|
|
unload(loader);
|
2013-02-21 07:54:55 -08:00
|
|
|
}
|
2013-02-01 13:17:34 -08:00
|
|
|
|
|
|
|
exports['test syntax errors'] = function(assert) {
|
2013-02-21 07:54:55 -08:00
|
|
|
let uri = root + '/fixtures/loader/syntax-error/';
|
2013-02-01 13:17:34 -08:00
|
|
|
let loader = Loader({ paths: { '': uri } });
|
|
|
|
|
|
|
|
try {
|
|
|
|
let program = main(loader, 'main');
|
|
|
|
} catch (error) {
|
|
|
|
assert.equal(error.name, "SyntaxError", "throws syntax error");
|
|
|
|
assert.equal(error.fileName.split("/").pop(), "error.js",
|
|
|
|
"Error contains filename");
|
|
|
|
assert.equal(error.lineNumber, 7, "error is on line 7")
|
|
|
|
let stack = parseStack(error.stack);
|
|
|
|
assert.equal(stack.pop().fileName, uri + "main.js",
|
|
|
|
"loader stack is omitted");
|
|
|
|
assert.equal(stack.pop().fileName, module.uri,
|
|
|
|
"previous in the stack is test module");
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
unload(loader);
|
|
|
|
}
|
2013-02-21 07:54:55 -08:00
|
|
|
}
|
2013-02-01 13:17:34 -08:00
|
|
|
|
|
|
|
exports['test missing module'] = function(assert) {
|
2013-02-21 07:54:55 -08:00
|
|
|
let uri = root + '/fixtures/loader/missing/'
|
2013-02-01 13:17:34 -08:00
|
|
|
let loader = Loader({ paths: { '': uri } });
|
|
|
|
|
|
|
|
try {
|
|
|
|
let program = main(loader, 'main')
|
|
|
|
} catch (error) {
|
|
|
|
assert.equal(error.message, "Module `not-found` is not found at " +
|
|
|
|
uri + "not-found.js", "throws if error not found");
|
|
|
|
|
|
|
|
assert.equal(error.fileName.split("/").pop(), "main.js",
|
|
|
|
"Error fileName is requirer module");
|
|
|
|
|
|
|
|
assert.equal(error.lineNumber, 7, "error is on line 7");
|
|
|
|
|
|
|
|
let stack = parseStack(error.stack);
|
|
|
|
|
|
|
|
assert.equal(stack.pop().fileName, uri + "main.js",
|
|
|
|
"loader stack is omitted");
|
|
|
|
|
|
|
|
assert.equal(stack.pop().fileName, module.uri,
|
|
|
|
"previous in the stack is test module");
|
|
|
|
} finally {
|
|
|
|
unload(loader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports['test exceptions in modules'] = function(assert) {
|
2013-02-21 07:54:55 -08:00
|
|
|
let uri = root + '/fixtures/loader/exceptions/'
|
2013-02-01 13:17:34 -08:00
|
|
|
|
|
|
|
let loader = Loader({ paths: { '': uri } });
|
|
|
|
|
|
|
|
try {
|
|
|
|
let program = main(loader, 'main')
|
|
|
|
} catch (error) {
|
|
|
|
assert.equal(error.message, "Boom!", "thrown errors propagate");
|
|
|
|
|
|
|
|
assert.equal(error.fileName.split("/").pop(), "boomer.js",
|
|
|
|
"Error comes from the module that threw it");
|
|
|
|
|
|
|
|
assert.equal(error.lineNumber, 8, "error is on line 8");
|
|
|
|
|
|
|
|
let stack = parseStack(error.stack);
|
|
|
|
|
|
|
|
let frame = stack.pop()
|
|
|
|
assert.equal(frame.fileName, uri + "boomer.js",
|
|
|
|
"module that threw is first in the stack");
|
|
|
|
assert.equal(frame.name, "exports.boom",
|
|
|
|
"name is in the stack");
|
|
|
|
|
|
|
|
frame = stack.pop()
|
|
|
|
assert.equal(frame.fileName, uri + "main.js",
|
|
|
|
"module that called it is next in the stack");
|
|
|
|
assert.equal(frame.lineNumber, 9, "caller line is in the stack");
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(stack.pop().fileName, module.uri,
|
|
|
|
"this test module is next in the stack");
|
|
|
|
} finally {
|
|
|
|
unload(loader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports['test early errors in module'] = function(assert) {
|
2013-02-21 07:54:55 -08:00
|
|
|
let uri = root + '/fixtures/loader/errors/';
|
2013-02-01 13:17:34 -08:00
|
|
|
let loader = Loader({ paths: { '': uri } });
|
|
|
|
|
|
|
|
try {
|
|
|
|
let program = main(loader, 'main')
|
|
|
|
} catch (error) {
|
|
|
|
assert.equal(String(error),
|
|
|
|
"Error: opening input stream (invalid filename?)",
|
|
|
|
"thrown errors propagate");
|
|
|
|
|
|
|
|
assert.equal(error.fileName.split("/").pop(), "boomer.js",
|
|
|
|
"Error comes from the module that threw it");
|
|
|
|
|
|
|
|
assert.equal(error.lineNumber, 7, "error is on line 7");
|
|
|
|
|
|
|
|
let stack = parseStack(error.stack);
|
|
|
|
|
|
|
|
let frame = stack.pop()
|
|
|
|
assert.equal(frame.fileName, uri + "boomer.js",
|
|
|
|
"module that threw is first in the stack");
|
|
|
|
|
|
|
|
frame = stack.pop()
|
|
|
|
assert.equal(frame.fileName, uri + "main.js",
|
|
|
|
"module that called it is next in the stack");
|
|
|
|
assert.equal(frame.lineNumber, 7, "caller line is in the stack");
|
|
|
|
|
|
|
|
|
|
|
|
assert.equal(stack.pop().fileName, module.uri,
|
|
|
|
"this test module is next in the stack");
|
|
|
|
} finally {
|
|
|
|
unload(loader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
require('test').run(exports);
|