mirror of
https://github.com/tc39/test262.git
synced 2025-07-20 12:34:41 +02:00
Add Verify Property harness (#979)
This commit is contained in:
parent
1ba3a7c4a9
commit
e43ce56105
@ -1,4 +1,74 @@
|
|||||||
|
|
||||||
|
function verifyProperty(obj, name, desc, options) {
|
||||||
|
assert(
|
||||||
|
arguments.length > 2,
|
||||||
|
'verifyProperty should receive at least 3 arguments: obj, name, and descriptor'
|
||||||
|
);
|
||||||
|
|
||||||
|
var originalDesc = Object.getOwnPropertyDescriptor(obj, name);
|
||||||
|
var nameStr = String(name);
|
||||||
|
|
||||||
|
// Allows checking for undefined descriptor if it's explicitly given.
|
||||||
|
if (desc === undefined) {
|
||||||
|
assert.sameValue(
|
||||||
|
originalDesc,
|
||||||
|
undefined,
|
||||||
|
`obj['${nameStr}'] descriptor should be undefined`
|
||||||
|
);
|
||||||
|
|
||||||
|
// desc and originalDesc are both undefined, problem solved;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, name),
|
||||||
|
`obj should have an own property ${nameStr}`
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.notSameValue(
|
||||||
|
desc,
|
||||||
|
null,
|
||||||
|
`The desc argument should be an object or undefined, null`
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
typeof desc,
|
||||||
|
"object",
|
||||||
|
`The desc argument should be an object or undefined, ${String(desc)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
var failures = [];
|
||||||
|
|
||||||
|
if (Object.prototype.hasOwnProperty.call(desc, 'enumerable')) {
|
||||||
|
if (desc.enumerable !== originalDesc.enumerable ||
|
||||||
|
desc.enumerable !== isEnumerable(obj, name)) {
|
||||||
|
failures.push(`descriptor should ${desc.enumerable ? '' : 'not '}be enumerable`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.prototype.hasOwnProperty.call(desc, 'writable')) {
|
||||||
|
if (desc.writable !== originalDesc.writable ||
|
||||||
|
desc.writable !== isWritable(obj, name)) {
|
||||||
|
failures.push(`descriptor should ${desc.writable ? '' : 'not '}be writable`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.prototype.hasOwnProperty.call(desc, 'configurable')) {
|
||||||
|
if (desc.configurable !== originalDesc.configurable ||
|
||||||
|
desc.configurable !== isConfigurable(obj, name)) {
|
||||||
|
failures.push(`descriptor should ${desc.configurable ? '' : 'not '}be configurable`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.sameValue(failures.length, 0, failures.join('; '));
|
||||||
|
|
||||||
|
if (options && options.restore) {
|
||||||
|
Object.defineProperty(obj, name, originalDesc);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
function isConfigurable(obj, name) {
|
function isConfigurable(obj, name) {
|
||||||
try {
|
try {
|
||||||
delete obj[name];
|
delete obj[name];
|
||||||
@ -11,7 +81,7 @@ function isConfigurable(obj, name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isEnumerable(obj, name) {
|
function isEnumerable(obj, name) {
|
||||||
var stringCheck;
|
var stringCheck = false;
|
||||||
|
|
||||||
if (typeof name === "string") {
|
if (typeof name === "string") {
|
||||||
for (var x in obj) {
|
for (var x in obj) {
|
||||||
|
25
test/harness/verifyProperty-arguments.js
Normal file
25
test/harness/verifyProperty-arguments.js
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
verifyProperty should receive at least 3 arguments: obj, name, and descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
// monkeypatch the API
|
||||||
|
$ERROR = function $ERROR(message) {
|
||||||
|
throw new Test262Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty();
|
||||||
|
}, "0 arguments");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(Object);
|
||||||
|
}, "1 argument");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(Object, 'foo');
|
||||||
|
}, "2 arguments");
|
35
test/harness/verifyProperty-desc-is-not-object.js
Normal file
35
test/harness/verifyProperty-desc-is-not-object.js
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
The desc argument should be an object or undefined
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
// monkeypatch the API
|
||||||
|
$ERROR = function $ERROR(message) {
|
||||||
|
throw new Test262Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
var sample = { foo: 42 };
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, "foo", 'configurable');
|
||||||
|
}, "string");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, 'foo', true);
|
||||||
|
}, "boolean");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, 'foo', 42);
|
||||||
|
}, "number");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, 'foo', null);
|
||||||
|
}, "null");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, 'foo', Symbol(1));
|
||||||
|
}, "symbol");
|
21
test/harness/verifyProperty-noproperty.js
Normal file
21
test/harness/verifyProperty-noproperty.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
The first argument should have an own property
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
// monkeypatch the API
|
||||||
|
$ERROR = function $ERROR(message) {
|
||||||
|
throw new Test262Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(Object, 'JeanPaulSartre', {});
|
||||||
|
}, "inexisting property");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty({}, 'hasOwnProperty', {});
|
||||||
|
}, "inexisting own property");
|
42
test/harness/verifyProperty-restore-accessor-symbol.js
Normal file
42
test/harness/verifyProperty-restore-accessor-symbol.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
verifyProperty allows restoring the original accessor descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var obj;
|
||||||
|
var prop = Symbol(1);
|
||||||
|
var desc = { enumerable: true, configurable: true, get() { return 42; }, set() {} };
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc, { restore: true });
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
assert.sameValue(obj[prop], 42);
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getOwnPropertyDescriptor(obj, prop).get,
|
||||||
|
desc.get
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getOwnPropertyDescriptor(obj, prop).set,
|
||||||
|
desc.set
|
||||||
|
);
|
42
test/harness/verifyProperty-restore-accessor.js
Normal file
42
test/harness/verifyProperty-restore-accessor.js
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
verifyProperty allows restoring the original accessor descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var obj;
|
||||||
|
var prop = "prop";
|
||||||
|
var desc = { enumerable: true, configurable: true, get() { return 42; }, set() {} };
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc, { restore: true });
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
assert.sameValue(obj[prop], 42);
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getOwnPropertyDescriptor(obj, prop).get,
|
||||||
|
desc.get
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.getOwnPropertyDescriptor(obj, prop).set,
|
||||||
|
desc.set
|
||||||
|
);
|
33
test/harness/verifyProperty-restore-symbol.js
Normal file
33
test/harness/verifyProperty-restore-symbol.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
verifyProperty allows restoring the original descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var obj;
|
||||||
|
var prop = Symbol(1);
|
||||||
|
var desc = { enumerable: true, configurable: true, writable: true, value: 42 };
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc, { restore: true });
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
assert.sameValue(obj[prop], 42);
|
33
test/harness/verifyProperty-restore.js
Normal file
33
test/harness/verifyProperty-restore.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
verifyProperty allows restoring the original descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var obj;
|
||||||
|
var prop = 'prop';
|
||||||
|
var desc = { enumerable: true, configurable: true, writable: true, value: 42 };
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
false
|
||||||
|
);
|
||||||
|
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
|
||||||
|
verifyProperty(obj, prop, desc, { restore: true });
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
Object.prototype.hasOwnProperty.call(obj, prop),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
assert.sameValue(obj[prop], 42);
|
51
test/harness/verifyProperty-string-prop.js
Normal file
51
test/harness/verifyProperty-string-prop.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
Verify property descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var obj;
|
||||||
|
var prop = 'prop';
|
||||||
|
|
||||||
|
function reset(desc) {
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkDesc(desc) {
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, desc));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { enumerable: desc.enumerable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { writable: desc.writable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { configurable: desc.configurable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { configurable: desc.configurable, enumerable: desc.enumerable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { configurable: desc.configurable, writable: desc.writable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { writable: desc.writable, enumerable: desc.enumerable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { enumerable: desc.enumerable, configurable: desc.configurable }));
|
||||||
|
}
|
||||||
|
|
||||||
|
checkDesc({ enumerable: true, configurable: true, writable: true });
|
||||||
|
checkDesc({ enumerable: false, writable: false, configurable: false });
|
||||||
|
checkDesc({ enumerable: true, writable: false, configurable: false });
|
||||||
|
checkDesc({ enumerable: false, writable: true, configurable: false });
|
||||||
|
checkDesc({ enumerable: false, writable: false, configurable: true });
|
||||||
|
checkDesc({ enumerable: true, writable: false, configurable: true });
|
||||||
|
checkDesc({ enumerable: true, writable: true, configurable: false });
|
||||||
|
checkDesc({ enumerable: false, writable: true, configurable: true });
|
51
test/harness/verifyProperty-symbol-prop.js
Normal file
51
test/harness/verifyProperty-symbol-prop.js
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
Verify symbol named property descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
var obj;
|
||||||
|
var prop = Symbol(1);
|
||||||
|
|
||||||
|
function reset(desc) {
|
||||||
|
obj = {};
|
||||||
|
Object.defineProperty(obj, prop, desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkDesc(desc) {
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, desc));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { enumerable: desc.enumerable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { writable: desc.writable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { configurable: desc.configurable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { configurable: desc.configurable, enumerable: desc.enumerable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { configurable: desc.configurable, writable: desc.writable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { writable: desc.writable, enumerable: desc.enumerable }));
|
||||||
|
|
||||||
|
reset(desc);
|
||||||
|
assert(verifyProperty(obj, prop, { enumerable: desc.enumerable, configurable: desc.configurable }));
|
||||||
|
}
|
||||||
|
|
||||||
|
checkDesc({ enumerable: true, configurable: true, writable: true });
|
||||||
|
checkDesc({ enumerable: false, writable: false, configurable: false });
|
||||||
|
checkDesc({ enumerable: true, writable: false, configurable: false });
|
||||||
|
checkDesc({ enumerable: false, writable: true, configurable: false });
|
||||||
|
checkDesc({ enumerable: false, writable: false, configurable: true });
|
||||||
|
checkDesc({ enumerable: true, writable: false, configurable: true });
|
||||||
|
checkDesc({ enumerable: true, writable: true, configurable: false });
|
||||||
|
checkDesc({ enumerable: false, writable: true, configurable: true });
|
32
test/harness/verifyProperty-undefined-desc.js
Normal file
32
test/harness/verifyProperty-undefined-desc.js
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
// Copyright (C) 2017 Leo Balter. All rights reserved.
|
||||||
|
// This code is governed by the BSD license found in the LICENSE file.
|
||||||
|
|
||||||
|
/*---
|
||||||
|
description: >
|
||||||
|
Verify an undefined descriptor
|
||||||
|
includes: [propertyHelper.js]
|
||||||
|
---*/
|
||||||
|
|
||||||
|
// monkeypatch the API
|
||||||
|
$ERROR = function $ERROR(message) {
|
||||||
|
throw new Test262Error(message);
|
||||||
|
};
|
||||||
|
|
||||||
|
var sample = {
|
||||||
|
bar: undefined,
|
||||||
|
get baz() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
assert.sameValue(
|
||||||
|
verifyProperty(sample, "foo", undefined),
|
||||||
|
true,
|
||||||
|
"returns true if desc and property descriptor are both undefined"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, 'bar', undefined);
|
||||||
|
}, "dataDescriptor value is undefined");
|
||||||
|
|
||||||
|
assert.throws(Test262Error, () => {
|
||||||
|
verifyProperty(sample, 'baz', undefined);
|
||||||
|
}, "accessor returns undefined");
|
Loading…
x
Reference in New Issue
Block a user