addon-sdk/source/test/addons/require/main.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/addons/require/main.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +exports["test local vs sdk module"] = function (assert) {
    1.10 +  assert.notEqual(require("memory"),
    1.11 +                  require("sdk/deprecated/memory"),
    1.12 +                  "Local module takes the priority over sdk modules");
    1.13 +  assert.ok(require("memory").local,
    1.14 +            "this module is really the local one");
    1.15 +}
    1.16 +
    1.17 +exports["test 3rd party vs sdk module"] = function (assert) {
    1.18 +  // We are testing with a 3rd party package called `tabs` with 3 modules
    1.19 +  // main, page-mod and third-party
    1.20 +
    1.21 +  // the only way to require 3rd party package modules are to use absolute paths
    1.22 +  // require("tabs/main"), require("tabs/page-mod"),
    1.23 +  // require("tabs/third-party") and also require("tabs") which will refer
    1.24 +  // to tabs's main package module.
    1.25 +
    1.26 +  // So require(page-mod) shouldn't map the 3rd party
    1.27 +  assert.equal(require("page-mod"),
    1.28 +               require("sdk/page-mod"),
    1.29 +               "Third party modules don't overload sdk modules");
    1.30 +  assert.ok(require("page-mod").PageMod,
    1.31 +            "page-mod module is really the sdk one");
    1.32 +
    1.33 +  assert.equal(require("tabs/page-mod").id, "page-mod",
    1.34 +               "tabs/page-mod is the 3rd party");
    1.35 +
    1.36 +  // But require(tabs) will map to 3rd party main module
    1.37 +  // *and* overload the sdk module
    1.38 +  // and also any local module with the same name
    1.39 +  assert.equal(require("tabs").id, "tabs-main",
    1.40 +               "Third party main module overload sdk modules");
    1.41 +  assert.equal(require("tabs"),
    1.42 +               require("tabs/main"),
    1.43 +               "require(tabs) maps to require(tabs/main)");
    1.44 +  // So that you have to use relative path to ensure getting the local module
    1.45 +  assert.equal(require("./tabs").id,
    1.46 +               "local-tabs",
    1.47 +               "require(./tabs) maps to the local module");
    1.48 +
    1.49 +  // It should still be possible to require sdk module with absolute path
    1.50 +  assert.ok(require("sdk/tabs").open,
    1.51 +            "We can bypass this overloading with absolute path to sdk modules");
    1.52 +  assert.equal(require("sdk/tabs"),
    1.53 +               require("addon-kit/tabs"),
    1.54 +               "Old and new layout both work");
    1.55 +}
    1.56 +
    1.57 +// /!\ Always use distinct module for each test.
    1.58 +//     Otherwise, the linker can correctly parse and allow the first usage of it
    1.59 +//     but still silently fail on the second. 
    1.60 +
    1.61 +exports.testRelativeRequire = function (assert) {
    1.62 +  assert.equal(require('./same-folder').id, 'same-folder');
    1.63 +}
    1.64 +
    1.65 +exports.testRelativeSubFolderRequire = function (assert) {
    1.66 +  assert.equal(require('./sub-folder/module').id, 'sub-folder');
    1.67 +}
    1.68 +
    1.69 +exports.testMultipleRequirePerLine = function (assert) {
    1.70 +  var a=require('./multiple/a'),b=require('./multiple/b');
    1.71 +  assert.equal(a.id, 'a');
    1.72 +  assert.equal(b.id, 'b');
    1.73 +}
    1.74 +
    1.75 +exports.testSDKRequire = function (assert) {
    1.76 +  assert.deepEqual(Object.keys(require('sdk/page-worker')), ['Page']);
    1.77 +  assert.equal(require('page-worker'), require('sdk/page-worker'));
    1.78 +}
    1.79 +
    1.80 +require("sdk/test/runner").runTestsFromModule(module);

mercurial