michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: var Cortex = require("sdk/deprecated/cortex").Cortex; michael@0: michael@0: exports["test property changes propagate"] = function (assert) { michael@0: var source = { michael@0: _foo: "secret", michael@0: get foo() { michael@0: return this._foo; michael@0: }, michael@0: set foo(value) { michael@0: this._foo = value; michael@0: }, michael@0: get getOnly() { michael@0: return this._foo; michael@0: }, michael@0: set setOnly(value) { michael@0: this._setOnly = value; michael@0: }, michael@0: bar: "public", michael@0: method: function method(a, b) { michael@0: return this._foo + a + b michael@0: } michael@0: }; michael@0: var fixture = Cortex(source); michael@0: michael@0: assert.ok(!('_foo' in fixture), michael@0: "properties that start with `_` are omitted"); michael@0: assert.equal(fixture.foo, "secret", "get accessor alias works"); michael@0: fixture.foo = "new secret"; michael@0: assert.equal(fixture.foo, "new secret", "set accessor alias works"); michael@0: assert.equal(source.foo, "new secret", "accessor delegates to the source"); michael@0: assert.equal(fixture.bar, "public", "data property alias works"); michael@0: fixture.bar = "bar"; michael@0: assert.equal(source.bar, "bar", "data property change propagates"); michael@0: source.bar = "foo" michael@0: assert.equal(fixture.bar, "foo", "data property change probagets back"); michael@0: assert.equal(fixture.method("a", "b"), "new secretab", michael@0: "public methods are callable"); michael@0: assert.equal(fixture.method.call({ _foo: "test" }, " a,", "b"), michael@0: "new secret a,b", michael@0: "`this` pseudo-variable can not be passed through call."); michael@0: assert.equal(fixture.method.apply({ _foo: "test" }, [" a,", "b"]), michael@0: "new secret a,b", michael@0: "`this` pseudo-variable can not be passed through apply."); michael@0: assert.equal(fixture.getOnly, source._foo, michael@0: "getter returned property of wrapped object"); michael@0: fixture.setOnly = 'bar' michael@0: assert.equal(source._setOnly, 'bar', "setter modified wrapped object") michael@0: }; michael@0: michael@0: michael@0: exports["test immunity of inheritance"] = function(assert) { michael@0: function Type() {} michael@0: Type.prototype = { michael@0: constructor: Type, michael@0: _bar: 2, michael@0: bar: 3, michael@0: get_Foo: function getFoo() { michael@0: return this._foo; michael@0: } michael@0: } michael@0: var source = Object.create(Type.prototype, { michael@0: _foo: { value: 'secret' }, michael@0: getBar: { value: function get_Bar() { michael@0: return this.bar michael@0: }}, michael@0: get_Bar: { value: function getBar() { michael@0: return this._bar michael@0: }} michael@0: }); michael@0: michael@0: var fixture = Cortex(source); michael@0: michael@0: assert.ok(Cortex({}, null, Type.prototype) instanceof Type, michael@0: "if custom prototype is providede cortex will inherit from it"); michael@0: assert.ok(fixture instanceof Type, michael@0: "if no prototype is given cortex inherits from object's prototype"); michael@0: michael@0: source.bar += 1; michael@0: assert.notEqual(fixture.bar, source.bar, michael@0: "chages of properties don't propagate to non-aliases"); michael@0: assert.equal(fixture.getBar(), source.bar, michael@0: "methods accessing public properties are bound to the source"); michael@0: michael@0: fixture._bar += 1; michael@0: assert.notEqual(fixture._bar, source._bar, michael@0: "changes of non aliased properties don't propagate"); michael@0: assert.equal(fixture.get_Bar(), source._bar, michael@0: "methods accessing privates are bound to the source"); michael@0: assert.notEqual(fixture.get_Foo(), source._foo, michael@0: "prototoype methods are not bound to the source"); michael@0: } michael@0: michael@0: exports["test customized public properties"] = function(assert) { michael@0: var source = { michael@0: _a: 'a', michael@0: b: 'b', michael@0: get: function get(name) { michael@0: return this[name]; michael@0: } michael@0: }; michael@0: michael@0: var fixture = Cortex(source, ['_a', 'get']); michael@0: fixture._a += "#change"; michael@0: michael@0: michael@0: assert.ok(!("b" in fixture), "non-public own property is not defined"); michael@0: assert.equal(fixture.get("b"), source.b, michael@0: "public methods preserve access to the private properties"); michael@0: assert.equal(fixture._a, source._a, michael@0: "custom public property changes propagate"); michael@0: } michael@0: michael@0: //if (require.main == module) michael@0: require("test").run(exports);