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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-errors.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,72 @@
     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 +const errors = require("sdk/deprecated/errors");
    1.10 +
    1.11 +exports.testCatchAndLog = function(assert) {
    1.12 +  var caught = [];
    1.13 +  function dummyLog(e) { caught.push(e); }
    1.14 +
    1.15 +  var wrapped = errors.catchAndLog(function(x) {
    1.16 +    throw Error("blah" + x + this);
    1.17 +  }, "boop", dummyLog);
    1.18 +
    1.19 +  assert.equal(wrapped.call("hi", 1), "boop",
    1.20 +               "exceptions should be trapped, def. resp. returned");
    1.21 +  assert.equal(caught.length, 1,
    1.22 +               "logging function should be called");
    1.23 +  assert.equal(caught[0].message, "blah1hi",
    1.24 +               "args and this should be passed to wrapped func");
    1.25 +};
    1.26 +
    1.27 +exports.testCatchAndLogProps = function(assert) {
    1.28 +  var caught = [];
    1.29 +  function dummyLog(e) { caught.push(e); }
    1.30 +
    1.31 +  var thing = {
    1.32 +    foo: function(x) { throw Error("nowai" + x); },
    1.33 +    bar: function() { throw Error("blah"); },
    1.34 +    baz: function() { throw Error("fnarg"); }
    1.35 +  };
    1.36 +
    1.37 +  errors.catchAndLogProps(thing, "foo", "ugh", dummyLog);
    1.38 +
    1.39 +  assert.equal(thing.foo(1), "ugh",
    1.40 +                   "props should be wrapped");
    1.41 +  assert.equal(caught.length, 1,
    1.42 +                   "logging function should be called");
    1.43 +  assert.equal(caught[0].message, "nowai1",
    1.44 +                   "args should be passed to wrapped func");
    1.45 +  assert.throws(function() { thing.bar(); },
    1.46 +                /blah/,
    1.47 +                "non-wrapped props should be wrapped");
    1.48 +
    1.49 +  errors.catchAndLogProps(thing, ["bar", "baz"], "err", dummyLog);
    1.50 +  assert.ok((thing.bar() == thing.baz()) &&
    1.51 +              (thing.bar() == "err"),
    1.52 +              "multiple props should be wrapped if array passed in");
    1.53 +};
    1.54 +
    1.55 +exports.testCatchAndReturn = function(assert) {
    1.56 +  var wrapped = errors.catchAndReturn(function(x) {
    1.57 +    if (x == 1)
    1.58 +      return "one";
    1.59 +    if (x == 2)
    1.60 +      throw new Error("two");
    1.61 +    return this + x;
    1.62 +  });
    1.63 +
    1.64 +  assert.equal(wrapped(1).returnValue, "one",
    1.65 +               "arg should be passed; return value should be returned");
    1.66 +  assert.ok(wrapped(2).exception, "exception should be returned");
    1.67 +  assert.equal(wrapped(2).exception.message, "two", "message is correct");
    1.68 +  assert.ok(wrapped(2).exception.fileName.indexOf("test-errors.js") != -1,
    1.69 +            "filename is present");
    1.70 +  assert.ok(wrapped(2).exception.stack, "stack is available");
    1.71 +  assert.equal(wrapped.call("hi", 3).returnValue, "hi3",
    1.72 +               "`this` should be set correctly");
    1.73 +};
    1.74 +
    1.75 +require("sdk/test").run(exports);

mercurial