Add Object.getOwnPropertyDescriptors tests.

This commit is contained in:
Jordan Harband 2016-02-01 01:45:23 -08:00
parent 07aafd0c63
commit cf578d5190
13 changed files with 307 additions and 0 deletions

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
es7id: 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
es7id: 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'
es7id: 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
es7id: pending
author: Jordan Harband
includes: [propertyHelper.js]
---*/
verifyNotEnumerable(Object, 'getOwnPropertyDescriptors');
verifyWritable(Object, 'getOwnPropertyDescriptors');
verifyConfigurable(Object, 'getOwnPropertyDescriptors');

View File

@ -0,0 +1,42 @@
// 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.
es7id: pending
author: Jordan Harband
---*/
var F = function G() {};
F.prototype.a = {};
F.prototype.b = {};
var f = new F();
f.b = {}; // 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, f.b, 'bs value is f.b');
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,
Object.getOwnPropertyNames(f).length,
'result has same number of own property names as f'
);

View File

@ -0,0 +1,45 @@
// 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
es7id: pending
author: Jordan Harband
features: [Proxy]
---*/
var log = "";
var object = { a: 0, b: 0, c: 0 };
var handler = {
get: function (target, propertyKey, receiver) {
throw new Test262Error('no values should be retrieved via [[Get]]');
},
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);
},
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 = {
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.
es7id: 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.
es7id: 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.
es7id: 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.
es7id: 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 does not include Symbol keys.
es7id: 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,21 @@
// 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
es7id: pending
author: Jordan Harband
---*/
function fakeObject() {
$ERROR('The overriden version of Object was called!');
}
fakeObject.getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors;
fakeObject.keys = Object.keys;
var global = Function('return this;')();
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
es7id: 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');