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