You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2 KiB

4 years ago
  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. /* eslint-disable no-extend-native */
  44. delete Object.prototype[key];
  45. /* eslint-enable no-extend-native */
  46. st.end();
  47. });
  48. t.test('gets all expected non-Symbol descriptors', function (st) {
  49. var obj = { normal: Infinity };
  50. Object.defineProperty(obj, 'enumerable', enumDescriptor);
  51. Object.defineProperty(obj, 'writable', writableDescriptor);
  52. var descriptors = getDescriptors(obj);
  53. st.deepEqual(descriptors, {
  54. enumerable: enumDescriptor,
  55. normal: {
  56. configurable: true,
  57. enumerable: true,
  58. value: Infinity,
  59. writable: true
  60. },
  61. writable: writableDescriptor
  62. });
  63. st.end();
  64. });
  65. var supportsSymbols = typeof Symbol === 'function' && typeof Symbol() === 'symbol';
  66. t.test('gets Symbol descriptors too', { skip: !supportsSymbols }, function (st) {
  67. var symbol = Symbol();
  68. var symDescriptor = {
  69. configurable: false,
  70. enumerable: true,
  71. value: [symbol],
  72. writable: true
  73. };
  74. var obj = { normal: Infinity };
  75. Object.defineProperty(obj, 'enumerable', enumDescriptor);
  76. Object.defineProperty(obj, 'writable', writableDescriptor);
  77. Object.defineProperty(obj, 'symbol', symDescriptor);
  78. var descriptors = getDescriptors(obj);
  79. st.deepEqual(descriptors, {
  80. enumerable: enumDescriptor,
  81. normal: {
  82. configurable: true,
  83. enumerable: true,
  84. value: Infinity,
  85. writable: true
  86. },
  87. symbol: symDescriptor,
  88. writable: writableDescriptor
  89. });
  90. st.end();
  91. });
  92. /* global Proxy */
  93. var supportsProxy = typeof Proxy === 'function';
  94. t.test('Proxies that return an undefined descriptor', { skip: !supportsProxy }, function (st) {
  95. var obj = { foo: true };
  96. var fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
  97. var proxy = new Proxy(obj, {
  98. getOwnPropertyDescriptor: function (target, key) {
  99. return Object.getOwnPropertyDescriptor(target, key);
  100. },
  101. ownKeys: function () {
  102. return [
  103. 'foo',
  104. 'bar'
  105. ];
  106. }
  107. });
  108. st.deepEqual(getDescriptors(proxy), { foo: fooDescriptor }, 'object has no descriptors');
  109. st.end();
  110. });
  111. };