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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-dispatcher.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     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 + */
     1.8 +"use strict";
     1.9 +
    1.10 +const { dispatcher } = require("sdk/util/dispatcher");
    1.11 +
    1.12 +exports["test dispatcher API"] = assert => {
    1.13 +  const dispatch = dispatcher();
    1.14 +
    1.15 +  assert.equal(typeof(dispatch), "function",
    1.16 +               "dispatch is a function");
    1.17 +
    1.18 +  assert.equal(typeof(dispatch.define), "function",
    1.19 +               "dispatch.define is a function");
    1.20 +
    1.21 +  assert.equal(typeof(dispatch.implement), "function",
    1.22 +               "dispatch.implement is a function");
    1.23 +
    1.24 +  assert.equal(typeof(dispatch.when), "function",
    1.25 +               "dispatch.when is a function");
    1.26 +};
    1.27 +
    1.28 +exports["test dispatcher"] = assert => {
    1.29 +  const isDuck = dispatcher();
    1.30 +
    1.31 +  const quacks = x => x && typeof(x.quack) === "function";
    1.32 +
    1.33 +  const Duck = function() {};
    1.34 +  const Goose = function() {};
    1.35 +
    1.36 +  const True = _ => true;
    1.37 +  const False = _ => false;
    1.38 +
    1.39 +
    1.40 +
    1.41 +  isDuck.define(Goose, False);
    1.42 +  isDuck.define(Duck, True);
    1.43 +  isDuck.when(quacks, True);
    1.44 +
    1.45 +  assert.equal(isDuck(new Goose()), false,
    1.46 +               "Goose ain't duck");
    1.47 +
    1.48 +  assert.equal(isDuck(new Duck()), true,
    1.49 +               "Ducks are ducks");
    1.50 +
    1.51 +  assert.equal(isDuck({ quack: () => "Quaaaaaack!" }), true,
    1.52 +               "It's a duck if it quacks");
    1.53 +
    1.54 +
    1.55 +  assert.throws(() => isDuck({}), /Type does not implements method/, "not implemneted");
    1.56 +
    1.57 +  isDuck.define(Object, False);
    1.58 +
    1.59 +  assert.equal(isDuck({}), false,
    1.60 +               "Ain't duck if it does not quacks!");
    1.61 +};
    1.62 +
    1.63 +exports["test redefining fails"] = assert => {
    1.64 +  const isPM = dispatcher();
    1.65 +  const isAfternoon = time => time.getHours() > 12;
    1.66 +
    1.67 +  isPM.when(isAfternoon, _ => true);
    1.68 +
    1.69 +  assert.equal(isPM(new Date(Date.parse("Jan 23, 1985, 13:20:00"))), true,
    1.70 +               "yeap afternoon");
    1.71 +  assert.equal(isPM({ getHours: _ => 17 }), true,
    1.72 +                "seems like afternoon");
    1.73 +
    1.74 +  assert.throws(() => isPM.when(isAfternoon, x => x > 12 && x < 24),
    1.75 +                /Already implemented for the given predicate/,
    1.76 +               "can't redefine on same predicate");
    1.77 +
    1.78 +};
    1.79 +
    1.80 +require("sdk/test").run(exports);

mercurial