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.

84 lines
2.6 KiB

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