tests.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. var keys = require('object-keys');
  3. var map = require('array.prototype.map');
  4. var define = require('define-properties');
  5. var hasSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
  6. module.exports = function (entries, t) {
  7. var a = {};
  8. var b = {};
  9. var c = {};
  10. var obj = { a: a, b: b, c: c };
  11. t.deepEqual(entries(obj), [['a', a], ['b', b], ['c', c]], 'basic support');
  12. t.deepEqual(entries({ a: a, b: a, c: c }), [['a', a], ['b', a], ['c', c]], 'duplicate entries are included');
  13. t.test('entries are in the same order as keys', function (st) {
  14. var object = { a: a, b: b };
  15. object[0] = 3;
  16. object.c = c;
  17. object[1] = 4;
  18. delete object[0];
  19. var objKeys = keys(object);
  20. var objEntries = map(objKeys, function (key) {
  21. return [key, object[key]];
  22. });
  23. st.deepEqual(entries(object), objEntries, 'entries match key order');
  24. st.end();
  25. });
  26. t.test('non-enumerable properties are omitted', { skip: !Object.defineProperty }, function (st) {
  27. var object = { a: a, b: b };
  28. Object.defineProperty(object, 'c', { enumerable: false, value: c });
  29. st.deepEqual(entries(object), [['a', a], ['b', b]], 'non-enumerable property‘s value is omitted');
  30. st.end();
  31. });
  32. t.test('inherited properties are omitted', function (st) {
  33. var F = function G() {};
  34. F.prototype.a = a;
  35. var f = new F();
  36. f.b = b;
  37. st.deepEqual(entries(f), [['b', b]], 'only own properties are included');
  38. st.end();
  39. });
  40. t.test('Symbol properties are omitted', { skip: !hasSymbols }, function (st) {
  41. var object = { a: a, b: b, c: c };
  42. var enumSym = Symbol('enum');
  43. var nonEnumSym = Symbol('non enum');
  44. object[enumSym] = enumSym;
  45. object.d = enumSym;
  46. Object.defineProperty(object, nonEnumSym, { enumerable: false, value: nonEnumSym });
  47. st.deepEqual(entries(object), [['a', a], ['b', b], ['c', c], ['d', enumSym]], 'symbol properties are omitted');
  48. st.end();
  49. });
  50. t.test('not-yet-visited keys deleted on [[Get]] must not show up in output', { skip: !define.supportsDescriptors }, function (st) {
  51. var o = { a: 1, b: 2, c: 3 };
  52. Object.defineProperty(o, 'a', {
  53. get: function () {
  54. delete this.b;
  55. return 1;
  56. }
  57. });
  58. st.deepEqual(entries(o), [['a', 1], ['c', 3]], 'when "b" is deleted prior to being visited, it should not show up');
  59. st.end();
  60. });
  61. t.test('not-yet-visited keys made non-enumerable on [[Get]] must not show up in output', { skip: !define.supportsDescriptors }, function (st) {
  62. var o = { a: 'A', b: 'B' };
  63. Object.defineProperty(o, 'a', {
  64. get: function () {
  65. Object.defineProperty(o, 'b', { enumerable: false });
  66. return 'A';
  67. }
  68. });
  69. st.deepEqual(entries(o), [['a', 'A']], 'when "b" is made non-enumerable prior to being visited, it should not show up');
  70. st.end();
  71. });
  72. };