Add test for for-in order (#2432)

* Add EnumerateObjectProperties tests for builtins which use it

* Add tests for EnumerateObjectProperties for for-in

* Add feature flag for for-in-order
This commit is contained in:
Kevin Gibbons 2019-12-18 08:25:20 -08:00 committed by Leo Balter
parent f7ad2953ed
commit cadd47aa5f
10 changed files with 294 additions and 0 deletions

View File

@ -162,6 +162,10 @@ AggregateError
# https://github.com/tc39/proposal-string-replaceall
String.prototype.replaceAll
# Enumeration order for for-in
# https://github.com/tc39/proposal-for-in-order
for-in-order
## Standard language features
#
# Language features that have been included in a published version of the

View File

@ -0,0 +1,20 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-internalizejsonproperty
description: JSON.parse reviver call order
features: [for-in-order]
includes: [compareArray.js]
---*/
var calls = [];
function reviver(name, val) {
calls.push(name);
return val;
}
JSON.parse('{"p1":0,"p2":0,"p1":0,"2":0,"1":0}', reviver);
// The empty string is the _rootName_ in JSON.parse
assert.compareArray(calls, ['1', '2', 'p1', 'p2', '']);

View File

@ -0,0 +1,39 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-serializejsonobject
description: JSON.stringify property enumeration order
features: [for-in-order]
---*/
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
// This getter will be triggered during enumeration, but the property it adds should not be enumerated.
Object.defineProperty(o, 'add', {
enumerable: true,
get: function () {
o.extra = 'extra';
return 'add';
}
});
o.p4 = 'p4';
o[2] = '2';
o[0] = '0';
o[1] = '1';
delete o.p1;
delete o.p3;
o.p1 = 'p1';
var actual = JSON.stringify(o);
var expected = '{"0":"0","1":"1","2":"2","p2":"p2","add":"add","p4":"p4","p1":"p1"}';
assert.sameValue(actual, expected);

View File

@ -0,0 +1,41 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-object.entries
description: Object.entries enumeration order
features: [for-in-order]
includes: [compareArray.js]
---*/
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
// This getter will be triggered during enumeration, but the property it adds should not be enumerated.
Object.defineProperty(o, 'add', {
enumerable: true,
get: function () {
o.extra = 'extra';
return 'add';
}
});
o.p4 = 'p4';
o[2] = '2';
o[0] = '0';
o[1] = '1';
delete o.p1;
delete o.p3;
o.p1 = 'p1';
var actual = Object.entries(o).map(function(e) { return e[0]; });
var expected = ['0', '1', '2', 'p2', 'add', 'p4', 'p1'];
assert.compareArray(actual, expected);

View File

@ -0,0 +1,30 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-object.keys
description: Object.keys enumeration order
features: [for-in-order]
includes: [compareArray.js]
---*/
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
o.p4 = 'p4';
o[2] = '2';
o[0] = '0';
o[1] = '1';
delete o.p1;
delete o.p3;
o.p1 = 'p1';
var actual = Object.keys(o);
var expected = ['0', '1', '2', 'p2', 'p4', 'p1'];
assert.compareArray(actual, expected);

View File

@ -0,0 +1,41 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-object.values
description: Object.values enumeration order
features: [for-in-order]
includes: [compareArray.js]
---*/
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
// This getter will be triggered during enumeration, but the property it adds should not be enumerated.
Object.defineProperty(o, 'add', {
enumerable: true,
get: function () {
o.extra = 'extra';
return 'add';
}
});
o.p4 = 'p4';
o[2] = '2';
o[0] = '0';
o[1] = '1';
delete o.p1;
delete o.p3;
o.p1 = 'p1';
var actual = Object.values(o);
var expected = ['0', '1', '2', 'p2', 'add', 'p4', 'p1'];
assert.compareArray(actual, expected);

View File

@ -0,0 +1,33 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-enumerate-object-properties
description: Enumerable properties the prototype which are shadowed by non-enumerable properties on the object are not enumerated
features: [for-in-order]
includes: [compareArray.js]
---*/
var proto = {
p2: 'p2',
};
var o = Object.create(proto, {
'p1': {
value: 'p1',
enumerable: true,
},
'p2': {
value: 'p1',
enumerable: false,
},
});
var keys = [];
for (var key in o) {
keys.push(key);
}
assert.compareArray(keys, ['p1']);

View File

@ -0,0 +1,25 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-enumerate-object-properties
description: Properties added to the object during iteration are not enumerated
features: [for-in-order]
includes: [compareArray.js]
---*/
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
var keys = [];
for (var key in o) {
if (key === 'p1') {
o.p4 = 'p4';
}
keys.push(key);
}
assert.compareArray(keys, ['p1', 'p2', 'p3']);

View File

@ -0,0 +1,28 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-enumerate-object-properties
description: Properties on the prototype are enumerated after properties on the object
features: [for-in-order]
includes: [compareArray.js]
---*/
var proto = {
p4: 'p4',
};
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
Object.setPrototypeOf(o, proto);
var keys = [];
for (var key in o) {
keys.push(key);
}
assert.compareArray(keys, ['p1', 'p2', 'p3', 'p4']);

View File

@ -0,0 +1,33 @@
// Copyright 2019 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-enumerate-object-properties
description: Property enumeration order for simple objects
features: [for-in-order]
includes: [compareArray.js]
---*/
var o = {
p1: 'p1',
p2: 'p2',
p3: 'p3',
};
o.p4 = 'p4';
o[2] = '2';
o[0] = '0';
o[1] = '1';
delete o.p1;
delete o.p3;
o.p1 = 'p1';
var keys = [];
for (var key in o) {
keys.push(key);
}
assert.compareArray(keys, ['0', '1', '2', 'p2', 'p4', 'p1']);