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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4 "use strict";
michael@0 5
michael@0 6 const { isWeak, WeakReference } = require("sdk/core/reference");
michael@0 7 const { Class } = require("sdk/core/heritage");
michael@0 8
michael@0 9 exports["test non WeakReferences"] = assert => {
michael@0 10 assert.equal(isWeak({}), false,
michael@0 11 "regular objects aren't weak");
michael@0 12
michael@0 13 assert.equal(isWeak([]), false,
michael@0 14 "arrays aren't weak");
michael@0 15 };
michael@0 16
michael@0 17 exports["test a WeakReference"] = assert => {
michael@0 18 assert.equal(isWeak(new WeakReference()), true,
michael@0 19 "instances of WeakReferences are weak");
michael@0 20 };
michael@0 21
michael@0 22 exports["test a subclass"] = assert => {
michael@0 23 const SubType = Class({
michael@0 24 extends: WeakReference,
michael@0 25 foo: function() {
michael@0 26 }
michael@0 27 });
michael@0 28
michael@0 29 const x = new SubType();
michael@0 30 assert.equal(isWeak(x), true,
michael@0 31 "subtypes of WeakReferences are weak");
michael@0 32
michael@0 33 const SubSubType = Class({
michael@0 34 extends: SubType
michael@0 35 });
michael@0 36
michael@0 37 const y = new SubSubType();
michael@0 38 assert.equal(isWeak(y), true,
michael@0 39 "sub subtypes of WeakReferences are weak");
michael@0 40 };
michael@0 41
michael@0 42 exports["test a mixin"] = assert => {
michael@0 43 const Mixer = Class({
michael@0 44 implements: [WeakReference]
michael@0 45 });
michael@0 46
michael@0 47 assert.equal(isWeak(new Mixer()), true,
michael@0 48 "instances with mixed WeakReference are weak");
michael@0 49
michael@0 50 const Mux = Class({});
michael@0 51 const MixMux = Class({
michael@0 52 extends: Mux,
michael@0 53 implements: [WeakReference]
michael@0 54 });
michael@0 55
michael@0 56 assert.equal(isWeak(new MixMux()), true,
michael@0 57 "instances with mixed WeakReference that " +
michael@0 58 "inheret from other are weak");
michael@0 59
michael@0 60
michael@0 61 const Base = Class({});
michael@0 62 const ReMix = Class({
michael@0 63 extends: Base,
michael@0 64 implements: [MixMux]
michael@0 65 });
michael@0 66
michael@0 67 assert.equal(isWeak(new ReMix()), true,
michael@0 68 "subtime of mix is still weak");
michael@0 69 };
michael@0 70
michael@0 71 exports["test an override"] = assert => {
michael@0 72 const SubType = Class({ extends: WeakReference });
michael@0 73 isWeak.define(SubType, x => false);
michael@0 74
michael@0 75 assert.equal(isWeak(new SubType()), false,
michael@0 76 "behavior of isWeak can still be overrided on subtype");
michael@0 77
michael@0 78 const Mixer = Class({ implements: [WeakReference] });
michael@0 79 const SubMixer = Class({ extends: Mixer });
michael@0 80 isWeak.define(SubMixer, x => false);
michael@0 81 assert.equal(isWeak(new SubMixer()), false,
michael@0 82 "behavior of isWeak can still be overrided on mixed type");
michael@0 83 };
michael@0 84
michael@0 85 exports["test an opt-in"] = assert => {
michael@0 86 var BaseType = Class({});
michael@0 87 var SubType = Class({ extends: BaseType });
michael@0 88
michael@0 89 isWeak.define(SubType, x => true);
michael@0 90 assert.equal(isWeak(new SubType()), true,
michael@0 91 "isWeak can be just implemented");
michael@0 92
michael@0 93 var x = {};
michael@0 94 isWeak.implement(x, x => true);
michael@0 95 assert.equal(isWeak(x), true,
michael@0 96 "isWeak can be implemented per instance");
michael@0 97 };
michael@0 98
michael@0 99 require("sdk/test").run(exports);

mercurial