addon-sdk/source/test/traits/inheritance-tests.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/traits/inheritance-tests.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     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 +var Trait = require("sdk/deprecated/light-traits").Trait;
    1.11 +
    1.12 +exports["test custom constructor and inherited toString"] = function(assert) {
    1.13 +  function Type() {
    1.14 +    return Object.create(Type.prototype);
    1.15 +  }
    1.16 +  Type.prototype = Trait({
    1.17 +    method: function method() {
    1.18 +      return 2;
    1.19 +    }
    1.20 +  }).create(Object.freeze(Type.prototype));
    1.21 +
    1.22 +  var fixture = Type();
    1.23 +
    1.24 +  assert.equal(fixture.constructor, Type, "must override constructor");
    1.25 +  assert.equal(fixture.toString(), "[object Type]", "must inherit toString");
    1.26 +};
    1.27 +
    1.28 +exports["test custom toString and inherited constructor"] = function(assert) {
    1.29 +  function Type() {
    1.30 +    return Object.create(Type.prototype);
    1.31 +  }
    1.32 +  Type.prototype = Trait({
    1.33 +    toString: function toString() {
    1.34 +      return "<toString>";
    1.35 +    }
    1.36 +  }).create();
    1.37 +
    1.38 +  var fixture = Type();
    1.39 +
    1.40 +  assert.equal(fixture.constructor, Trait, "must inherit constructor Trait");
    1.41 +  assert.equal(fixture.toString(), "<toString>", "Must override toString");
    1.42 +};
    1.43 +
    1.44 +exports["test custom toString and constructor"] = function(assert) {
    1.45 +  function Type() {
    1.46 +    return TypeTrait.create(Type.prototype);
    1.47 +  }
    1.48 +  Object.freeze(Type.prototype);
    1.49 +  var TypeTrait = Trait({
    1.50 +    toString: function toString() {
    1.51 +      return "<toString>";
    1.52 +    }
    1.53 +  });
    1.54 +
    1.55 +  var fixture = Type();
    1.56 +
    1.57 +  assert.equal(fixture.constructor, Type, "constructor is provided to create");
    1.58 +  assert.equal(fixture.toString(), "<toString>", "toString was overridden");
    1.59 +};
    1.60 +
    1.61 +exports["test resolve constructor"] = function (assert) {
    1.62 +  function Type() {}
    1.63 +  var T1 = Trait({ constructor: Type }).resolve({ constructor: '_foo' });
    1.64 +  var f1 = T1.create();
    1.65 +
    1.66 +  assert.equal(f1._foo, Type, "constructor was resolved");
    1.67 +  assert.equal(f1.constructor, Trait, "constructor of prototype is inherited");
    1.68 +  assert.equal(f1.toString(), "[object Trait]", "toString is inherited");
    1.69 +};
    1.70 +
    1.71 +exports["test compose read-only"] = function (assert) {
    1.72 +  function Type() {}
    1.73 +  Type.prototype = Trait.compose(Trait({}), {
    1.74 +    constructor: { value: Type },
    1.75 +    a: { value: "b", enumerable: true }
    1.76 +  }).resolve({ a: "b" }).create({ a: "a" });
    1.77 +
    1.78 +  var f1 = new Type();
    1.79 +
    1.80 +  assert.equal(Object.getPrototypeOf(f1), Type.prototype, "inherits correctly");
    1.81 +  assert.equal(f1.constructor, Type, "constructor was overridden");
    1.82 +  assert.equal(f1.toString(), "[object Type]", "toString was inherited");
    1.83 +  assert.equal(f1.a, "a", "property a was resolved");
    1.84 +  assert.equal(f1.b, "b", "property a was renamed to b");
    1.85 +  assert.ok(!Object.getOwnPropertyDescriptor(Type.prototype, "a"),
    1.86 +            "a is not on the prototype of the instance");
    1.87 +
    1.88 +  var proto = Object.getPrototypeOf(Type.prototype);
    1.89 +  var dc = Object.getOwnPropertyDescriptor(Type.prototype, "constructor");
    1.90 +  var db = Object.getOwnPropertyDescriptor(Type.prototype, "b");
    1.91 +  var da = Object.getOwnPropertyDescriptor(proto, "a");
    1.92 +
    1.93 +  assert.ok(!dc.writable, "constructor is not writable");
    1.94 +  assert.ok(!dc.enumerable, "constructor is not enumerable");
    1.95 +  assert.ok(dc.configurable, "constructor inherits configurability");
    1.96 +
    1.97 +  assert.ok(!db.writable, "a -> b is not writable");
    1.98 +  assert.ok(db.enumerable, "a -> b is enumerable");
    1.99 +  assert.ok(!db.configurable, "a -> b is not configurable");
   1.100 +
   1.101 +  assert.ok(da.writable, "a is writable");
   1.102 +  assert.ok(da.enumerable, "a is enumerable");
   1.103 +  assert.ok(da.configurable, "a is configurable");
   1.104 +};
   1.105 +
   1.106 +if (require.main == module)
   1.107 +  require("test").run(exports);

mercurial