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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 'use strict';
     7 const traceback = require('sdk/console/traceback');
     8 const REQUIRE_LINE_NO = 30;
    10 exports.test_no_args = function(assert) {
    11   let passed = tryRequireModule(assert);
    12   assert.ok(passed, 'require() with no args should raise helpful error');
    13 };
    15 exports.test_invalid_sdk_module = function (assert) {
    16   let passed = tryRequireModule(assert, 'sdk/does-not-exist');
    17   assert.ok(passed, 'require() with an invalid sdk module should raise');
    18 };
    20 exports.test_invalid_relative_module = function (assert) {
    21   let passed = tryRequireModule(assert, './does-not-exist');
    22   assert.ok(passed, 'require() with an invalid relative module should raise');
    23 };
    26 function tryRequireModule(assert, module) {
    27   let passed = false;
    28   try {
    29     // This line number is important, referenced in REQUIRE_LINE_NO
    30     let doesNotExist = require(module);
    31   } catch(e) {
    32     checkError(assert, module, e);
    33     passed = true;
    34   }
    35   return passed;
    36 }
    38 function checkError (assert, name, e) {
    39   let msg = e.toString();
    40   if (name) {
    41     assert.ok(/is not found at/.test(msg),
    42       'Error message indicates module not found');
    43     assert.ok(msg.indexOf(name.replace(/\./g,'')) > -1,
    44       'Error message has the invalid module name in the message');
    45   }
    46   else {
    47     assert.equal(msg.indexOf('Error: you must provide a module name when calling require() from '), 0);
    48     assert.ok(msg.indexOf("test-require") !== -1, msg);
    49   }
    51   // we'd also like to assert that the right filename
    52   // and linenumber is in the stacktrace
    53   let tb = traceback.fromException(e);
    54   // Get the second to last frame, as the last frame is inside
    55   // toolkit/loader
    56   let lastFrame = tb[tb.length-2];
    57   assert.ok(lastFrame.fileName.indexOf("test-require.js") !== -1,
    58                           'Filename found in stacktrace');
    59   assert.equal(lastFrame.lineNumber, REQUIRE_LINE_NO,
    60                           'stacktrace has correct line number');
    61 }
    63 require('test').run(exports);

mercurial