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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-require.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     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 +
     1.8 +'use strict';
     1.9 +
    1.10 +const traceback = require('sdk/console/traceback');
    1.11 +const REQUIRE_LINE_NO = 30;
    1.12 +
    1.13 +exports.test_no_args = function(assert) {
    1.14 +  let passed = tryRequireModule(assert);
    1.15 +  assert.ok(passed, 'require() with no args should raise helpful error');
    1.16 +};
    1.17 +
    1.18 +exports.test_invalid_sdk_module = function (assert) {
    1.19 +  let passed = tryRequireModule(assert, 'sdk/does-not-exist');
    1.20 +  assert.ok(passed, 'require() with an invalid sdk module should raise');
    1.21 +};
    1.22 +
    1.23 +exports.test_invalid_relative_module = function (assert) {
    1.24 +  let passed = tryRequireModule(assert, './does-not-exist');
    1.25 +  assert.ok(passed, 'require() with an invalid relative module should raise');
    1.26 +};
    1.27 +
    1.28 +
    1.29 +function tryRequireModule(assert, module) {
    1.30 +  let passed = false;
    1.31 +  try {
    1.32 +    // This line number is important, referenced in REQUIRE_LINE_NO
    1.33 +    let doesNotExist = require(module);
    1.34 +  } catch(e) {
    1.35 +    checkError(assert, module, e);
    1.36 +    passed = true;
    1.37 +  }
    1.38 +  return passed;
    1.39 +}
    1.40 +
    1.41 +function checkError (assert, name, e) {
    1.42 +  let msg = e.toString();
    1.43 +  if (name) {
    1.44 +    assert.ok(/is not found at/.test(msg),
    1.45 +      'Error message indicates module not found');
    1.46 +    assert.ok(msg.indexOf(name.replace(/\./g,'')) > -1,
    1.47 +      'Error message has the invalid module name in the message');
    1.48 +  }
    1.49 +  else {
    1.50 +    assert.equal(msg.indexOf('Error: you must provide a module name when calling require() from '), 0);
    1.51 +    assert.ok(msg.indexOf("test-require") !== -1, msg);
    1.52 +  }
    1.53 +
    1.54 +  // we'd also like to assert that the right filename
    1.55 +  // and linenumber is in the stacktrace
    1.56 +  let tb = traceback.fromException(e);
    1.57 +  // Get the second to last frame, as the last frame is inside
    1.58 +  // toolkit/loader
    1.59 +  let lastFrame = tb[tb.length-2];
    1.60 +  assert.ok(lastFrame.fileName.indexOf("test-require.js") !== -1,
    1.61 +                          'Filename found in stacktrace');
    1.62 +  assert.equal(lastFrame.lineNumber, REQUIRE_LINE_NO,
    1.63 +                          'stacktrace has correct line number');
    1.64 +}
    1.65 +
    1.66 +require('test').run(exports);

mercurial