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 Trait = require("sdk/deprecated/light-traits").Trait; michael@0: michael@0: exports["test custom constructor and inherited toString"] = function(assert) { michael@0: function Type() { michael@0: return Object.create(Type.prototype); michael@0: } michael@0: Type.prototype = Trait({ michael@0: method: function method() { michael@0: return 2; michael@0: } michael@0: }).create(Object.freeze(Type.prototype)); michael@0: michael@0: var fixture = Type(); michael@0: michael@0: assert.equal(fixture.constructor, Type, "must override constructor"); michael@0: assert.equal(fixture.toString(), "[object Type]", "must inherit toString"); michael@0: }; michael@0: michael@0: exports["test custom toString and inherited constructor"] = function(assert) { michael@0: function Type() { michael@0: return Object.create(Type.prototype); michael@0: } michael@0: Type.prototype = Trait({ michael@0: toString: function toString() { michael@0: return ""; michael@0: } michael@0: }).create(); michael@0: michael@0: var fixture = Type(); michael@0: michael@0: assert.equal(fixture.constructor, Trait, "must inherit constructor Trait"); michael@0: assert.equal(fixture.toString(), "", "Must override toString"); michael@0: }; michael@0: michael@0: exports["test custom toString and constructor"] = function(assert) { michael@0: function Type() { michael@0: return TypeTrait.create(Type.prototype); michael@0: } michael@0: Object.freeze(Type.prototype); michael@0: var TypeTrait = Trait({ michael@0: toString: function toString() { michael@0: return ""; michael@0: } michael@0: }); michael@0: michael@0: var fixture = Type(); michael@0: michael@0: assert.equal(fixture.constructor, Type, "constructor is provided to create"); michael@0: assert.equal(fixture.toString(), "", "toString was overridden"); michael@0: }; michael@0: michael@0: exports["test resolve constructor"] = function (assert) { michael@0: function Type() {} michael@0: var T1 = Trait({ constructor: Type }).resolve({ constructor: '_foo' }); michael@0: var f1 = T1.create(); michael@0: michael@0: assert.equal(f1._foo, Type, "constructor was resolved"); michael@0: assert.equal(f1.constructor, Trait, "constructor of prototype is inherited"); michael@0: assert.equal(f1.toString(), "[object Trait]", "toString is inherited"); michael@0: }; michael@0: michael@0: exports["test compose read-only"] = function (assert) { michael@0: function Type() {} michael@0: Type.prototype = Trait.compose(Trait({}), { michael@0: constructor: { value: Type }, michael@0: a: { value: "b", enumerable: true } michael@0: }).resolve({ a: "b" }).create({ a: "a" }); michael@0: michael@0: var f1 = new Type(); michael@0: michael@0: assert.equal(Object.getPrototypeOf(f1), Type.prototype, "inherits correctly"); michael@0: assert.equal(f1.constructor, Type, "constructor was overridden"); michael@0: assert.equal(f1.toString(), "[object Type]", "toString was inherited"); michael@0: assert.equal(f1.a, "a", "property a was resolved"); michael@0: assert.equal(f1.b, "b", "property a was renamed to b"); michael@0: assert.ok(!Object.getOwnPropertyDescriptor(Type.prototype, "a"), michael@0: "a is not on the prototype of the instance"); michael@0: michael@0: var proto = Object.getPrototypeOf(Type.prototype); michael@0: var dc = Object.getOwnPropertyDescriptor(Type.prototype, "constructor"); michael@0: var db = Object.getOwnPropertyDescriptor(Type.prototype, "b"); michael@0: var da = Object.getOwnPropertyDescriptor(proto, "a"); michael@0: michael@0: assert.ok(!dc.writable, "constructor is not writable"); michael@0: assert.ok(!dc.enumerable, "constructor is not enumerable"); michael@0: assert.ok(dc.configurable, "constructor inherits configurability"); michael@0: michael@0: assert.ok(!db.writable, "a -> b is not writable"); michael@0: assert.ok(db.enumerable, "a -> b is enumerable"); michael@0: assert.ok(!db.configurable, "a -> b is not configurable"); michael@0: michael@0: assert.ok(da.writable, "a is writable"); michael@0: assert.ok(da.enumerable, "a is enumerable"); michael@0: assert.ok(da.configurable, "a is configurable"); michael@0: }; michael@0: michael@0: if (require.main == module) michael@0: require("test").run(exports);