lazy.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. "use strict";
  2. var d = require("../")
  3. , getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  4. module.exports = function (t, a) {
  5. var Foo = function () {}, i = 1, o, o2, desc;
  6. Object.defineProperties(
  7. Foo.prototype,
  8. t({
  9. bar: d(function () { return ++i; }),
  10. bar2: d(function () { return this.bar + 23; }),
  11. bar3: d(function () { return this.bar2 + 34; }, { desc: "ew" }),
  12. bar4: d(function () { return this.bar3 + 12; }, { cacheName: "_bar4_" }),
  13. bar5: d(function () { return this.bar4 + 3; }, { cacheName: "_bar5_", desc: "e" })
  14. })
  15. );
  16. desc = getOwnPropertyDescriptor(Foo.prototype, "bar");
  17. a(desc.configurable, true, "Configurable: default");
  18. a(desc.enumerable, false, "Enumerable: default");
  19. o = new Foo();
  20. a.deep([o.bar, o.bar2, o.bar3, o.bar4, o.bar5], [2, 25, 59, 71, 74], "Values");
  21. a.deep(
  22. getOwnPropertyDescriptor(o, "bar3"),
  23. { configurable: false, enumerable: true, writable: true, value: 59 }, "Desc"
  24. );
  25. a(o.hasOwnProperty("bar4"), false, "Cache not exposed");
  26. desc = getOwnPropertyDescriptor(o, "bar5");
  27. a.deep(
  28. desc, { configurable: false, enumerable: true, get: desc.get, set: desc.set },
  29. "Cache & Desc: desc"
  30. );
  31. o2 = Object.create(o);
  32. o2.bar = 30;
  33. o2.bar3 = 100;
  34. a.deep(
  35. [o2.bar, o2.bar2, o2.bar3, o2.bar4, o2.bar5], [30, 25, 100, 112, 115], "Extension Values"
  36. );
  37. Foo = function () {};
  38. Object.defineProperties(
  39. Foo.prototype,
  40. t({
  41. test: d("w", function () { return "raz"; }),
  42. test2: d("", function () { return "raz"; }, { desc: "w" }),
  43. test3: d("", function () { return "raz"; }, { cacheName: "__test3__", desc: "w" }),
  44. test4: d("w", "bar")
  45. })
  46. );
  47. o = new Foo();
  48. o.test = "marko";
  49. a.deep(
  50. getOwnPropertyDescriptor(o, "test"),
  51. { configurable: false, enumerable: false, writable: true, value: "marko" }, "Set before get"
  52. );
  53. o.test2 = "marko2";
  54. a.deep(
  55. getOwnPropertyDescriptor(o, "test2"),
  56. { configurable: false, enumerable: false, writable: true, value: "marko2" },
  57. "Set before get: Custom desc"
  58. );
  59. o.test3 = "marko3";
  60. a.deep(
  61. getOwnPropertyDescriptor(o, "__test3__"),
  62. { configurable: false, enumerable: false, writable: true, value: "marko3" },
  63. "Set before get: Custom cache name"
  64. );
  65. a(o.test4, "bar", "Resolve by value");
  66. a.h1("Flat");
  67. Object.defineProperties(
  68. Foo.prototype,
  69. t({
  70. flat: d(function () { return "foo"; }, { flat: true }),
  71. flat2: d(function () { return "bar"; }, { flat: true })
  72. })
  73. );
  74. a.h2("Instance");
  75. a(o.flat, "foo", "Value");
  76. a(o.hasOwnProperty("flat"), false, "Instance");
  77. a(Foo.prototype.flat, "foo", "Prototype");
  78. a.h2("Direct");
  79. a(Foo.prototype.flat2, "bar");
  80. a.h2("Reset direct");
  81. Object.defineProperties(Foo.prototype, t({ testResetDirect: d(false) }));
  82. a.throws(function () { Foo.prototype.testResetDirect = false; }, TypeError);
  83. };