addon-sdk/source/test/test-traceback.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 "use strict";
michael@0 5
michael@0 6 var traceback = require("sdk/console/traceback");
michael@0 7 var {Cc,Ci,Cr,Cu} = require("chrome");
michael@0 8 const { on, off } = require("sdk/system/events");
michael@0 9
michael@0 10 function throwNsIException() {
michael@0 11 var ios = Cc['@mozilla.org/network/io-service;1']
michael@0 12 .getService(Ci.nsIIOService);
michael@0 13 ios.newURI("i'm a malformed URI", null, null);
michael@0 14 }
michael@0 15
michael@0 16 function throwError() {
michael@0 17 throw new Error("foob");
michael@0 18 }
michael@0 19
michael@0 20 exports.testFormatDoesNotFetchRemoteFiles = function(assert) {
michael@0 21 ["http", "https"].forEach(
michael@0 22 function(scheme) {
michael@0 23 var httpRequests = 0;
michael@0 24 function onHttp() {
michael@0 25 httpRequests++;
michael@0 26 }
michael@0 27
michael@0 28 on("http-on-modify-request", onHttp);
michael@0 29
michael@0 30 try {
michael@0 31 var tb = [{filename: scheme + "://www.mozilla.org/",
michael@0 32 lineNumber: 1,
michael@0 33 name: "blah"}];
michael@0 34 traceback.format(tb);
michael@0 35 } catch (e) {
michael@0 36 assert.fail(e);
michael@0 37 }
michael@0 38
michael@0 39 off("http-on-modify-request", onHttp);
michael@0 40
michael@0 41 assert.equal(httpRequests, 0,
michael@0 42 "traceback.format() does not make " +
michael@0 43 scheme + " request");
michael@0 44 });
michael@0 45 };
michael@0 46
michael@0 47 exports.testFromExceptionWithString = function(assert) {
michael@0 48 try {
michael@0 49 throw "foob";
michael@0 50 assert.fail("an exception should've been thrown");
michael@0 51 } catch (e) {
michael@0 52 if (e == "foob") {
michael@0 53 var tb = traceback.fromException(e);
michael@0 54 assert.equal(tb.length, 0);
michael@0 55 }
michael@0 56 else {
michael@0 57 throw e;
michael@0 58 }
michael@0 59 }
michael@0 60 };
michael@0 61
michael@0 62 exports.testFormatWithString = function(assert) {
michael@0 63 // This can happen if e.g. a thrown exception was
michael@0 64 // a string instead of an Error instance.
michael@0 65 assert.equal(traceback.format("blah"),
michael@0 66 "Traceback (most recent call last):");
michael@0 67 };
michael@0 68
michael@0 69 exports.testFromExceptionWithError = function(assert) {
michael@0 70 try {
michael@0 71 throwError();
michael@0 72 assert.fail("an exception should've been thrown");
michael@0 73 } catch (e) {
michael@0 74 if (e instanceof Error) {
michael@0 75 var tb = traceback.fromException(e);
michael@0 76
michael@0 77 var xulApp = require("sdk/system/xul-app");
michael@0 78 assert.equal(tb.slice(-1)[0].name, "throwError");
michael@0 79 }
michael@0 80 else {
michael@0 81 throw e;
michael@0 82 }
michael@0 83 }
michael@0 84 };
michael@0 85
michael@0 86 exports.testFromExceptionWithNsIException = function(assert) {
michael@0 87 try {
michael@0 88 throwNsIException();
michael@0 89 assert.fail("an exception should've been thrown");
michael@0 90 } catch (e) {
michael@0 91 if (e.result == Cr.NS_ERROR_MALFORMED_URI) {
michael@0 92 var tb = traceback.fromException(e);
michael@0 93 assert.equal(tb[tb.length - 1].name, "throwNsIException");
michael@0 94 }
michael@0 95 else {
michael@0 96 throw e;
michael@0 97 }
michael@0 98 }
michael@0 99 };
michael@0 100
michael@0 101 exports.testFormat = function(assert) {
michael@0 102 function getTraceback() {
michael@0 103 return traceback.format();
michael@0 104 }
michael@0 105
michael@0 106 var formatted = getTraceback();
michael@0 107 assert.equal(typeof(formatted), "string");
michael@0 108 var lines = formatted.split("\n");
michael@0 109
michael@0 110 assert.equal(lines[lines.length - 2].indexOf("getTraceback") > 0,
michael@0 111 true,
michael@0 112 "formatted traceback should include function name");
michael@0 113
michael@0 114 assert.equal(lines[lines.length - 1].trim(),
michael@0 115 "return traceback.format();",
michael@0 116 "formatted traceback should include source code");
michael@0 117 };
michael@0 118
michael@0 119 exports.testExceptionsWithEmptyStacksAreLogged = function(assert) {
michael@0 120 // Ensures that our fix to bug 550368 works.
michael@0 121 var sandbox = Cu.Sandbox("http://www.foo.com");
michael@0 122 var excRaised = false;
michael@0 123 try {
michael@0 124 Cu.evalInSandbox("returns 1 + 2;", sandbox, "1.8",
michael@0 125 "blah.js", 25);
michael@0 126 } catch (e) {
michael@0 127 excRaised = true;
michael@0 128 var stack = traceback.fromException(e);
michael@0 129 assert.equal(stack.length, 1, "stack should have one frame");
michael@0 130
michael@0 131 assert.ok(stack[0].fileName, "blah.js", "frame should have filename");
michael@0 132 assert.ok(stack[0].lineNumber, 25, "frame should have line no");
michael@0 133 assert.equal(stack[0].name, null, "frame should have null function name");
michael@0 134 }
michael@0 135 if (!excRaised)
michael@0 136 assert.fail("Exception should have been raised.");
michael@0 137 };
michael@0 138
michael@0 139 require('sdk/test').run(exports);

mercurial