Use SameValue in verifyProperty helper (#2185)

This commit is contained in:
Alexey Shvayka 2019-06-12 21:14:14 +03:00 committed by Leo Balter
parent 430ee14d90
commit 57f0884396
2 changed files with 26 additions and 6 deletions

View File

@ -47,7 +47,7 @@ function verifyProperty(obj, name, desc, options) {
var failures = [];
if (Object.prototype.hasOwnProperty.call(desc, 'value')) {
if (desc.value !== originalDesc.value) {
if (!isSameValue(desc.value, originalDesc.value)) {
failures.push("descriptor value should be " + desc.value);
}
}
@ -113,10 +113,11 @@ function isEnumerable(obj, name) {
Object.prototype.propertyIsEnumerable.call(obj, name);
}
function isEqualTo(obj, name, expectedValue) {
var actualValue = obj[name];
function isSameValue(a, b) {
if (a === 0 && b === 0) return 1 / a === 1 / b;
if (a !== a && b !== b) return true;
return assert._isSameValue(actualValue, expectedValue);
return a === b;
}
function isWritable(obj, name, verifyProp, value) {
@ -133,7 +134,7 @@ function isWritable(obj, name, verifyProp, value) {
}
}
writeSucceeded = isEqualTo(obj, verifyProp || name, newValue);
writeSucceeded = isSameValue(obj[verifyProp || name], newValue);
// Revert the change only if it was successful (in other cases, reverting
// is unnecessary and may trigger exceptions for certain property
@ -150,7 +151,7 @@ function isWritable(obj, name, verifyProp, value) {
}
function verifyEqualTo(obj, name, value) {
if (!isEqualTo(obj, name, value)) {
if (!isSameValue(obj[name], value)) {
$ERROR("Expected obj[" + String(name) + "] to equal " + value +
", actually " + obj[name]);
}

View File

@ -0,0 +1,19 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
description: >
verifyProperty uses SameValue for value comparison.
includes: [propertyHelper.js]
---*/
var obj = {
a: NaN,
b: -0,
};
assert(verifyProperty(obj, 'a', { value: NaN }));
assert(verifyProperty(obj, 'b', { value: -0 }));
assert.throws(Test262Error, function() {
verifyProperty(obj, 'b', { value: 0 });
});