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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-traceback.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     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 +var traceback = require("sdk/console/traceback");
    1.10 +var {Cc,Ci,Cr,Cu} = require("chrome");
    1.11 +const { on, off } = require("sdk/system/events");
    1.12 +
    1.13 +function throwNsIException() {
    1.14 +  var ios = Cc['@mozilla.org/network/io-service;1']
    1.15 +            .getService(Ci.nsIIOService);
    1.16 +  ios.newURI("i'm a malformed URI", null, null);
    1.17 +}
    1.18 +
    1.19 +function throwError() {
    1.20 +  throw new Error("foob");
    1.21 +}
    1.22 +
    1.23 +exports.testFormatDoesNotFetchRemoteFiles = function(assert) {
    1.24 +  ["http", "https"].forEach(
    1.25 +    function(scheme) {
    1.26 +      var httpRequests = 0;
    1.27 +      function onHttp() {
    1.28 +        httpRequests++;
    1.29 +      }
    1.30 +
    1.31 +      on("http-on-modify-request", onHttp);
    1.32 +
    1.33 +      try {
    1.34 +        var tb = [{filename: scheme + "://www.mozilla.org/",
    1.35 +                   lineNumber: 1,
    1.36 +                   name: "blah"}];
    1.37 +        traceback.format(tb);
    1.38 +      } catch (e) {
    1.39 +        assert.fail(e);
    1.40 +      }
    1.41 +
    1.42 +      off("http-on-modify-request", onHttp);
    1.43 +
    1.44 +      assert.equal(httpRequests, 0,
    1.45 +                       "traceback.format() does not make " +
    1.46 +                       scheme + " request");
    1.47 +    });
    1.48 +};
    1.49 +
    1.50 +exports.testFromExceptionWithString = function(assert) {
    1.51 +  try {
    1.52 +    throw "foob";
    1.53 +    assert.fail("an exception should've been thrown");
    1.54 +  } catch (e) {
    1.55 +    if (e == "foob") {
    1.56 +      var tb = traceback.fromException(e);
    1.57 +      assert.equal(tb.length, 0);
    1.58 +    }
    1.59 +    else {
    1.60 +      throw e;
    1.61 +    }
    1.62 +  }
    1.63 +};
    1.64 +
    1.65 +exports.testFormatWithString = function(assert) {
    1.66 +  // This can happen if e.g. a thrown exception was
    1.67 +  // a string instead of an Error instance.
    1.68 +  assert.equal(traceback.format("blah"),
    1.69 +		   "Traceback (most recent call last):");
    1.70 +};
    1.71 +
    1.72 +exports.testFromExceptionWithError = function(assert) {
    1.73 +  try {
    1.74 +    throwError();
    1.75 +    assert.fail("an exception should've been thrown");
    1.76 +  } catch (e) {
    1.77 +    if (e instanceof Error) {
    1.78 +      var tb = traceback.fromException(e);
    1.79 +
    1.80 +      var xulApp = require("sdk/system/xul-app");
    1.81 +      assert.equal(tb.slice(-1)[0].name, "throwError");
    1.82 +    }
    1.83 +    else {
    1.84 +      throw e;
    1.85 +    }
    1.86 +  }
    1.87 +};
    1.88 +
    1.89 +exports.testFromExceptionWithNsIException = function(assert) {
    1.90 +  try {
    1.91 +    throwNsIException();
    1.92 +    assert.fail("an exception should've been thrown");
    1.93 +  } catch (e) {
    1.94 +    if (e.result == Cr.NS_ERROR_MALFORMED_URI) {
    1.95 +      var tb = traceback.fromException(e);
    1.96 +      assert.equal(tb[tb.length - 1].name, "throwNsIException");
    1.97 +    }
    1.98 +    else {
    1.99 +      throw e;
   1.100 +    }
   1.101 +  }
   1.102 +};
   1.103 +
   1.104 +exports.testFormat = function(assert) {
   1.105 +  function getTraceback() {
   1.106 +    return traceback.format();
   1.107 +  }
   1.108 +
   1.109 +  var formatted = getTraceback();
   1.110 +  assert.equal(typeof(formatted), "string");
   1.111 +  var lines = formatted.split("\n");
   1.112 +
   1.113 +  assert.equal(lines[lines.length - 2].indexOf("getTraceback") > 0,
   1.114 +                   true,
   1.115 +                   "formatted traceback should include function name");
   1.116 +
   1.117 +  assert.equal(lines[lines.length - 1].trim(),
   1.118 +                   "return traceback.format();",
   1.119 +                   "formatted traceback should include source code");
   1.120 +};
   1.121 +
   1.122 +exports.testExceptionsWithEmptyStacksAreLogged = function(assert) {
   1.123 +  // Ensures that our fix to bug 550368 works.
   1.124 +  var sandbox = Cu.Sandbox("http://www.foo.com");
   1.125 +  var excRaised = false;
   1.126 +  try {
   1.127 +    Cu.evalInSandbox("returns 1 + 2;", sandbox, "1.8",
   1.128 +                     "blah.js", 25);
   1.129 +  } catch (e) {
   1.130 +    excRaised = true;
   1.131 +    var stack = traceback.fromException(e);
   1.132 +    assert.equal(stack.length, 1, "stack should have one frame");
   1.133 +
   1.134 +    assert.ok(stack[0].fileName, "blah.js", "frame should have filename");
   1.135 +    assert.ok(stack[0].lineNumber, 25, "frame should have line no");
   1.136 +    assert.equal(stack[0].name, null, "frame should have null function name");
   1.137 +  }
   1.138 +  if (!excRaised)
   1.139 +    assert.fail("Exception should have been raised.");
   1.140 +};
   1.141 +
   1.142 +require('sdk/test').run(exports);

mercurial