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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial