lazy.js 2.7 KB

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