summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-dispatcher.js
blob: 437d751761c78734659799f7e72aa933ba2f4421 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

const { dispatcher } = require("sdk/util/dispatcher");

exports["test dispatcher API"] = assert => {
  const dispatch = dispatcher();

  assert.equal(typeof(dispatch), "function",
               "dispatch is a function");

  assert.equal(typeof(dispatch.define), "function",
               "dispatch.define is a function");

  assert.equal(typeof(dispatch.implement), "function",
               "dispatch.implement is a function");

  assert.equal(typeof(dispatch.when), "function",
               "dispatch.when is a function");
};

exports["test dispatcher"] = assert => {
  const isDuck = dispatcher();

  const quacks = x => x && typeof(x.quack) === "function";

  const Duck = function() {};
  const Goose = function() {};

  const True = _ => true;
  const False = _ => false;



  isDuck.define(Goose, False);
  isDuck.define(Duck, True);
  isDuck.when(quacks, True);

  assert.equal(isDuck(new Goose()), false,
               "Goose ain't duck");

  assert.equal(isDuck(new Duck()), true,
               "Ducks are ducks");

  assert.equal(isDuck({ quack: () => "Quaaaaaack!" }), true,
               "It's a duck if it quacks");


  assert.throws(() => isDuck({}), /Type does not implements method/, "not implemneted");

  isDuck.define(Object, False);

  assert.equal(isDuck({}), false,
               "Ain't duck if it does not quacks!");
};

exports["test redefining fails"] = assert => {
  const isPM = dispatcher();
  const isAfternoon = time => time.getHours() > 12;

  isPM.when(isAfternoon, _ => true);

  assert.equal(isPM(new Date(Date.parse("Jan 23, 1985, 13:20:00"))), true,
               "yeap afternoon");
  assert.equal(isPM({ getHours: _ => 17 }), true,
                "seems like afternoon");

  assert.throws(() => isPM.when(isAfternoon, x => x > 12 && x < 24),
                /Already implemented for the given predicate/,
               "can't redefine on same predicate");

};

require("sdk/test").run(exports);