test-namespace.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. const { ns } = require("sdk/core/namespace");
  6. const { Cc, Ci, Cu } = require("chrome");
  7. const { setTimeout } = require("sdk/timers")
  8. exports["test post GC references"] = function (assert, done) {
  9. var target = {}, local = ns()
  10. local(target).there = true
  11. assert.equal(local(target).there, true, "namespaced preserved");
  12. Cu.schedulePreciseGC(function() {
  13. assert.equal(local(target).there, true, "namespace is preserved post GC");
  14. done();
  15. });
  16. };
  17. exports["test namsepace basics"] = function(assert) {
  18. var privates = ns();
  19. var object = { foo: function foo() { return "hello foo"; } };
  20. assert.notEqual(privates(object), object,
  21. "namespaced object is not the same");
  22. assert.ok(!('foo' in privates(object)),
  23. "public properties are not in the namespace");
  24. assert.equal(privates(object), privates(object),
  25. "same namespaced object is returned on each call");
  26. };
  27. exports["test namespace overlays"] = function(assert) {
  28. var _ = ns();
  29. var object = { foo: 'foo' };
  30. _(object).foo = 'bar';
  31. assert.equal(_(object).foo, "bar",
  32. "namespaced property `foo` changed value");
  33. assert.equal(object.foo, "foo",
  34. "public property `foo` has original value");
  35. object.foo = "baz";
  36. assert.equal(_(object).foo, "bar",
  37. "property changes do not affect namespaced properties");
  38. object.bar = "foo";
  39. assert.ok(!("bar" in _(object)),
  40. "new public properties are not reflected in namespace");
  41. };
  42. exports["test shared namespaces"] = function(assert) {
  43. var _ = ns();
  44. var f1 = { hello: 1 };
  45. var f2 = { foo: 'foo', hello: 2 };
  46. _(f1).foo = _(f2).foo = 'bar';
  47. assert.equal(_(f1).hello, _(f2).hello, "namespace can be shared");
  48. assert.notEqual(f1.hello, _(f1).hello, "shared namespace can overlay");
  49. assert.notEqual(f2.hello, _(f2).hello, "target is not affected");
  50. _(f1).hello = 3;
  51. assert.notEqual(_(f1).hello, _(f2).hello,
  52. "namespaced property can be overided");
  53. assert.equal(_(f2).hello, _({}).hello, "namespace does not change");
  54. };
  55. exports["test multi namespace"] = function(assert) {
  56. var n1 = ns();
  57. var n2 = ns();
  58. var object = { baz: 1 };
  59. n1(object).foo = 1;
  60. n2(object).foo = 2;
  61. n1(object).bar = n2(object).bar = 3;
  62. assert.notEqual(n1(object).foo, n2(object).foo,
  63. "object can have multiple namespaces");
  64. assert.equal(n1(object).bar, n2(object).bar,
  65. "object can have matching props in diff namespaces");
  66. };
  67. exports["test ns alias"] = function(assert) {
  68. assert.strictEqual(ns, require('sdk/core/namespace').Namespace,
  69. "ns is an alias of Namespace");
  70. };
  71. exports["test ns inheritance"] = function(assert) {
  72. let _ = ns();
  73. let prototype = { level: 1 };
  74. let object = Object.create(prototype);
  75. let delegee = Object.create(object);
  76. _(prototype).foo = {};
  77. assert.ok(!Object.prototype.hasOwnProperty.call(_(delegee), "foo"),
  78. "namespaced property is not copied to descendants");
  79. assert.equal(_(delegee).foo, _(prototype).foo,
  80. "namespaced properties are inherited by descendants");
  81. _(object).foo = {};
  82. assert.notEqual(_(object).foo, _(prototype).foo,
  83. "namespaced properties may be shadowed");
  84. assert.equal(_(object).foo, _(delegee).foo,
  85. "shadwed properties are inherited by descendants");
  86. _(object).bar = {};
  87. assert.ok(!("bar" in _(prototype)),
  88. "descendants properties are not copied to ancestors");
  89. assert.ok(_(object).bar, _(delegee).bar,
  90. "descendants properties are inherited");
  91. };
  92. require("test").run(exports);