addon-sdk/source/test/test-cortex.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-cortex.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +var Cortex = require("sdk/deprecated/cortex").Cortex;
    1.11 +
    1.12 +exports["test property changes propagate"] = function (assert) {
    1.13 +  var source = {
    1.14 +    _foo: "secret",
    1.15 +    get foo() {
    1.16 +      return this._foo;
    1.17 +    },
    1.18 +    set foo(value) {
    1.19 +      this._foo = value;
    1.20 +    },
    1.21 +    get getOnly() {
    1.22 +      return this._foo;
    1.23 +    },
    1.24 +    set setOnly(value) {
    1.25 +      this._setOnly = value;
    1.26 +    },
    1.27 +    bar: "public",
    1.28 +    method: function method(a, b) {
    1.29 +      return this._foo + a + b
    1.30 +    }
    1.31 +  };
    1.32 +  var fixture = Cortex(source);
    1.33 +
    1.34 +  assert.ok(!('_foo' in fixture),
    1.35 +            "properties that start with `_` are omitted");
    1.36 +  assert.equal(fixture.foo, "secret", "get accessor alias works");
    1.37 +  fixture.foo = "new secret";
    1.38 +  assert.equal(fixture.foo, "new secret", "set accessor alias works");
    1.39 +  assert.equal(source.foo, "new secret", "accessor delegates to the source");
    1.40 +  assert.equal(fixture.bar, "public", "data property alias works");
    1.41 +  fixture.bar = "bar";
    1.42 +  assert.equal(source.bar, "bar", "data property change propagates");
    1.43 +  source.bar = "foo"
    1.44 +  assert.equal(fixture.bar, "foo", "data property change probagets back");
    1.45 +  assert.equal(fixture.method("a", "b"), "new secretab",
    1.46 +               "public methods are callable");
    1.47 +  assert.equal(fixture.method.call({ _foo: "test" }, " a,", "b"),
    1.48 +               "new secret a,b",
    1.49 +               "`this` pseudo-variable can not be passed through call.");
    1.50 +  assert.equal(fixture.method.apply({ _foo: "test" }, [" a,", "b"]),
    1.51 +               "new secret a,b",
    1.52 +               "`this` pseudo-variable can not be passed through apply.");
    1.53 +  assert.equal(fixture.getOnly, source._foo,
    1.54 +               "getter returned property of wrapped object");
    1.55 +  fixture.setOnly = 'bar'
    1.56 +  assert.equal(source._setOnly, 'bar', "setter modified wrapped object")
    1.57 +};
    1.58 +
    1.59 +
    1.60 +exports["test immunity of inheritance"] = function(assert) {
    1.61 +  function Type() {}
    1.62 +  Type.prototype = {
    1.63 +    constructor: Type,
    1.64 +    _bar: 2,
    1.65 +    bar: 3,
    1.66 +    get_Foo: function getFoo() {
    1.67 +      return this._foo;
    1.68 +    }
    1.69 +  }
    1.70 +  var source = Object.create(Type.prototype, {
    1.71 +    _foo: { value: 'secret' },
    1.72 +    getBar: { value: function get_Bar() {
    1.73 +      return this.bar
    1.74 +    }},
    1.75 +    get_Bar: { value: function getBar() {
    1.76 +      return this._bar
    1.77 +    }}
    1.78 +  });
    1.79 +
    1.80 +  var fixture = Cortex(source);
    1.81 +
    1.82 +  assert.ok(Cortex({}, null, Type.prototype) instanceof Type,
    1.83 +            "if custom prototype is providede cortex will inherit from it");
    1.84 +  assert.ok(fixture instanceof Type,
    1.85 +            "if no prototype is given cortex inherits from object's prototype");
    1.86 +
    1.87 +  source.bar += 1;
    1.88 +  assert.notEqual(fixture.bar, source.bar,
    1.89 +                  "chages of properties don't propagate to non-aliases");
    1.90 +  assert.equal(fixture.getBar(), source.bar,
    1.91 +               "methods accessing public properties are bound to the source");
    1.92 +
    1.93 +  fixture._bar += 1;
    1.94 +  assert.notEqual(fixture._bar, source._bar,
    1.95 +                  "changes of non aliased properties don't propagate");
    1.96 +  assert.equal(fixture.get_Bar(), source._bar,
    1.97 +               "methods accessing privates are bound to the source");
    1.98 +  assert.notEqual(fixture.get_Foo(), source._foo,
    1.99 +                  "prototoype methods are not bound to the source");
   1.100 +}
   1.101 +
   1.102 +exports["test customized public properties"] = function(assert) {
   1.103 +  var source = {
   1.104 +    _a: 'a',
   1.105 +    b: 'b',
   1.106 +    get: function get(name) {
   1.107 +      return this[name];
   1.108 +    }
   1.109 +  };
   1.110 +  
   1.111 +  var fixture = Cortex(source, ['_a', 'get']);
   1.112 +  fixture._a += "#change";
   1.113 +
   1.114 +
   1.115 +  assert.ok(!("b" in fixture), "non-public own property is not defined");
   1.116 +  assert.equal(fixture.get("b"), source.b,
   1.117 +              "public methods preserve access to the private properties");
   1.118 +  assert.equal(fixture._a, source._a,
   1.119 +              "custom public property changes propagate");
   1.120 +}
   1.121 +
   1.122 +//if (require.main == module)
   1.123 +  require("test").run(exports);

mercurial