tests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. module.exports = function (getDescriptors, t) {
  3. var enumDescriptor = {
  4. configurable: true,
  5. enumerable: false,
  6. value: true,
  7. writable: false
  8. };
  9. var writableDescriptor = {
  10. configurable: true,
  11. enumerable: true,
  12. value: 42,
  13. writable: true
  14. };
  15. t.test('works with Object.prototype poisoned setter', { skip: !Object.defineProperty }, function (st) {
  16. var key = 'foo';
  17. var obj = {};
  18. obj[key] = 42;
  19. var expected = {};
  20. expected[key] = {
  21. configurable: true,
  22. enumerable: true,
  23. value: 42,
  24. writable: true
  25. };
  26. /* eslint-disable no-extend-native, accessor-pairs */
  27. Object.defineProperty(Object.prototype, key, { configurable: true, set: function (v) { throw new Error(v); } });
  28. /* eslint-enable no-extend-native, accessor-pairs */
  29. var hasOwnNamesBug = false;
  30. try {
  31. Object.getOwnPropertyNames(obj);
  32. } catch (e) {
  33. // v8 in node 0.6 - 0.12 has a bug :-(
  34. hasOwnNamesBug = true;
  35. st.comment('SKIP: this engine has a bug with Object.getOwnPropertyNames: it can not handle a throwing setter on Object.prototype.');
  36. }
  37. if (!hasOwnNamesBug) {
  38. st.doesNotThrow(function () {
  39. var result = getDescriptors(obj);
  40. st.deepEqual(result, expected, 'got expected descriptors');
  41. });
  42. }
  43. delete Object.prototype[key];
  44. st.end();
  45. });
  46. t.test('gets all expected non-Symbol descriptors', function (st) {
  47. var obj = { normal: Infinity };
  48. Object.defineProperty(obj, 'enumerable', enumDescriptor);
  49. Object.defineProperty(obj, 'writable', writableDescriptor);
  50. var descriptors = getDescriptors(obj);
  51. st.deepEqual(descriptors, {
  52. enumerable: enumDescriptor,
  53. normal: {
  54. configurable: true,
  55. enumerable: true,
  56. value: Infinity,
  57. writable: true
  58. },
  59. writable: writableDescriptor
  60. });
  61. st.end();
  62. });
  63. var supportsSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
  64. t.test('gets Symbol descriptors too', { skip: !supportsSymbols }, function (st) {
  65. var symbol = Symbol('sym');
  66. var symDescriptor = {
  67. configurable: false,
  68. enumerable: true,
  69. value: [symbol],
  70. writable: true
  71. };
  72. var obj = { normal: Infinity };
  73. Object.defineProperty(obj, 'enumerable', enumDescriptor);
  74. Object.defineProperty(obj, 'writable', writableDescriptor);
  75. Object.defineProperty(obj, 'symbol', symDescriptor);
  76. var descriptors = getDescriptors(obj);
  77. st.deepEqual(descriptors, {
  78. enumerable: enumDescriptor,
  79. normal: {
  80. configurable: true,
  81. enumerable: true,
  82. value: Infinity,
  83. writable: true
  84. },
  85. symbol: symDescriptor,
  86. writable: writableDescriptor
  87. });
  88. st.end();
  89. });
  90. /* global Proxy */
  91. var supportsProxy = typeof Proxy === 'function';
  92. t.test('Proxies that return an undefined descriptor', { skip: !supportsProxy }, function (st) {
  93. var obj = { foo: true };
  94. var fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
  95. var proxy = new Proxy(obj, {
  96. getOwnPropertyDescriptor: function (target, key) {
  97. return Object.getOwnPropertyDescriptor(target, key);
  98. },
  99. ownKeys: function () {
  100. return ['foo', 'bar'];
  101. }
  102. });
  103. st.deepEqual(getDescriptors(proxy), { foo: fooDescriptor }, 'object has no descriptors');
  104. st.end();
  105. });
  106. };