aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.3/packages/api-utils/tests/traits/inheritance-tests.js
blob: 73a23b777098843658281b03ea476d7e364c9bfa (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
100
"use strict";

var Trait = require("light-traits").Trait;

exports["test custom constructor and inherited toString"] = function(assert) {
  function Type() {
    return Object.create(Type.prototype);
  }
  Type.prototype = Trait({
    method: function method() {
      return 2;
    }
  }).create(Object.freeze(Type.prototype));

  var fixture = Type();

  assert.equal(fixture.constructor, Type, "must override constructor");
  assert.equal(fixture.toString(), "[object Type]", "must inherit toString");
};

exports["test custom toString and inherited constructor"] = function(assert) {
  function Type() {
    return Object.create(Type.prototype);
  }
  Type.prototype = Trait({
    toString: function toString() {
      return "<toString>";
    }
  }).create();

  var fixture = Type();

  assert.equal(fixture.constructor, Trait, "must inherit constructor Trait");
  assert.equal(fixture.toString(), "<toString>", "Must override toString");
};

exports["test custom toString and constructor"] = function(assert) {
  function Type() {
    return TypeTrait.create(Type.prototype);
  }
  Object.freeze(Type.prototype);
  var TypeTrait = Trait({
    toString: function toString() {
      return "<toString>";
    }
  });

  var fixture = Type();

  assert.equal(fixture.constructor, Type, "constructor is provided to create");
  assert.equal(fixture.toString(), "<toString>", "toString was overridden");
};

exports["test resolve constructor"] = function (assert) {
  function Type() {}
  var T1 = Trait({ constructor: Type }).resolve({ constructor: '_foo' });
  var f1 = T1.create();

  assert.equal(f1._foo, Type, "constructor was resolved");
  assert.equal(f1.constructor, Trait, "constructor of prototype is inherited");
  assert.equal(f1.toString(), "[object Trait]", "toString is inherited");
};

exports["test compose read-only"] = function (assert) {
  function Type() {}
  Type.prototype = Trait.compose(Trait({}), {
    constructor: { value: Type },
    a: { value: "b", enumerable: true }
  }).resolve({ a: "b" }).create({ a: "a" });

  var f1 = new Type();

  assert.equal(Object.getPrototypeOf(f1), Type.prototype, "inherits correctly");
  assert.equal(f1.constructor, Type, "constructor was overridden");
  assert.equal(f1.toString(), "[object Type]", "toString was inherited");
  assert.equal(f1.a, "a", "property a was resolved");
  assert.equal(f1.b, "b", "property a was renamed to b");
  assert.ok(!Object.getOwnPropertyDescriptor(Type.prototype, "a"),
            "a is not on the prototype of the instance");

  var proto = Object.getPrototypeOf(Type.prototype);
  var dc = Object.getOwnPropertyDescriptor(Type.prototype, "constructor");
  var db = Object.getOwnPropertyDescriptor(Type.prototype, "b");
  var da = Object.getOwnPropertyDescriptor(proto, "a");

  assert.ok(!dc.writable, "constructor is not writable");
  assert.ok(!dc.enumerable, "constructor is not enumerable");
  assert.ok(dc.configurable, "constructor inherits configurability");

  assert.ok(!db.writable, "a -> b is not writable");
  assert.ok(db.enumerable, "a -> b is enumerable");
  assert.ok(!db.configurable, "a -> b is not configurable");

  assert.ok(da.writable, "a is writable");
  assert.ok(da.enumerable, "a is enumerable");
  assert.ok(da.configurable, "a is configurable");
};

if (require.main == module)
  require("test").run(exports);