Skip to content

Commit b1746de

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

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-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') {
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 = Object.create(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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import defineProperty from '@core-js/pure/es/object/define-property';
12
import keysLength from '@core-js/pure/full/object/keys-length';
3+
import Symbol from '@core-js/pure/es/symbol';
24

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

0 commit comments

Comments
 (0)