|
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/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 var Trait = require("sdk/deprecated/light-traits").Trait; |
|
8 |
|
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)); |
|
18 |
|
19 var fixture = Type(); |
|
20 |
|
21 assert.equal(fixture.constructor, Type, "must override constructor"); |
|
22 assert.equal(fixture.toString(), "[object Type]", "must inherit toString"); |
|
23 }; |
|
24 |
|
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(); |
|
34 |
|
35 var fixture = Type(); |
|
36 |
|
37 assert.equal(fixture.constructor, Trait, "must inherit constructor Trait"); |
|
38 assert.equal(fixture.toString(), "<toString>", "Must override toString"); |
|
39 }; |
|
40 |
|
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 }); |
|
51 |
|
52 var fixture = Type(); |
|
53 |
|
54 assert.equal(fixture.constructor, Type, "constructor is provided to create"); |
|
55 assert.equal(fixture.toString(), "<toString>", "toString was overridden"); |
|
56 }; |
|
57 |
|
58 exports["test resolve constructor"] = function (assert) { |
|
59 function Type() {} |
|
60 var T1 = Trait({ constructor: Type }).resolve({ constructor: '_foo' }); |
|
61 var f1 = T1.create(); |
|
62 |
|
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 }; |
|
67 |
|
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" }); |
|
74 |
|
75 var f1 = new Type(); |
|
76 |
|
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"); |
|
84 |
|
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"); |
|
89 |
|
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"); |
|
93 |
|
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"); |
|
97 |
|
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 }; |
|
102 |
|
103 if (require.main == module) |
|
104 require("test").run(exports); |