michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: exports.testDefine = function(assert) { michael@0: let tiger = require('./modules/tiger'); michael@0: assert.equal(tiger.name, 'tiger', 'name proprety was exported properly'); michael@0: assert.equal(tiger.type, 'cat', 'property form other module exported'); michael@0: }; michael@0: michael@0: exports.testDefineInoresNonFactory = function(assert) { michael@0: let mod = require('./modules/async2'); michael@0: assert.equal(mod.name, 'async2', 'name proprety was exported properly'); michael@0: assert.ok(mod.traditional2Name !== 'traditional2', '1st is ignored'); michael@0: }; michael@0: /* Disable test that require AMD specific functionality: michael@0: michael@0: // define() that exports a function as the module value, michael@0: // specifying a module name. michael@0: exports.testDefExport = function(assert) { michael@0: var add = require('modules/add'); michael@0: assert.equal(add(1, 1), 2, 'Named define() exporting a function'); michael@0: }; michael@0: michael@0: // define() that exports function as a value, but is anonymous michael@0: exports.testAnonDefExport = function (assert) { michael@0: var subtract = require('modules/subtract'); michael@0: assert.equal(subtract(4, 2), 2, michael@0: 'Anonymous define() exporting a function'); michael@0: } michael@0: michael@0: // using require([], function () {}) to load modules. michael@0: exports.testSimpleRequire = function (assert) { michael@0: require(['modules/blue', 'modules/orange'], function (blue, orange) { michael@0: assert.equal(blue.name, 'blue', 'Simple require for blue'); michael@0: assert.equal(orange.name, 'orange', 'Simple require for orange'); michael@0: assert.equal(orange.parentType, 'color', michael@0: 'Simple require dependency check for orange'); michael@0: }); michael@0: } michael@0: michael@0: // using nested require([]) calls. michael@0: exports.testSimpleRequireNested = function (assert) { michael@0: require(['modules/blue', 'modules/orange', 'modules/green'], michael@0: function (blue, orange, green) { michael@0: michael@0: require(['modules/orange', 'modules/red'], function (orange, red) { michael@0: assert.equal(red.name, 'red', 'Simple require for red'); michael@0: assert.equal(red.parentType, 'color', michael@0: 'Simple require dependency check for red'); michael@0: assert.equal(blue.name, 'blue', 'Simple require for blue'); michael@0: assert.equal(orange.name, 'orange', 'Simple require for orange'); michael@0: assert.equal(orange.parentType, 'color', michael@0: 'Simple require dependency check for orange'); michael@0: assert.equal(green.name, 'green', 'Simple require for green'); michael@0: assert.equal(green.parentType, 'color', michael@0: 'Simple require dependency check for green'); michael@0: }); michael@0: michael@0: }); michael@0: } michael@0: michael@0: // requiring a traditional module, that uses async, that use traditional and michael@0: // async, with a circular reference michael@0: exports.testMixedCircular = function (assert) { michael@0: var t = require('modules/traditional1'); michael@0: assert.equal(t.name, 'traditional1', 'Testing name'); michael@0: assert.equal(t.traditional2Name, 'traditional2', michael@0: 'Testing dependent name'); michael@0: assert.equal(t.traditional1Name, 'traditional1', 'Testing circular name'); michael@0: assert.equal(t.async2Name, 'async2', 'Testing async2 name'); michael@0: assert.equal(t.async2Traditional2Name, 'traditional2', michael@0: 'Testing nested traditional2 name'); michael@0: } michael@0: michael@0: // Testing define()(function(require) {}) with some that use exports, michael@0: // some that use return. michael@0: exports.testAnonExportsReturn = function (assert) { michael@0: var lion = require('modules/lion'); michael@0: require(['modules/tiger', 'modules/cheetah'], function (tiger, cheetah) { michael@0: assert.equal('lion', lion, 'Check lion name'); michael@0: assert.equal('tiger', tiger.name, 'Check tiger name'); michael@0: assert.equal('cat', tiger.type, 'Check tiger type'); michael@0: assert.equal('cheetah', cheetah(), 'Check cheetah name'); michael@0: }); michael@0: } michael@0: michael@0: // circular dependency michael@0: exports.testCircular = function (assert) { michael@0: var pollux = require('modules/pollux'), michael@0: castor = require('modules/castor'); michael@0: michael@0: assert.equal(pollux.name, 'pollux', 'Pollux\'s name'); michael@0: assert.equal(pollux.getCastorName(), michael@0: 'castor', 'Castor\'s name from Pollux.'); michael@0: assert.equal(castor.name, 'castor', 'Castor\'s name'); michael@0: assert.equal(castor.getPolluxName(), 'pollux', michael@0: 'Pollux\'s name from Castor.'); michael@0: } michael@0: michael@0: // test a bad module that asks for exports but also does a define() return michael@0: exports.testBadExportAndReturn = function (assert) { michael@0: var passed = false; michael@0: try { michael@0: var bad = require('modules/badExportAndReturn'); michael@0: } catch(e) { michael@0: passed = /cannot use exports and also return/.test(e.toString()); michael@0: } michael@0: assert.equal(passed, true, 'Make sure exports and return fail'); michael@0: } michael@0: michael@0: // test a bad circular dependency, where an exported value is needed, but michael@0: // the return value happens too late, a module already asked for the exported michael@0: // value. michael@0: exports.testBadExportAndReturnCircular = function (assert) { michael@0: var passed = false; michael@0: try { michael@0: var bad = require('modules/badFirst'); michael@0: } catch(e) { michael@0: passed = /after another module has referenced its exported value/ michael@0: .test(e.toString()); michael@0: } michael@0: assert.equal(passed, true, 'Make sure return after an exported ' + michael@0: 'value is grabbed by another module fails.'); michael@0: } michael@0: michael@0: // only allow one define call per file. michael@0: exports.testOneDefine = function (assert) { michael@0: var passed = false; michael@0: try { michael@0: var dupe = require('modules/dupe'); michael@0: } catch(e) { michael@0: passed = /Only one call to define/.test(e.toString()); michael@0: } michael@0: assert.equal(passed, true, 'Only allow one define call per module'); michael@0: } michael@0: michael@0: // only allow one define call per file, testing a bad nested define call. michael@0: exports.testOneDefineNested = function (assert) { michael@0: var passed = false; michael@0: try { michael@0: var dupe = require('modules/dupeNested'); michael@0: } catch(e) { michael@0: passed = /Only one call to define/.test(e.toString()); michael@0: } michael@0: assert.equal(passed, true, 'Only allow one define call per module'); michael@0: } michael@0: */ michael@0: michael@0: require('sdk/test').run(exports);