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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 const errors = require("sdk/deprecated/errors");
michael@0 7
michael@0 8 exports.testCatchAndLog = function(assert) {
michael@0 9 var caught = [];
michael@0 10 function dummyLog(e) { caught.push(e); }
michael@0 11
michael@0 12 var wrapped = errors.catchAndLog(function(x) {
michael@0 13 throw Error("blah" + x + this);
michael@0 14 }, "boop", dummyLog);
michael@0 15
michael@0 16 assert.equal(wrapped.call("hi", 1), "boop",
michael@0 17 "exceptions should be trapped, def. resp. returned");
michael@0 18 assert.equal(caught.length, 1,
michael@0 19 "logging function should be called");
michael@0 20 assert.equal(caught[0].message, "blah1hi",
michael@0 21 "args and this should be passed to wrapped func");
michael@0 22 };
michael@0 23
michael@0 24 exports.testCatchAndLogProps = function(assert) {
michael@0 25 var caught = [];
michael@0 26 function dummyLog(e) { caught.push(e); }
michael@0 27
michael@0 28 var thing = {
michael@0 29 foo: function(x) { throw Error("nowai" + x); },
michael@0 30 bar: function() { throw Error("blah"); },
michael@0 31 baz: function() { throw Error("fnarg"); }
michael@0 32 };
michael@0 33
michael@0 34 errors.catchAndLogProps(thing, "foo", "ugh", dummyLog);
michael@0 35
michael@0 36 assert.equal(thing.foo(1), "ugh",
michael@0 37 "props should be wrapped");
michael@0 38 assert.equal(caught.length, 1,
michael@0 39 "logging function should be called");
michael@0 40 assert.equal(caught[0].message, "nowai1",
michael@0 41 "args should be passed to wrapped func");
michael@0 42 assert.throws(function() { thing.bar(); },
michael@0 43 /blah/,
michael@0 44 "non-wrapped props should be wrapped");
michael@0 45
michael@0 46 errors.catchAndLogProps(thing, ["bar", "baz"], "err", dummyLog);
michael@0 47 assert.ok((thing.bar() == thing.baz()) &&
michael@0 48 (thing.bar() == "err"),
michael@0 49 "multiple props should be wrapped if array passed in");
michael@0 50 };
michael@0 51
michael@0 52 exports.testCatchAndReturn = function(assert) {
michael@0 53 var wrapped = errors.catchAndReturn(function(x) {
michael@0 54 if (x == 1)
michael@0 55 return "one";
michael@0 56 if (x == 2)
michael@0 57 throw new Error("two");
michael@0 58 return this + x;
michael@0 59 });
michael@0 60
michael@0 61 assert.equal(wrapped(1).returnValue, "one",
michael@0 62 "arg should be passed; return value should be returned");
michael@0 63 assert.ok(wrapped(2).exception, "exception should be returned");
michael@0 64 assert.equal(wrapped(2).exception.message, "two", "message is correct");
michael@0 65 assert.ok(wrapped(2).exception.fileName.indexOf("test-errors.js") != -1,
michael@0 66 "filename is present");
michael@0 67 assert.ok(wrapped(2).exception.stack, "stack is available");
michael@0 68 assert.equal(wrapped.call("hi", 3).returnValue, "hi3",
michael@0 69 "`this` should be set correctly");
michael@0 70 };
michael@0 71
michael@0 72 require("sdk/test").run(exports);

mercurial