|
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 Cortex = require("sdk/deprecated/cortex").Cortex; |
|
8 |
|
9 exports["test property changes propagate"] = function (assert) { |
|
10 var source = { |
|
11 _foo: "secret", |
|
12 get foo() { |
|
13 return this._foo; |
|
14 }, |
|
15 set foo(value) { |
|
16 this._foo = value; |
|
17 }, |
|
18 get getOnly() { |
|
19 return this._foo; |
|
20 }, |
|
21 set setOnly(value) { |
|
22 this._setOnly = value; |
|
23 }, |
|
24 bar: "public", |
|
25 method: function method(a, b) { |
|
26 return this._foo + a + b |
|
27 } |
|
28 }; |
|
29 var fixture = Cortex(source); |
|
30 |
|
31 assert.ok(!('_foo' in fixture), |
|
32 "properties that start with `_` are omitted"); |
|
33 assert.equal(fixture.foo, "secret", "get accessor alias works"); |
|
34 fixture.foo = "new secret"; |
|
35 assert.equal(fixture.foo, "new secret", "set accessor alias works"); |
|
36 assert.equal(source.foo, "new secret", "accessor delegates to the source"); |
|
37 assert.equal(fixture.bar, "public", "data property alias works"); |
|
38 fixture.bar = "bar"; |
|
39 assert.equal(source.bar, "bar", "data property change propagates"); |
|
40 source.bar = "foo" |
|
41 assert.equal(fixture.bar, "foo", "data property change probagets back"); |
|
42 assert.equal(fixture.method("a", "b"), "new secretab", |
|
43 "public methods are callable"); |
|
44 assert.equal(fixture.method.call({ _foo: "test" }, " a,", "b"), |
|
45 "new secret a,b", |
|
46 "`this` pseudo-variable can not be passed through call."); |
|
47 assert.equal(fixture.method.apply({ _foo: "test" }, [" a,", "b"]), |
|
48 "new secret a,b", |
|
49 "`this` pseudo-variable can not be passed through apply."); |
|
50 assert.equal(fixture.getOnly, source._foo, |
|
51 "getter returned property of wrapped object"); |
|
52 fixture.setOnly = 'bar' |
|
53 assert.equal(source._setOnly, 'bar', "setter modified wrapped object") |
|
54 }; |
|
55 |
|
56 |
|
57 exports["test immunity of inheritance"] = function(assert) { |
|
58 function Type() {} |
|
59 Type.prototype = { |
|
60 constructor: Type, |
|
61 _bar: 2, |
|
62 bar: 3, |
|
63 get_Foo: function getFoo() { |
|
64 return this._foo; |
|
65 } |
|
66 } |
|
67 var source = Object.create(Type.prototype, { |
|
68 _foo: { value: 'secret' }, |
|
69 getBar: { value: function get_Bar() { |
|
70 return this.bar |
|
71 }}, |
|
72 get_Bar: { value: function getBar() { |
|
73 return this._bar |
|
74 }} |
|
75 }); |
|
76 |
|
77 var fixture = Cortex(source); |
|
78 |
|
79 assert.ok(Cortex({}, null, Type.prototype) instanceof Type, |
|
80 "if custom prototype is providede cortex will inherit from it"); |
|
81 assert.ok(fixture instanceof Type, |
|
82 "if no prototype is given cortex inherits from object's prototype"); |
|
83 |
|
84 source.bar += 1; |
|
85 assert.notEqual(fixture.bar, source.bar, |
|
86 "chages of properties don't propagate to non-aliases"); |
|
87 assert.equal(fixture.getBar(), source.bar, |
|
88 "methods accessing public properties are bound to the source"); |
|
89 |
|
90 fixture._bar += 1; |
|
91 assert.notEqual(fixture._bar, source._bar, |
|
92 "changes of non aliased properties don't propagate"); |
|
93 assert.equal(fixture.get_Bar(), source._bar, |
|
94 "methods accessing privates are bound to the source"); |
|
95 assert.notEqual(fixture.get_Foo(), source._foo, |
|
96 "prototoype methods are not bound to the source"); |
|
97 } |
|
98 |
|
99 exports["test customized public properties"] = function(assert) { |
|
100 var source = { |
|
101 _a: 'a', |
|
102 b: 'b', |
|
103 get: function get(name) { |
|
104 return this[name]; |
|
105 } |
|
106 }; |
|
107 |
|
108 var fixture = Cortex(source, ['_a', 'get']); |
|
109 fixture._a += "#change"; |
|
110 |
|
111 |
|
112 assert.ok(!("b" in fixture), "non-public own property is not defined"); |
|
113 assert.equal(fixture.get("b"), source.b, |
|
114 "public methods preserve access to the private properties"); |
|
115 assert.equal(fixture._a, source._a, |
|
116 "custom public property changes propagate"); |
|
117 } |
|
118 |
|
119 //if (require.main == module) |
|
120 require("test").run(exports); |