inheritance-tests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. "use strict";
  5. var Trait = require("sdk/deprecated/light-traits").Trait;
  6. exports["test custom constructor and inherited toString"] = function(assert) {
  7. function Type() {
  8. return Object.create(Type.prototype);
  9. }
  10. Type.prototype = Trait({
  11. method: function method() {
  12. return 2;
  13. }
  14. }).create(Object.freeze(Type.prototype));
  15. var fixture = Type();
  16. assert.equal(fixture.constructor, Type, "must override constructor");
  17. assert.equal(fixture.toString(), "[object Type]", "must inherit toString");
  18. };
  19. exports["test custom toString and inherited constructor"] = function(assert) {
  20. function Type() {
  21. return Object.create(Type.prototype);
  22. }
  23. Type.prototype = Trait({
  24. toString: function toString() {
  25. return "<toString>";
  26. }
  27. }).create();
  28. var fixture = Type();
  29. assert.equal(fixture.constructor, Trait, "must inherit constructor Trait");
  30. assert.equal(fixture.toString(), "<toString>", "Must override toString");
  31. };
  32. exports["test custom toString and constructor"] = function(assert) {
  33. function Type() {
  34. return TypeTrait.create(Type.prototype);
  35. }
  36. Object.freeze(Type.prototype);
  37. var TypeTrait = Trait({
  38. toString: function toString() {
  39. return "<toString>";
  40. }
  41. });
  42. var fixture = Type();
  43. assert.equal(fixture.constructor, Type, "constructor is provided to create");
  44. assert.equal(fixture.toString(), "<toString>", "toString was overridden");
  45. };
  46. exports["test resolve constructor"] = function (assert) {
  47. function Type() {}
  48. var T1 = Trait({ constructor: Type }).resolve({ constructor: '_foo' });
  49. var f1 = T1.create();
  50. assert.equal(f1._foo, Type, "constructor was resolved");
  51. assert.equal(f1.constructor, Trait, "constructor of prototype is inherited");
  52. assert.equal(f1.toString(), "[object Trait]", "toString is inherited");
  53. };
  54. exports["test compose read-only"] = function (assert) {
  55. function Type() {}
  56. Type.prototype = Trait.compose(Trait({}), {
  57. constructor: { value: Type },
  58. a: { value: "b", enumerable: true }
  59. }).resolve({ a: "b" }).create({ a: "a" });
  60. var f1 = new Type();
  61. assert.equal(Object.getPrototypeOf(f1), Type.prototype, "inherits correctly");
  62. assert.equal(f1.constructor, Type, "constructor was overridden");
  63. assert.equal(f1.toString(), "[object Type]", "toString was inherited");
  64. assert.equal(f1.a, "a", "property a was resolved");
  65. assert.equal(f1.b, "b", "property a was renamed to b");
  66. assert.ok(!Object.getOwnPropertyDescriptor(Type.prototype, "a"),
  67. "a is not on the prototype of the instance");
  68. var proto = Object.getPrototypeOf(Type.prototype);
  69. var dc = Object.getOwnPropertyDescriptor(Type.prototype, "constructor");
  70. var db = Object.getOwnPropertyDescriptor(Type.prototype, "b");
  71. var da = Object.getOwnPropertyDescriptor(proto, "a");
  72. assert.ok(!dc.writable, "constructor is not writable");
  73. assert.ok(!dc.enumerable, "constructor is not enumerable");
  74. assert.ok(dc.configurable, "constructor inherits configurability");
  75. assert.ok(!db.writable, "a -> b is not writable");
  76. assert.ok(db.enumerable, "a -> b is enumerable");
  77. assert.ok(!db.configurable, "a -> b is not configurable");
  78. assert.ok(da.writable, "a is writable");
  79. assert.ok(da.enumerable, "a is enumerable");
  80. assert.ok(da.configurable, "a is configurable");
  81. };
  82. if (require.main == module)
  83. require("test").run(exports);