Skip to content

Commit 74568e1

Browse files
committed
Add Object.keysLength test
1 parent 279a1b5 commit 74568e1

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

tests/unit-global/esnext.object.keys-length.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,53 @@ QUnit.test('Object.keysLength', assert => {
88

99
assert.same(keysLength({ a: 1, b: 2 }), 2, 'Basic functionality');
1010
assert.same(keysLength({}), 0, 'Empty object');
11+
12+
if (typeof Symbol == 'function' && !Symbol.sham) {
13+
assert.same(keysLength({ a: 1, [Symbol('s')]: 2 }), 1, 'Ignores symbol keys');
14+
}
15+
16+
const nonEnum = { a: 1 };
17+
Object.defineProperty(nonEnum, 'hidden', {
18+
value: true,
19+
enumerable: false,
20+
});
21+
assert.same(keysLength(nonEnum), 1, 'Ignores non-enumerable own properties');
22+
23+
const withAccessor = {};
24+
Object.defineProperty(withAccessor, 'foo', {
25+
get() { return 1; },
26+
enumerable: true,
27+
});
28+
assert.same(keysLength(withAccessor), 1, 'Counts enumerable accessors');
29+
30+
const withNonEnumAccessor = {};
31+
Object.defineProperty(withNonEnumAccessor, 'bar', {
32+
get() { return 1; },
33+
enumerable: false,
34+
});
35+
assert.same(keysLength(withNonEnumAccessor), 0, 'Ignores non-enumerable accessors');
36+
37+
const proto = { a: 1, b: 2 };
38+
const obj = Object.create(proto);
39+
obj.c = 3;
40+
Object.defineProperty(proto, 'd', {
41+
value: 4,
42+
enumerable: true,
43+
});
44+
assert.same(keysLength(obj), 1, 'Does not count inherited properties');
45+
46+
const dict = { __proto__: null };
47+
dict.x = 1;
48+
assert.same(keysLength(dict), 1, 'Works with null-prototype objects');
49+
50+
assert.same(keysLength('abc'), 3, 'String primitive indices are own enumerable keys');
51+
assert.same(keysLength(42), 0, 'Number primitive has no enumerable string keys');
52+
assert.same(keysLength(true), 0, 'Boolean primitive has no enumerable string keys');
53+
54+
const arr = ['a'];
55+
arr[2] = 'b';
56+
assert.same(keysLength(arr), 2, 'Array counts only present indices');
57+
1158
assert.throws(() => keysLength(null), TypeError);
1259
assert.throws(() => keysLength(), TypeError);
1360
});

tests/unit-pure/esnext.object.keys-length.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import create from '@core-js/pure/es/object/create';
2+
import defineProperty from '@core-js/pure/es/object/define-property';
13
import keysLength from '@core-js/pure/full/object/keys-length';
4+
import Symbol from '@core-js/pure/es/symbol';
25

36
QUnit.test('Object.keysLength', assert => {
47
assert.isFunction(keysLength);
@@ -9,4 +12,48 @@ QUnit.test('Object.keysLength', assert => {
912
assert.same(keysLength({}), 0, 'Empty object');
1013
assert.throws(() => keysLength(null), TypeError);
1114
assert.throws(() => keysLength(), TypeError);
15+
16+
assert.same(keysLength({ a: 1, [Symbol('s')]: 2 }), 1, 'Ignores symbol keys');
17+
18+
const nonEnum = { a: 1 };
19+
defineProperty(nonEnum, 'hidden', {
20+
value: true,
21+
enumerable: false,
22+
});
23+
assert.same(keysLength(nonEnum), 1, 'Ignores non-enumerable own properties');
24+
25+
const withAccessor = {};
26+
defineProperty(withAccessor, 'foo', {
27+
get() { return 1; },
28+
enumerable: true,
29+
});
30+
assert.same(keysLength(withAccessor), 1, 'Counts enumerable accessors');
31+
32+
const withNonEnumAccessor = {};
33+
defineProperty(withNonEnumAccessor, 'bar', {
34+
get() { return 1; },
35+
enumerable: false,
36+
});
37+
assert.same(keysLength(withNonEnumAccessor), 0, 'Ignores non-enumerable accessors');
38+
39+
const proto = { a: 1, b: 2 };
40+
const obj = create(proto);
41+
obj.c = 3;
42+
defineProperty(proto, 'd', {
43+
value: 4,
44+
enumerable: true,
45+
});
46+
assert.same(keysLength(obj), 1, 'Does not count inherited properties');
47+
48+
const dict = { __proto__: null };
49+
dict.x = 1;
50+
assert.same(keysLength(dict), 1, 'Works with null-prototype objects');
51+
52+
assert.same(keysLength('abc'), 3, 'String primitive indices are own enumerable keys');
53+
assert.same(keysLength(42), 0, 'Number primitive has no enumerable string keys');
54+
assert.same(keysLength(true), 0, 'Boolean primitive has no enumerable string keys');
55+
56+
const arr = ['a'];
57+
arr[2] = 'b';
58+
assert.same(keysLength(arr), 2, 'Array counts only present indices');
1259
});

0 commit comments

Comments
 (0)