Merge pull request #484 from ljharb/object_get_own_property_descriptors

Add Object.getOwnPropertyDescriptors tests
This commit is contained in:
Gorkem Yakin 2016-02-19 12:08:37 -08:00
commit 7d345fc95b
20 changed files with 489 additions and 26 deletions

View File

@ -0,0 +1,22 @@
function allowProxyTraps(overrides) {
function throwTest262Error(msg) {
return function () { throw new Test262Error(msg); };
}
if (!overrides) { overrides = {}; }
return {
getPrototypeOf: overrides.getPrototypeOf || throwTest262Error('[[GetPrototypeOf]] trap called'),
setPrototypeOf: overrides.setPrototypeOf || throwTest262Error('[[SetPrototypeOf]] trap called'),
isExtensible: overrides.isExtensible || throwTest262Error('[[IsExtensible]] trap called'),
preventExtensions: overrides.preventExtensions || throwTest262Error('[[PreventExtensions]] trap called'),
getOwnPropertyDescriptor: overrides.getOwnPropertyDescriptor || throwTest262Error('[[GetOwnProperty]] trap called'),
has: overrides.has || throwTest262Error('[[HasProperty]] trap called'),
get: overrides.get || throwTest262Error('[[Get]] trap called'),
set: overrides.set || throwTest262Error('[[Set]] trap called'),
deleteProperty: overrides.deleteProperty || throwTest262Error('[[Delete]] trap called'),
defineProperty: overrides.defineProperty || throwTest262Error('[[DefineOwnProperty]] trap called'),
enumerate: throwTest262Error('[[Enumerate]] trap called: this trap has been removed'),
ownKeys: overrides.ownKeys || throwTest262Error('[[OwnPropertyKeys]] trap called'),
apply: overrides.apply || throwTest262Error('[[Call]] trap called'),
construct: overrides.construct || throwTest262Error('[[Construct]] trap called')
};
}

View File

@ -6,11 +6,12 @@ id: sec-object.entries
description: Object.entries should perform observable operations in the correct order
author: Jordan Harband
features: [Proxy]
includes: [proxyTrapsHelper.js]
---*/
var log = "";
var object = { a: 0, b: 0, c: 0 };
var handler = {
var handler = allowProxyTraps({
get: function (target, propertyKey, receiver) {
assert.sameValue(target, object, "get target");
assert.sameValue(receiver, proxy, "get receiver");
@ -26,23 +27,14 @@ var handler = {
assert.sameValue(target, object, "ownKeys");
log += "|ownKeys";
return Object.getOwnPropertyNames(target);
},
deleteProperty: function (oTarget, sKey) {
throw new Test262Error('properties should not be deleted');
},
defineProperty: function (oTarget, sKey, oDesc) {
throw new Test262Error('properties should not be defined');
},
set: function (oTarget, sKey, vValue) {
throw new Test262Error('properties should not be assigned');
}
};
var check = {
});
var check = allowProxyTraps({
get: function (target, propertyKey, receiver) {
assert(propertyKey in target, "handler check: " + propertyKey);
return target[propertyKey];
}
};
});
var proxy = new Proxy(object, new Proxy(handler, check));
var result = Object.entries(proxy);
assert.sameValue(log, "|ownKeys|getOwnPropertyDescriptor:a|get:a|getOwnPropertyDescriptor:b|get:b|getOwnPropertyDescriptor:c|get:c", log);

View File

@ -0,0 +1,31 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors on a proxy with duplicate ownKeys should work
id: pending
author: Jordan Harband
features: [Proxy]
---*/
var i = 0;
var descriptors = [
{ enumerable: false, value: "A1", writable: true, configurable: true },
{ enumerable: true, value: "A2", writable: true, configurable: true }
];
var log = '';
var proxy = new Proxy({}, {
ownKeys() {
log += 'ownKeys|';
return ['DUPLICATE', 'DUPLICATE', 'DUPLICATE'];
},
getOwnPropertyDescriptor(t, name) {
log += 'getOwnPropertyDescriptor:' + name + '|';
return descriptors[i++];
}
});
var result = Object.getOwnPropertyDescriptors(proxy);
assert.sameValue(result.hasOwnProperty('DUPLICATE'), true);
assert.sameValue(result.DUPLICATE, undefined);
assert.sameValue(log, 'ownKeys|getOwnPropertyDescriptor:DUPLICATE|getOwnPropertyDescriptor:DUPLICATE|getOwnPropertyDescriptor:DUPLICATE');

View File

@ -0,0 +1,16 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors should fail if given a null or undefined value
id: pending
author: Jordan Harband
---*/
assert.throws(TypeError, function () {
Object.getOwnPropertyDescriptors(null);
});
assert.throws(TypeError, function () {
Object.getOwnPropertyDescriptors(undefined);
});

View File

@ -0,0 +1,15 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors should have length 1
id: pending
author: Jordan Harband
includes: [propertyHelper.js]
---*/
assert.sameValue(Object.getOwnPropertyDescriptors.length, 1, 'Expected Object.getOwnPropertyDescriptors.length to be 1');
verifyNotEnumerable(Object.getOwnPropertyDescriptors, 'length');
verifyNotWritable(Object.getOwnPropertyDescriptors, 'length');
verifyConfigurable(Object.getOwnPropertyDescriptors, 'length');

View File

@ -0,0 +1,19 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors should have name property with value 'getOwnPropertyDescriptors'
id: pending
author: Jordan Harband
includes: [propertyHelper.js]
---*/
assert.sameValue(
Object.getOwnPropertyDescriptors.name,
'getOwnPropertyDescriptors',
'Expected Object.getOwnPropertyDescriptors.name to be "getOwnPropertyDescriptors"'
);
verifyNotEnumerable(Object.getOwnPropertyDescriptors, 'name');
verifyNotWritable(Object.getOwnPropertyDescriptors, 'name');
verifyConfigurable(Object.getOwnPropertyDescriptors, 'name');

View File

@ -0,0 +1,13 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors should be writable, non-enumerable, and configurable
id: pending
author: Jordan Harband
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(Object, 'getOwnPropertyDescriptors');
verifyWritable(Object, 'getOwnPropertyDescriptors');
verifyConfigurable(Object, 'getOwnPropertyDescriptors');

View File

@ -0,0 +1,43 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors does not see inherited properties.
id: pending
author: Jordan Harband
---*/
var F = function () {};
F.prototype.a = {};
F.prototype.b = {};
var f = new F();
var bValue = {};
f.b = bValue; // shadow the prototype
Object.defineProperty(f, 'c', {
enumerable: false,
configurable: true,
writable: false,
value: {}
}); // solely an own property
var result = Object.getOwnPropertyDescriptors(f);
assert.sameValue(!!result.b, true, 'b has a descriptor');
assert.sameValue(!!result.c, true, 'c has a descriptor');
assert.sameValue(result.b.enumerable, true, 'b is enumerable');
assert.sameValue(result.b.configurable, true, 'b is configurable');
assert.sameValue(result.b.writable, true, 'b is writable');
assert.sameValue(result.b.value, bValue, 'bs value is `bValue`');
assert.sameValue(result.c.enumerable, false, 'c is enumerable');
assert.sameValue(result.c.configurable, true, 'c is configurable');
assert.sameValue(result.c.writable, false, 'c is writable');
assert.sameValue(result.c.value, f.c, 'cs value is `f.c`');
assert.sameValue(
Object.keys(result).length,
2,
'result has same number of own property names as f'
);

View File

@ -0,0 +1,13 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors should produce a normal object inheriting from Object.prototype
id: pending
author: Jordan Harband
---*/
assert.sameValue(
Object.getPrototypeOf(Object.getOwnPropertyDescriptors({})),
Object.prototype
);

View File

@ -0,0 +1,34 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors should perform observable operations in the correct order
id: pending
author: Jordan Harband
features: [Proxy]
includes: [proxyTrapsHelper.js]
---*/
var log = "";
var object = { a: 0, b: 0, c: 0 };
var handler = allowProxyTraps({
getOwnPropertyDescriptor: function (target, propertyKey) {
assert.sameValue(target, object, "getOwnPropertyDescriptor");
log += "|getOwnPropertyDescriptor:" + propertyKey;
return Object.getOwnPropertyDescriptor(target, propertyKey);
},
ownKeys: function (target) {
assert.sameValue(target, object, "ownKeys");
log += "|ownKeys";
return Object.getOwnPropertyNames(target);
}
});
var check = allowProxyTraps({
get: function (target, propertyKey, receiver) {
assert(propertyKey in target, "handler check: " + propertyKey);
return target[propertyKey];
}
});
var proxy = new Proxy(object, new Proxy(handler, check));
var result = Object.getOwnPropertyDescriptors(proxy);
assert.sameValue(log, "|ownKeys|getOwnPropertyDescriptor:a|getOwnPropertyDescriptor:b|getOwnPropertyDescriptor:c", 'log');

View File

@ -0,0 +1,16 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors accepts boolean primitives.
id: pending
author: Jordan Harband
---*/
var trueResult = Object.getOwnPropertyDescriptors(true);
assert.sameValue(Object.keys(trueResult).length, 0, 'trueResult has 0 items');
var falseResult = Object.getOwnPropertyDescriptors(false);
assert.sameValue(Object.keys(falseResult).length, 0, 'falseResult has 0 items');

View File

@ -0,0 +1,15 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors accepts number primitives.
id: pending
author: Jordan Harband
---*/
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors(0)).length, 0, '0 has zero descriptors');
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors(-0)).length, 0, '-0 has zero descriptors');
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors(Infinity)).length, 0, 'Infinity has zero descriptors');
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors(-Infinity)).length, 0, '-Infinity has zero descriptors');
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors(NaN)).length, 0, 'NaN has zero descriptors');
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors(Math.PI)).length, 0, 'Math.PI has zero descriptors');

View File

@ -0,0 +1,33 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors accepts string primitives.
id: pending
author: Jordan Harband
---*/
var result = Object.getOwnPropertyDescriptors('abc');
assert.sameValue(Object.keys(result).length, 4, 'string has 4 descriptors');
assert.sameValue(result.length.configurable, false, 'length is not configurable');
assert.sameValue(result.length.enumerable, false, 'length is not enumerable');
assert.sameValue(result.length.writable, false, 'length is not writable');
assert.sameValue(result.length.value, 3, 'length is 3');
assert.sameValue(result[0].configurable, false, 'index 0 is not configurable');
assert.sameValue(result[0].enumerable, true, 'index 0 is enumerable');
assert.sameValue(result[0].writable, false, 'index 0 is not writable');
assert.sameValue(result[0].value, 'a', 'index 0 is "a"');
assert.sameValue(result[1].configurable, false, 'index 1 is not configurable');
assert.sameValue(result[1].enumerable, true, 'index 1 is enumerable');
assert.sameValue(result[1].writable, false, 'index 1 is not writable');
assert.sameValue(result[1].value, 'b', 'index 1 is "b"');
assert.sameValue(result[2].configurable, false, 'index 2 is not configurable');
assert.sameValue(result[2].enumerable, true, 'index 2 is enumerable');
assert.sameValue(result[2].writable, false, 'index 2 is not writable');
assert.sameValue(result[2].value, 'c', 'index 2 is "c"');

View File

@ -0,0 +1,13 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors accepts Symbol primitives.
id: pending
author: Jordan Harband
features: [Symbol]
---*/
var result = Object.getOwnPropertyDescriptors(Symbol());
assert.sameValue(Object.keys(result).length, 0, 'symbol primitive has no descriptors');

View File

@ -0,0 +1,37 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: Object.getOwnPropertyDescriptors includes Symbol keys.
id: pending
author: Jordan Harband
features: [Symbol]
---*/
var value = {};
var enumSym = Symbol('enum');
var nonEnumSym = Symbol('nonenum');
var symValue = Symbol('value');
var obj = { key: symValue };
obj[enumSym] = value;
Object.defineProperty(obj, nonEnumSym, { enumerable: false, value: value });
var result = Object.getOwnPropertyDescriptors(obj);
assert.sameValue(Object.keys(result).length, 3, 'obj has 3 descriptors');
assert.sameValue(result.key.configurable, true, 'result.key is configurable');
assert.sameValue(result.key.enumerable, true, 'result.key is enumerable');
assert.sameValue(result.key.writable, true, 'result.key is writable');
assert.sameValue(result.key.value, symValue, 'result.key has value symValue');
assert.sameValue(result[enumSym].configurable, true, 'result[enumSym] is configurable');
assert.sameValue(result[enumSym].enumerable, true, 'result[enumSym] is enumerable');
assert.sameValue(result[enumSym].writable, true, 'result[enumSym] is writable');
assert.sameValue(result[enumSym].value, value, 'result[enumSym] has value `value`');
assert.sameValue(result[nonEnumSym].configurable, true, 'result[nonEnumSym] is configurable');
assert.sameValue(result[nonEnumSym].enumerable, false, 'result[nonEnumSym] is not enumerable');
assert.sameValue(result[nonEnumSym].writable, true, 'result[nonEnumSym] is writable');
assert.sameValue(result[nonEnumSym].value, value, 'result[nonEnumSym] has value `value`');

View File

@ -0,0 +1,22 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Object.getOwnPropertyDescriptors should not have its behavior impacted by modifications to the global property Object
id: pending
author: Jordan Harband
includes: [fnGlobalObject.js]
---*/
function fakeObject() {
$ERROR('The overriden version of Object was called!');
}
fakeObject.getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;
fakeObject.keys = Object.keys;
var global = fnGlobalObject();
global.Object = fakeObject;
assert.sameValue(Object, fakeObject, 'Sanity check failed: could not modify the global Object');
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors('a')).length, 2, 'Expected string primitive to have 2 descriptors');

View File

@ -0,0 +1,22 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
Object.getOwnPropertyDescriptors should not have its behavior impacted by modifications to Object.getOwnPropertyDescriptor
id: pending
author: Jordan Harband
---*/
function fakeObjectGetOwnPropertyDescriptor() {
$ERROR('The overriden version of Object.getOwnPropertyDescriptor was called!');
}
Object.getOwnPropertyDescriptor = fakeObjectGetOwnPropertyDescriptor;
assert.sameValue(
Object.getOwnPropertyDescriptor,
fakeObjectGetOwnPropertyDescriptor,
'Sanity check failed: could not modify the global Object.getOwnPropertyDescriptor'
);
assert.sameValue(Object.keys(Object.getOwnPropertyDescriptors({ a: 1 })).length, 1, 'Expected object with 1 key to have 1 descriptor');

View File

@ -6,11 +6,12 @@ id: sec-object.values
description: Object.values should perform observable operations in the correct order
author: Jordan Harband
features: [Proxy]
includes: [proxyTrapsHelper.js]
---*/
var log = "";
var object = { a: 0, b: 0, c: 0 };
var handler = {
var handler = allowProxyTraps({
get: function (target, propertyKey, receiver) {
assert.sameValue(target, object, "get target");
assert.sameValue(receiver, proxy, "get receiver");
@ -26,23 +27,14 @@ var handler = {
assert.sameValue(target, object, "ownKeys");
log += "|ownKeys";
return Object.getOwnPropertyNames(target);
},
deleteProperty: function (oTarget, sKey) {
throw new Test262Error('properties should not be deleted');
},
defineProperty: function (oTarget, sKey, oDesc) {
throw new Test262Error('properties should not be defined');
},
set: function (oTarget, sKey, vValue) {
throw new Test262Error('properties should not be assigned');
}
};
var check = {
});
var check = allowProxyTraps({
get: function (target, propertyKey, receiver) {
assert(propertyKey in target, "handler check: " + propertyKey);
return target[propertyKey];
}
};
});
var proxy = new Proxy(object, new Proxy(handler, check));
var result = Object.values(proxy);
assert.sameValue(log, "|ownKeys|getOwnPropertyDescriptor:a|get:a|getOwnPropertyDescriptor:b|get:b|getOwnPropertyDescriptor:c|get:c", log);

View File

@ -0,0 +1,40 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: allowProxyTraps helper should default throw on all the proxy trap named methods being invoked
id: pending
author: Jordan Harband
includes: [proxyTrapsHelper.js]
---*/
var traps = allowProxyTraps();
function assertTrapThrows(trap) {
if (typeof traps[trap] !== 'function') {
throw new Test262Error('trap ' + trap + ' is not a function');
}
var failedToThrow = false;
try {
traps[trap]();
failedToThrow = true;
} catch (e) {}
if (failedToThrow) {
throw new Test262Error('trap ' + trap + ' did not throw an error');
}
}
assertTrapThrows('getPrototypeOf');
assertTrapThrows('setPrototypeOf');
assertTrapThrows('isExtensible');
assertTrapThrows('preventExtensions');
assertTrapThrows('getOwnPropertyDescriptor');
assertTrapThrows('has');
assertTrapThrows('get');
assertTrapThrows('set');
assertTrapThrows('deleteProperty');
assertTrapThrows('defineProperty');
assertTrapThrows('enumerate');
assertTrapThrows('ownKeys');
assertTrapThrows('apply');
assertTrapThrows('construct');

View File

@ -0,0 +1,75 @@
// Copyright (C) 2016 Jordan Harband. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: allowProxyTraps helper should default throw on all the proxy trap named methods being invoked
id: pending
author: Jordan Harband
includes: [proxyTrapsHelper.js]
---*/
var overrides = {
getPrototypeOf: function () {},
setPrototypeOf: function () {},
isExtensible: function () {},
preventExtensions: function () {},
getOwnPropertyDescriptor: function () {},
has: function () {},
get: function () {},
set: function () {},
deleteProperty: function () {},
defineProperty: function () {},
enumerate: function () {},
ownKeys: function () {},
apply: function () {},
construct: function () {},
};
var traps = allowProxyTraps(overrides);
function assertTrapSucceeds(trap) {
if (typeof traps[trap] !== 'function') {
throw new Test262Error('trap ' + trap + ' is not a function');
}
if (traps[trap] !== overrides[trap]) {
throw new Test262Error('trap ' + trap + ' was not overriden in allowProxyTraps');
}
var threw = false;
try {
traps[trap]();
} catch (e) {
threw = true;
}
if (threw) {
throw new Test262Error('trap ' + trap + ' threw an error');
}
}
function assertTrapThrows(trap) {
if (typeof traps[trap] !== 'function') {
throw new Test262Error('trap ' + trap + ' is not a function');
}
var failedToThrow = false;
try {
traps[trap]();
failedToThrow = true;
} catch (e) {}
if (failedToThrow) {
throw new Test262Error('trap ' + trap + ' did not throw an error');
}
}
assertTrapSucceeds('getPrototypeOf');
assertTrapSucceeds('setPrototypeOf');
assertTrapSucceeds('isExtensible');
assertTrapSucceeds('preventExtensions');
assertTrapSucceeds('getOwnPropertyDescriptor');
assertTrapSucceeds('has');
assertTrapSucceeds('get');
assertTrapSucceeds('set');
assertTrapSucceeds('deleteProperty');
assertTrapSucceeds('defineProperty');
assertTrapSucceeds('ownKeys');
assertTrapSucceeds('apply');
assertTrapSucceeds('construct');
// enumerate should always throw because the trap has been removed
assertTrapThrows('enumerate');