summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/test-reference.js
blob: 1fe08004ce177a154b15ac6aef45345e92357309 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* 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 { isWeak, WeakReference } = require("sdk/core/reference");
const { Class } = require("sdk/core/heritage");

exports["test non WeakReferences"] = assert => {
  assert.equal(isWeak({}), false,
               "regular objects aren't weak");

  assert.equal(isWeak([]), false,
               "arrays aren't weak");
};

exports["test a WeakReference"] = assert => {
  assert.equal(isWeak(new WeakReference()), true,
               "instances of WeakReferences are weak");
};

exports["test a subclass"] = assert => {
  const SubType = Class({
    extends: WeakReference,
    foo: function() {
    }
  });

  const x = new SubType();
  assert.equal(isWeak(x), true,
               "subtypes of WeakReferences are weak");

  const SubSubType = Class({
    extends: SubType
  });

  const y = new SubSubType();
  assert.equal(isWeak(y), true,
               "sub subtypes of WeakReferences are weak");
};

exports["test a mixin"] = assert => {
  const Mixer = Class({
    implements: [WeakReference]
  });

  assert.equal(isWeak(new Mixer()), true,
               "instances with mixed WeakReference are weak");

  const Mux = Class({});
  const MixMux = Class({
    extends: Mux,
    implements: [WeakReference]
  });

  assert.equal(isWeak(new MixMux()), true,
               "instances with mixed WeakReference that " +
               "inheret from other are weak");


  const Base = Class({});
  const ReMix = Class({
    extends: Base,
    implements: [MixMux]
  });

  assert.equal(isWeak(new ReMix()), true,
               "subtime of mix is still weak");
};

exports["test an override"] = assert => {
  const SubType = Class({ extends: WeakReference });
  isWeak.define(SubType, x => false);

  assert.equal(isWeak(new SubType()), false,
               "behavior of isWeak can still be overrided on subtype");

  const Mixer = Class({ implements: [WeakReference] });
  const SubMixer = Class({ extends: Mixer });
  isWeak.define(SubMixer, x => false);
  assert.equal(isWeak(new SubMixer()), false,
               "behavior of isWeak can still be overrided on mixed type");
};

exports["test an opt-in"] = assert => {
  var BaseType = Class({});
  var SubType = Class({ extends: BaseType });

  isWeak.define(SubType, x => true);
  assert.equal(isWeak(new SubType()), true,
               "isWeak can be just implemented");

  var x = {};
  isWeak.implement(x, x => true);
  assert.equal(isWeak(x), true,
               "isWeak can be implemented per instance");
};

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