addon-sdk/source/test/traits/inheritance-tests.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.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 var Trait = require("sdk/deprecated/light-traits").Trait;
     9 exports["test custom constructor and inherited toString"] = function(assert) {
    10   function Type() {
    11     return Object.create(Type.prototype);
    12   }
    13   Type.prototype = Trait({
    14     method: function method() {
    15       return 2;
    16     }
    17   }).create(Object.freeze(Type.prototype));
    19   var fixture = Type();
    21   assert.equal(fixture.constructor, Type, "must override constructor");
    22   assert.equal(fixture.toString(), "[object Type]", "must inherit toString");
    23 };
    25 exports["test custom toString and inherited constructor"] = function(assert) {
    26   function Type() {
    27     return Object.create(Type.prototype);
    28   }
    29   Type.prototype = Trait({
    30     toString: function toString() {
    31       return "<toString>";
    32     }
    33   }).create();
    35   var fixture = Type();
    37   assert.equal(fixture.constructor, Trait, "must inherit constructor Trait");
    38   assert.equal(fixture.toString(), "<toString>", "Must override toString");
    39 };
    41 exports["test custom toString and constructor"] = function(assert) {
    42   function Type() {
    43     return TypeTrait.create(Type.prototype);
    44   }
    45   Object.freeze(Type.prototype);
    46   var TypeTrait = Trait({
    47     toString: function toString() {
    48       return "<toString>";
    49     }
    50   });
    52   var fixture = Type();
    54   assert.equal(fixture.constructor, Type, "constructor is provided to create");
    55   assert.equal(fixture.toString(), "<toString>", "toString was overridden");
    56 };
    58 exports["test resolve constructor"] = function (assert) {
    59   function Type() {}
    60   var T1 = Trait({ constructor: Type }).resolve({ constructor: '_foo' });
    61   var f1 = T1.create();
    63   assert.equal(f1._foo, Type, "constructor was resolved");
    64   assert.equal(f1.constructor, Trait, "constructor of prototype is inherited");
    65   assert.equal(f1.toString(), "[object Trait]", "toString is inherited");
    66 };
    68 exports["test compose read-only"] = function (assert) {
    69   function Type() {}
    70   Type.prototype = Trait.compose(Trait({}), {
    71     constructor: { value: Type },
    72     a: { value: "b", enumerable: true }
    73   }).resolve({ a: "b" }).create({ a: "a" });
    75   var f1 = new Type();
    77   assert.equal(Object.getPrototypeOf(f1), Type.prototype, "inherits correctly");
    78   assert.equal(f1.constructor, Type, "constructor was overridden");
    79   assert.equal(f1.toString(), "[object Type]", "toString was inherited");
    80   assert.equal(f1.a, "a", "property a was resolved");
    81   assert.equal(f1.b, "b", "property a was renamed to b");
    82   assert.ok(!Object.getOwnPropertyDescriptor(Type.prototype, "a"),
    83             "a is not on the prototype of the instance");
    85   var proto = Object.getPrototypeOf(Type.prototype);
    86   var dc = Object.getOwnPropertyDescriptor(Type.prototype, "constructor");
    87   var db = Object.getOwnPropertyDescriptor(Type.prototype, "b");
    88   var da = Object.getOwnPropertyDescriptor(proto, "a");
    90   assert.ok(!dc.writable, "constructor is not writable");
    91   assert.ok(!dc.enumerable, "constructor is not enumerable");
    92   assert.ok(dc.configurable, "constructor inherits configurability");
    94   assert.ok(!db.writable, "a -> b is not writable");
    95   assert.ok(db.enumerable, "a -> b is enumerable");
    96   assert.ok(!db.configurable, "a -> b is not configurable");
    98   assert.ok(da.writable, "a is writable");
    99   assert.ok(da.enumerable, "a is enumerable");
   100   assert.ok(da.configurable, "a is configurable");
   101 };
   103 if (require.main == module)
   104   require("test").run(exports);

mercurial