test-cortex.js 3.9 KB

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