addon-sdk/source/test/test-modules.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 exports.testDefine = function(assert) {
michael@0 6 let tiger = require('./modules/tiger');
michael@0 7 assert.equal(tiger.name, 'tiger', 'name proprety was exported properly');
michael@0 8 assert.equal(tiger.type, 'cat', 'property form other module exported');
michael@0 9 };
michael@0 10
michael@0 11 exports.testDefineInoresNonFactory = function(assert) {
michael@0 12 let mod = require('./modules/async2');
michael@0 13 assert.equal(mod.name, 'async2', 'name proprety was exported properly');
michael@0 14 assert.ok(mod.traditional2Name !== 'traditional2', '1st is ignored');
michael@0 15 };
michael@0 16 /* Disable test that require AMD specific functionality:
michael@0 17
michael@0 18 // define() that exports a function as the module value,
michael@0 19 // specifying a module name.
michael@0 20 exports.testDefExport = function(assert) {
michael@0 21 var add = require('modules/add');
michael@0 22 assert.equal(add(1, 1), 2, 'Named define() exporting a function');
michael@0 23 };
michael@0 24
michael@0 25 // define() that exports function as a value, but is anonymous
michael@0 26 exports.testAnonDefExport = function (assert) {
michael@0 27 var subtract = require('modules/subtract');
michael@0 28 assert.equal(subtract(4, 2), 2,
michael@0 29 'Anonymous define() exporting a function');
michael@0 30 }
michael@0 31
michael@0 32 // using require([], function () {}) to load modules.
michael@0 33 exports.testSimpleRequire = function (assert) {
michael@0 34 require(['modules/blue', 'modules/orange'], function (blue, orange) {
michael@0 35 assert.equal(blue.name, 'blue', 'Simple require for blue');
michael@0 36 assert.equal(orange.name, 'orange', 'Simple require for orange');
michael@0 37 assert.equal(orange.parentType, 'color',
michael@0 38 'Simple require dependency check for orange');
michael@0 39 });
michael@0 40 }
michael@0 41
michael@0 42 // using nested require([]) calls.
michael@0 43 exports.testSimpleRequireNested = function (assert) {
michael@0 44 require(['modules/blue', 'modules/orange', 'modules/green'],
michael@0 45 function (blue, orange, green) {
michael@0 46
michael@0 47 require(['modules/orange', 'modules/red'], function (orange, red) {
michael@0 48 assert.equal(red.name, 'red', 'Simple require for red');
michael@0 49 assert.equal(red.parentType, 'color',
michael@0 50 'Simple require dependency check for red');
michael@0 51 assert.equal(blue.name, 'blue', 'Simple require for blue');
michael@0 52 assert.equal(orange.name, 'orange', 'Simple require for orange');
michael@0 53 assert.equal(orange.parentType, 'color',
michael@0 54 'Simple require dependency check for orange');
michael@0 55 assert.equal(green.name, 'green', 'Simple require for green');
michael@0 56 assert.equal(green.parentType, 'color',
michael@0 57 'Simple require dependency check for green');
michael@0 58 });
michael@0 59
michael@0 60 });
michael@0 61 }
michael@0 62
michael@0 63 // requiring a traditional module, that uses async, that use traditional and
michael@0 64 // async, with a circular reference
michael@0 65 exports.testMixedCircular = function (assert) {
michael@0 66 var t = require('modules/traditional1');
michael@0 67 assert.equal(t.name, 'traditional1', 'Testing name');
michael@0 68 assert.equal(t.traditional2Name, 'traditional2',
michael@0 69 'Testing dependent name');
michael@0 70 assert.equal(t.traditional1Name, 'traditional1', 'Testing circular name');
michael@0 71 assert.equal(t.async2Name, 'async2', 'Testing async2 name');
michael@0 72 assert.equal(t.async2Traditional2Name, 'traditional2',
michael@0 73 'Testing nested traditional2 name');
michael@0 74 }
michael@0 75
michael@0 76 // Testing define()(function(require) {}) with some that use exports,
michael@0 77 // some that use return.
michael@0 78 exports.testAnonExportsReturn = function (assert) {
michael@0 79 var lion = require('modules/lion');
michael@0 80 require(['modules/tiger', 'modules/cheetah'], function (tiger, cheetah) {
michael@0 81 assert.equal('lion', lion, 'Check lion name');
michael@0 82 assert.equal('tiger', tiger.name, 'Check tiger name');
michael@0 83 assert.equal('cat', tiger.type, 'Check tiger type');
michael@0 84 assert.equal('cheetah', cheetah(), 'Check cheetah name');
michael@0 85 });
michael@0 86 }
michael@0 87
michael@0 88 // circular dependency
michael@0 89 exports.testCircular = function (assert) {
michael@0 90 var pollux = require('modules/pollux'),
michael@0 91 castor = require('modules/castor');
michael@0 92
michael@0 93 assert.equal(pollux.name, 'pollux', 'Pollux\'s name');
michael@0 94 assert.equal(pollux.getCastorName(),
michael@0 95 'castor', 'Castor\'s name from Pollux.');
michael@0 96 assert.equal(castor.name, 'castor', 'Castor\'s name');
michael@0 97 assert.equal(castor.getPolluxName(), 'pollux',
michael@0 98 'Pollux\'s name from Castor.');
michael@0 99 }
michael@0 100
michael@0 101 // test a bad module that asks for exports but also does a define() return
michael@0 102 exports.testBadExportAndReturn = function (assert) {
michael@0 103 var passed = false;
michael@0 104 try {
michael@0 105 var bad = require('modules/badExportAndReturn');
michael@0 106 } catch(e) {
michael@0 107 passed = /cannot use exports and also return/.test(e.toString());
michael@0 108 }
michael@0 109 assert.equal(passed, true, 'Make sure exports and return fail');
michael@0 110 }
michael@0 111
michael@0 112 // test a bad circular dependency, where an exported value is needed, but
michael@0 113 // the return value happens too late, a module already asked for the exported
michael@0 114 // value.
michael@0 115 exports.testBadExportAndReturnCircular = function (assert) {
michael@0 116 var passed = false;
michael@0 117 try {
michael@0 118 var bad = require('modules/badFirst');
michael@0 119 } catch(e) {
michael@0 120 passed = /after another module has referenced its exported value/
michael@0 121 .test(e.toString());
michael@0 122 }
michael@0 123 assert.equal(passed, true, 'Make sure return after an exported ' +
michael@0 124 'value is grabbed by another module fails.');
michael@0 125 }
michael@0 126
michael@0 127 // only allow one define call per file.
michael@0 128 exports.testOneDefine = function (assert) {
michael@0 129 var passed = false;
michael@0 130 try {
michael@0 131 var dupe = require('modules/dupe');
michael@0 132 } catch(e) {
michael@0 133 passed = /Only one call to define/.test(e.toString());
michael@0 134 }
michael@0 135 assert.equal(passed, true, 'Only allow one define call per module');
michael@0 136 }
michael@0 137
michael@0 138 // only allow one define call per file, testing a bad nested define call.
michael@0 139 exports.testOneDefineNested = function (assert) {
michael@0 140 var passed = false;
michael@0 141 try {
michael@0 142 var dupe = require('modules/dupeNested');
michael@0 143 } catch(e) {
michael@0 144 passed = /Only one call to define/.test(e.toString());
michael@0 145 }
michael@0 146 assert.equal(passed, true, 'Only allow one define call per module');
michael@0 147 }
michael@0 148 */
michael@0 149
michael@0 150 require('sdk/test').run(exports);

mercurial