2013-02-01 21:17:34 +00: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/. */
|
|
|
|
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testDefine = function(assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
let tiger = require('./modules/tiger');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(tiger.name, 'tiger', 'name proprety was exported properly');
|
|
|
|
assert.equal(tiger.type, 'cat', 'property form other module exported');
|
2013-02-01 21:17:34 +00:00
|
|
|
};
|
|
|
|
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testDefineInoresNonFactory = function(assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
let mod = require('./modules/async2');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(mod.name, 'async2', 'name proprety was exported properly');
|
|
|
|
assert.ok(mod.traditional2Name !== 'traditional2', '1st is ignored');
|
2013-02-01 21:17:34 +00:00
|
|
|
};
|
|
|
|
/* Disable test that require AMD specific functionality:
|
|
|
|
|
|
|
|
// define() that exports a function as the module value,
|
|
|
|
// specifying a module name.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testDefExport = function(assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var add = require('modules/add');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(add(1, 1), 2, 'Named define() exporting a function');
|
2013-02-01 21:17:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// define() that exports function as a value, but is anonymous
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testAnonDefExport = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var subtract = require('modules/subtract');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(subtract(4, 2), 2,
|
2013-02-01 21:17:34 +00:00
|
|
|
'Anonymous define() exporting a function');
|
|
|
|
}
|
|
|
|
|
|
|
|
// using require([], function () {}) to load modules.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testSimpleRequire = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
require(['modules/blue', 'modules/orange'], function (blue, orange) {
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(blue.name, 'blue', 'Simple require for blue');
|
|
|
|
assert.equal(orange.name, 'orange', 'Simple require for orange');
|
|
|
|
assert.equal(orange.parentType, 'color',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Simple require dependency check for orange');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// using nested require([]) calls.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testSimpleRequireNested = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
require(['modules/blue', 'modules/orange', 'modules/green'],
|
|
|
|
function (blue, orange, green) {
|
|
|
|
|
|
|
|
require(['modules/orange', 'modules/red'], function (orange, red) {
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(red.name, 'red', 'Simple require for red');
|
|
|
|
assert.equal(red.parentType, 'color',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Simple require dependency check for red');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(blue.name, 'blue', 'Simple require for blue');
|
|
|
|
assert.equal(orange.name, 'orange', 'Simple require for orange');
|
|
|
|
assert.equal(orange.parentType, 'color',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Simple require dependency check for orange');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(green.name, 'green', 'Simple require for green');
|
|
|
|
assert.equal(green.parentType, 'color',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Simple require dependency check for green');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// requiring a traditional module, that uses async, that use traditional and
|
|
|
|
// async, with a circular reference
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testMixedCircular = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var t = require('modules/traditional1');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(t.name, 'traditional1', 'Testing name');
|
|
|
|
assert.equal(t.traditional2Name, 'traditional2',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Testing dependent name');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(t.traditional1Name, 'traditional1', 'Testing circular name');
|
|
|
|
assert.equal(t.async2Name, 'async2', 'Testing async2 name');
|
|
|
|
assert.equal(t.async2Traditional2Name, 'traditional2',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Testing nested traditional2 name');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Testing define()(function(require) {}) with some that use exports,
|
|
|
|
// some that use return.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testAnonExportsReturn = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var lion = require('modules/lion');
|
|
|
|
require(['modules/tiger', 'modules/cheetah'], function (tiger, cheetah) {
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal('lion', lion, 'Check lion name');
|
|
|
|
assert.equal('tiger', tiger.name, 'Check tiger name');
|
|
|
|
assert.equal('cat', tiger.type, 'Check tiger type');
|
|
|
|
assert.equal('cheetah', cheetah(), 'Check cheetah name');
|
2013-02-01 21:17:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// circular dependency
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testCircular = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var pollux = require('modules/pollux'),
|
|
|
|
castor = require('modules/castor');
|
|
|
|
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(pollux.name, 'pollux', 'Pollux\'s name');
|
|
|
|
assert.equal(pollux.getCastorName(),
|
2013-02-01 21:17:34 +00:00
|
|
|
'castor', 'Castor\'s name from Pollux.');
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(castor.name, 'castor', 'Castor\'s name');
|
|
|
|
assert.equal(castor.getPolluxName(), 'pollux',
|
2013-02-01 21:17:34 +00:00
|
|
|
'Pollux\'s name from Castor.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// test a bad module that asks for exports but also does a define() return
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testBadExportAndReturn = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var passed = false;
|
|
|
|
try {
|
|
|
|
var bad = require('modules/badExportAndReturn');
|
|
|
|
} catch(e) {
|
|
|
|
passed = /cannot use exports and also return/.test(e.toString());
|
|
|
|
}
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(passed, true, 'Make sure exports and return fail');
|
2013-02-01 21:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test a bad circular dependency, where an exported value is needed, but
|
|
|
|
// the return value happens too late, a module already asked for the exported
|
|
|
|
// value.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testBadExportAndReturnCircular = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var passed = false;
|
|
|
|
try {
|
|
|
|
var bad = require('modules/badFirst');
|
|
|
|
} catch(e) {
|
|
|
|
passed = /after another module has referenced its exported value/
|
|
|
|
.test(e.toString());
|
|
|
|
}
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(passed, true, 'Make sure return after an exported ' +
|
2013-02-01 21:17:34 +00:00
|
|
|
'value is grabbed by another module fails.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// only allow one define call per file.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testOneDefine = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var passed = false;
|
|
|
|
try {
|
|
|
|
var dupe = require('modules/dupe');
|
|
|
|
} catch(e) {
|
|
|
|
passed = /Only one call to define/.test(e.toString());
|
|
|
|
}
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(passed, true, 'Only allow one define call per module');
|
2013-02-01 21:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// only allow one define call per file, testing a bad nested define call.
|
2013-10-04 23:48:52 +00:00
|
|
|
exports.testOneDefineNested = function (assert) {
|
2013-02-01 21:17:34 +00:00
|
|
|
var passed = false;
|
|
|
|
try {
|
|
|
|
var dupe = require('modules/dupeNested');
|
|
|
|
} catch(e) {
|
|
|
|
passed = /Only one call to define/.test(e.toString());
|
|
|
|
}
|
2013-10-04 23:48:52 +00:00
|
|
|
assert.equal(passed, true, 'Only allow one define call per module');
|
2013-02-01 21:17:34 +00:00
|
|
|
}
|
|
|
|
*/
|
2013-10-04 23:48:52 +00:00
|
|
|
|
|
|
|
require('sdk/test').run(exports);
|