From 7b1eddf6b06fe0699089c6360e595d01b4093940 Mon Sep 17 00:00:00 2001 From: Leonardo Balter Date: Thu, 30 Jun 2016 15:05:47 -0300 Subject: [PATCH] Update and expand tests for isNaN --- test/built-ins/isNaN/S15.1.2.4_A1_T1.js | 70 ------------------ test/built-ins/isNaN/S15.1.2.4_A1_T2.js | 40 ---------- test/built-ins/isNaN/S15.1.2.4_A2.1.js | 25 ------- test/built-ins/isNaN/S15.1.2.4_A2.2.js | 25 ------- test/built-ins/isNaN/S15.1.2.4_A2.3.js | 16 ---- test/built-ins/isNaN/S15.1.2.4_A2.4.js | 13 ---- test/built-ins/isNaN/S15.1.2.4_A2.5.js | 25 ------- test/built-ins/isNaN/length.js | 16 ++++ test/built-ins/isNaN/prop-desc.js | 14 ++++ ...turn-abrupt-from-tonumber-number-symbol.js | 20 +++++ .../return-abrupt-from-tonumber-number.js | 33 +++++++++ .../isNaN/return-false-not-nan-numbers.js | 28 +++++++ test/built-ins/isNaN/return-true-nan.js | 19 +++++ test/built-ins/isNaN/tonumber-operations.js | 28 +++++++ .../isNaN/toprimitive-call-abrupt.js | 30 ++++++++ .../built-ins/isNaN/toprimitive-get-abrupt.js | 29 ++++++++ .../isNaN/toprimitive-not-callable-throws.js | 53 ++++++++++++++ .../toprimitive-result-is-object-throws.js | 32 ++++++++ .../toprimitive-result-is-symbol-throws.js | 36 +++++++++ .../isNaN/toprimitive-valid-result.js | 73 +++++++++++++++++++ 20 files changed, 411 insertions(+), 214 deletions(-) delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A1_T1.js delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A1_T2.js delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A2.1.js delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A2.2.js delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A2.3.js delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A2.4.js delete mode 100644 test/built-ins/isNaN/S15.1.2.4_A2.5.js create mode 100644 test/built-ins/isNaN/length.js create mode 100644 test/built-ins/isNaN/prop-desc.js create mode 100644 test/built-ins/isNaN/return-abrupt-from-tonumber-number-symbol.js create mode 100644 test/built-ins/isNaN/return-abrupt-from-tonumber-number.js create mode 100644 test/built-ins/isNaN/return-false-not-nan-numbers.js create mode 100644 test/built-ins/isNaN/return-true-nan.js create mode 100644 test/built-ins/isNaN/tonumber-operations.js create mode 100644 test/built-ins/isNaN/toprimitive-call-abrupt.js create mode 100644 test/built-ins/isNaN/toprimitive-get-abrupt.js create mode 100644 test/built-ins/isNaN/toprimitive-not-callable-throws.js create mode 100644 test/built-ins/isNaN/toprimitive-result-is-object-throws.js create mode 100644 test/built-ins/isNaN/toprimitive-result-is-symbol-throws.js create mode 100644 test/built-ins/isNaN/toprimitive-valid-result.js diff --git a/test/built-ins/isNaN/S15.1.2.4_A1_T1.js b/test/built-ins/isNaN/S15.1.2.4_A1_T1.js deleted file mode 100644 index 7280a328d4..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A1_T1.js +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: > - isNaN applies ToNumber to its argument, then return true if the result is - NaN, and otherwise return false -es5id: 15.1.2.4_A1_T1 -description: Checking all primitive ----*/ - -// CHECK#1 -if (!(isNaN(NaN) === true)) { - $ERROR('#1: NaN === Not-a-Number. Actual: ' + (NaN)); -} - -// CHECK#2 -if (!(isNaN(Number.NaN) === true)) { - $ERROR('#2: Number.NaN === Not-a-Number. Actual: ' + (Number.NaN)); -} - -// CHECK#3 -if (!(isNaN(Number(void 0)) === true)) { - $ERROR('#3: Number(void 0) === Not-a-Number. Actual: ' + (Number(void 0))); -} - -// CHECK#4 -if (!(isNaN(void 0) === true)) { - $ERROR('#4: void 0 === Not-a-Number. Actual: ' + (void 0)); -} - -// CHECK#5 -if (!(isNaN("string") === true)) { - $ERROR('#5: "string" === Not-a-Number. Actual: ' + ("string")); -} - -// CHECK#6 -if (isNaN(Number.POSITIVE_INFINITY) === true) { - $ERROR('#6: Number.POSITIVE_INFINITY !== Not-a-Number'); -} - -// CHECK#7 -if (isNaN(Number.NEGATIVE_INFINITY) === true) { - $ERROR('#7: Number.NEGATIVE_INFINITY !== Not-a-Number'); -} - -// CHECK#8 -if (isNaN(Number.MAX_VALUE) === true) { - $ERROR('#8: Number.MAX_VALUE !== Not-a-Number'); -} - -// CHECK#9 -if (isNaN(Number.MIN_VALUE) === true) { - $ERROR('#9: Number.MIN_VALUE !== Not-a-Number'); -} - -// CHECK#10 -if (isNaN(-0) === true) { - $ERROR('#10: -0 !== Not-a-Number'); -} - -// CHECK#11 -if (isNaN(true) === true) { - $ERROR('#11: true !== Not-a-Number'); -} - -// CHECK#12 -if (isNaN("1") === true) { - $ERROR('#12: "1" !== Not-a-Number'); -} diff --git a/test/built-ins/isNaN/S15.1.2.4_A1_T2.js b/test/built-ins/isNaN/S15.1.2.4_A1_T2.js deleted file mode 100644 index 34bb54409e..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A1_T2.js +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: > - isNaN applies ToNumber to its argument, then return true if the result is - NaN, and otherwise return false -es5id: 15.1.2.4_A1_T2 -description: Checking all object ----*/ - -// CHECK#1 -if (!(isNaN({}) === true)) { - $ERROR('#1: {} === Not-a-Number. Actual: ' + ({})); -} - -// CHECK#2 -if (!(isNaN(new String("string")) === true)) { - $ERROR('#2: new String("string") === Not-a-Number. Actual: ' + (new String("string"))); -} - -// CHECK#3 -if (isNaN(new String("1")) === true) { - $ERROR('#3: new String("1") === Not-a-Number. Actual: ' + (new String("1"))); -} - -// CHECK#4 -if (isNaN(new Number(1)) === true) { - $ERROR('#4: new Number(1) !== Not-a-Number'); -} - -// CHECK#5 -if (!(isNaN(new Number(NaN)) === true)) { - $ERROR('#5: new Number(NaN) === Not-a-Number. Actual: ' + (new Number(NaN))); -} - -// CHECK#6 -if (isNaN(new Boolean(true)) === true) { - $ERROR('#6: new Boolean(true) !== Not-a-Number'); -} diff --git a/test/built-ins/isNaN/S15.1.2.4_A2.1.js b/test/built-ins/isNaN/S15.1.2.4_A2.1.js deleted file mode 100644 index 820f918b71..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A2.1.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: The length property of isNaN has the attribute DontEnum -es5id: 15.1.2.4_A2.1 -description: Checking use propertyIsEnumerable, for-in ----*/ - -//CHECK#1 -if (isNaN.propertyIsEnumerable('length') !== false) { - $ERROR('#1: isNaN.propertyIsEnumerable(\'length\') === false. Actual: ' + (isNaN.propertyIsEnumerable('length'))); -} - -//CHECK#2 -var result = true; -for (var p in isNaN){ - if (p === "length") { - result = false; - } -} - -if (result !== true) { - $ERROR('#2: result = true; for (p in isNaN) { if (p === "length") result = false; } result === true;'); -} diff --git a/test/built-ins/isNaN/S15.1.2.4_A2.2.js b/test/built-ins/isNaN/S15.1.2.4_A2.2.js deleted file mode 100644 index 0ce5cf3be5..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A2.2.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: The length property of isNaN does not have the attribute DontDelete -es5id: 15.1.2.4_A2.2 -description: Checking use hasOwnProperty, delete ----*/ - -//CHECK#1 -if (isNaN.hasOwnProperty('length') !== true) { - $ERROR('#1: isNaN.hasOwnProperty(\'length\') === true. Actual: ' + (isNaN.hasOwnProperty('length'))); -} - -delete isNaN.length; - -//CHECK#2 -if (isNaN.hasOwnProperty('length') !== false) { - $ERROR('#2: delete isNaN.length; isNaN.hasOwnProperty(\'length\') === false. Actual: ' + (isNaN.hasOwnProperty('length'))); -} - -//CHECK#3 -if (isNaN.length === undefined) { - $ERROR('#3: delete isNaN.length; isNaN.length !== undefined'); -} diff --git a/test/built-ins/isNaN/S15.1.2.4_A2.3.js b/test/built-ins/isNaN/S15.1.2.4_A2.3.js deleted file mode 100644 index 99efe94e3f..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A2.3.js +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: The length property of isNaN has the attribute ReadOnly -es5id: 15.1.2.4_A2.3 -description: Checking if varying the length property fails -includes: [propertyHelper.js] ----*/ - -//CHECK#1 -var x = isNaN.length; -verifyNotWritable(isNaN, "length", null, Infinity); -if (isNaN.length !== x) { - $ERROR('#1: x = isNaN.length; isNaN.length = Infinity; isNaN.length === x. Actual: ' + (isNaN.length)); -} diff --git a/test/built-ins/isNaN/S15.1.2.4_A2.4.js b/test/built-ins/isNaN/S15.1.2.4_A2.4.js deleted file mode 100644 index c485ce45cc..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A2.4.js +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: The length property of isNaN is 1 -es5id: 15.1.2.4_A2.4 -description: isNaN.length === 1 ----*/ - -//CHECK#1 -if (isNaN.length !== 1) { - $ERROR('#1: isNaN.length === 1. Actual: ' + (isNaN.length)); -} diff --git a/test/built-ins/isNaN/S15.1.2.4_A2.5.js b/test/built-ins/isNaN/S15.1.2.4_A2.5.js deleted file mode 100644 index 645cd549f5..0000000000 --- a/test/built-ins/isNaN/S15.1.2.4_A2.5.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2009 the Sputnik authors. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -info: The isNaN property has the attribute DontEnum -es5id: 15.1.2.4_A2.5 -description: Checking use propertyIsEnumerable, for-in ----*/ - -//CHECK#1 -if (this.propertyIsEnumerable('isNaN') !== false) { - $ERROR('#1: this.propertyIsEnumerable(\'isNaN\') === false. Actual: ' + (this.propertyIsEnumerable('isNaN'))); -} - -//CHECK#2 -var result = true; -for (var p in this){ - if (p === "isNaN") { - result = false; - } -} - -if (result !== true) { - $ERROR('#2: result = true; for (p in this) { if (p === "isNaN") result = false; } result === true;'); -} diff --git a/test/built-ins/isNaN/length.js b/test/built-ins/isNaN/length.js new file mode 100644 index 0000000000..a35ae0f5d0 --- /dev/null +++ b/test/built-ins/isNaN/length.js @@ -0,0 +1,16 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + The length property of isNaN is 1 +includes: [propertyHelper.js] +---*/ + +assert.sameValue(isNaN.length, 1, "The value of `isNaN.length` is `1`"); + +verifyNotEnumerable(isNaN, "length"); +verifyNotWritable(isNaN, "length"); +verifyConfigurable(isNaN, "length"); diff --git a/test/built-ins/isNaN/prop-desc.js b/test/built-ins/isNaN/prop-desc.js new file mode 100644 index 0000000000..3228f1824e --- /dev/null +++ b/test/built-ins/isNaN/prop-desc.js @@ -0,0 +1,14 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Property descriptor for isNaN +includes: [propertyHelper.js] +---*/ + +verifyNotEnumerable(this, "isNaN"); +verifyWritable(this, "isNaN"); +verifyConfigurable(this, "isNaN"); diff --git a/test/built-ins/isNaN/return-abrupt-from-tonumber-number-symbol.js b/test/built-ins/isNaN/return-abrupt-from-tonumber-number-symbol.js new file mode 100644 index 0000000000..cfacbf8e71 --- /dev/null +++ b/test/built-ins/isNaN/return-abrupt-from-tonumber-number-symbol.js @@ -0,0 +1,20 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Throws a TypeError if number is a Symbol +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). +features: [Symbol] +---*/ + +var s = Symbol(1); + +assert.throws(TypeError, function() { + isNaN(s); +}); diff --git a/test/built-ins/isNaN/return-abrupt-from-tonumber-number.js b/test/built-ins/isNaN/return-abrupt-from-tonumber-number.js new file mode 100644 index 0000000000..b4f062bd88 --- /dev/null +++ b/test/built-ins/isNaN/return-abrupt-from-tonumber-number.js @@ -0,0 +1,33 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Return abrupt completion from ToNumber(number) +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). +---*/ + +var obj1 = { + valueOf: function() { + throw new Test262Error(); + } +}; + +var obj2 = { + toString: function() { + throw new Test262Error(); + } +}; + +assert.throws(Test262Error, function() { + isNaN(obj1); +}, "valueOf"); + +assert.throws(Test262Error, function() { + isNaN(obj2); +}, "toString"); diff --git a/test/built-ins/isNaN/return-false-not-nan-numbers.js b/test/built-ins/isNaN/return-false-not-nan-numbers.js new file mode 100644 index 0000000000..cf9c9bef55 --- /dev/null +++ b/test/built-ins/isNaN/return-false-not-nan-numbers.js @@ -0,0 +1,28 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Return false if number is not NaN +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + 2. If num is NaN, return true. + 3. Otherwise, return false. +---*/ + +assert.sameValue(isNaN(0), false, "0"); +assert.sameValue(isNaN(-0), false, "-0"); +assert.sameValue(isNaN(Math.pow(2, 53)), false, "Math.pow(2, 53)"); +assert.sameValue(isNaN(-Math.pow(2, 53)), false, "-Math.pow(2, 53)"); +assert.sameValue(isNaN(1), false, "1"); +assert.sameValue(isNaN(-1), false, "-1"); +assert.sameValue(isNaN(0.000001), false, "0.000001"); +assert.sameValue(isNaN(-0.000001), false, "-0.000001"); +assert.sameValue(isNaN(1e42), false, "1e42"); +assert.sameValue(isNaN(-1e42), false, "-1e42"); +assert.sameValue(isNaN(Infinity), false, "Infinity"); +assert.sameValue(isNaN(-Infinity), false, "-Infinity"); diff --git a/test/built-ins/isNaN/return-true-nan.js b/test/built-ins/isNaN/return-true-nan.js new file mode 100644 index 0000000000..876c4b5833 --- /dev/null +++ b/test/built-ins/isNaN/return-true-nan.js @@ -0,0 +1,19 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Return true if number is NaN +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + 2. If num is NaN, return true. +includes: [nans.js] +---*/ + +distinctNaNs.forEach(function(v, i) { + assert.sameValue(isNaN(v), true, "value on position: " + i); +}); diff --git a/test/built-ins/isNaN/tonumber-operations.js b/test/built-ins/isNaN/tonumber-operations.js new file mode 100644 index 0000000000..744b8973e0 --- /dev/null +++ b/test/built-ins/isNaN/tonumber-operations.js @@ -0,0 +1,28 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + number argument is converted by ToNumber +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + 2. If num is NaN, return true. + 3. Otherwise, return false. +---*/ + +assert.sameValue(isNaN("0"), false, "'0'"); +assert.sameValue(isNaN(""), false, "the empty string"); +assert.sameValue(isNaN("Infinity"), false, "'Infinity'"); +assert.sameValue(isNaN("this is not a number"), true, "string"); +assert.sameValue(isNaN(true), false, "true"); +assert.sameValue(isNaN(false), false, "false"); +assert.sameValue(isNaN([1]), false, "Object [1]"); +assert.sameValue(isNaN([Infinity]), false, "Object [Infinity]"); +assert.sameValue(isNaN([NaN]), true, "Object [NaN]"); +assert.sameValue(isNaN(null), false, "null"); +assert.sameValue(isNaN(undefined), true, "undefined"); +assert.sameValue(isNaN(), true, "no arg"); diff --git a/test/built-ins/isNaN/toprimitive-call-abrupt.js b/test/built-ins/isNaN/toprimitive-call-abrupt.js new file mode 100644 index 0000000000..31152ee4fd --- /dev/null +++ b/test/built-ins/isNaN/toprimitive-call-abrupt.js @@ -0,0 +1,30 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Return abrupt completion calling number.@@toPrimitive +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + + ToPrimitive ( input [ , PreferredType ] ) + + [...] + 4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + 5. If exoticToPrim is not undefined, then + a. Let result be ? Call(exoticToPrim, input, « hint »). +features: [Symbol.toPrimitive] +---*/ + +var obj = {}; +obj[Symbol.toPrimitive] = function() { + throw new Test262Error(); +}; + +assert.throws(Test262Error, function() { + isNaN(obj); +}); diff --git a/test/built-ins/isNaN/toprimitive-get-abrupt.js b/test/built-ins/isNaN/toprimitive-get-abrupt.js new file mode 100644 index 0000000000..c3cb940cde --- /dev/null +++ b/test/built-ins/isNaN/toprimitive-get-abrupt.js @@ -0,0 +1,29 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Return abrupt completion getting number.@@toPrimitive +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + + ToPrimitive ( input [ , PreferredType ] ) + + [...] + 4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). +features: [Symbol.toPrimitive] +---*/ + +var obj = Object.defineProperty({}, Symbol.toPrimitive, { + get: function() { + throw new Test262Error(); + } +}); + +assert.throws(Test262Error, function() { + isNaN(obj); +}); diff --git a/test/built-ins/isNaN/toprimitive-not-callable-throws.js b/test/built-ins/isNaN/toprimitive-not-callable-throws.js new file mode 100644 index 0000000000..dff4ed660c --- /dev/null +++ b/test/built-ins/isNaN/toprimitive-not-callable-throws.js @@ -0,0 +1,53 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Throws a TypeError if number.@@toPrimitive is not null, undefined, or callable +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + + ToPrimitive ( input [ , PreferredType ] ) + + [...] + 4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + + GetMethod (V, P) + + [...] + 2. Let func be ? GetV(V, P). + 3. If func is either undefined or null, return undefined. + 4. If IsCallable(func) is false, throw a TypeError exception. +features: [Symbol.toPrimitive] +---*/ + +var obj = {}; + +obj[Symbol.toPrimitive] = 42; +assert.throws(TypeError, function() { + isNaN(obj); +}, "number"); + +obj[Symbol.toPrimitive] = ""; +assert.throws(TypeError, function() { + isNaN(obj); +}, "string"); + +obj[Symbol.toPrimitive] = true; +assert.throws(TypeError, function() { + isNaN(obj); +}, "boolean"); + +obj[Symbol.toPrimitive] = Symbol.toPrimitive; +assert.throws(TypeError, function() { + isNaN(obj); +}, "symbol"); + +obj[Symbol.toPrimitive] = {}; +assert.throws(TypeError, function() { + isNaN(obj); +}, "object"); diff --git a/test/built-ins/isNaN/toprimitive-result-is-object-throws.js b/test/built-ins/isNaN/toprimitive-result-is-object-throws.js new file mode 100644 index 0000000000..7c0fecc7c3 --- /dev/null +++ b/test/built-ins/isNaN/toprimitive-result-is-object-throws.js @@ -0,0 +1,32 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Throws a TypeError if the result of calling number.@@toPrimitive is an Object +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + + ToPrimitive ( input [ , PreferredType ] ) + + [...] + 4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + 5. If exoticToPrim is not undefined, then + a. Let result be ? Call(exoticToPrim, input, « hint »). + b. If Type(result) is not Object, return result. + c. Throw a TypeError exception. +features: [Symbol.toPrimitive] +---*/ + +var obj = {}; +obj[Symbol.toPrimitive] = function() { + return [42]; +}; + +assert.throws(TypeError, function() { + isNaN(obj); +}); diff --git a/test/built-ins/isNaN/toprimitive-result-is-symbol-throws.js b/test/built-ins/isNaN/toprimitive-result-is-symbol-throws.js new file mode 100644 index 0000000000..b5320d5022 --- /dev/null +++ b/test/built-ins/isNaN/toprimitive-result-is-symbol-throws.js @@ -0,0 +1,36 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Throws a TypeError if the result of calling number.@@toPrimitive is a symbol +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + + ToNumber ( argument ) + + 1. Let primValue be ? ToPrimitive(argument, hint Number). + 2. Return ? ToNumber(primValue). + + ToPrimitive ( input [ , PreferredType ] ) + + [...] + 4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + 5. If exoticToPrim is not undefined, then + a. Let result be ? Call(exoticToPrim, input, « hint »). + b. If Type(result) is not Object, return result. +features: [Symbol.toPrimitive] +---*/ + +var obj = {}; +obj[Symbol.toPrimitive] = function() { + return Symbol.toPrimitive; +}; + +assert.throws(TypeError, function() { + isNaN(obj); +}); diff --git a/test/built-ins/isNaN/toprimitive-valid-result.js b/test/built-ins/isNaN/toprimitive-valid-result.js new file mode 100644 index 0000000000..b76b271287 --- /dev/null +++ b/test/built-ins/isNaN/toprimitive-valid-result.js @@ -0,0 +1,73 @@ +// Copyright (C) 2016 The V8 Project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-isnan-number +es6id: 18.2.3 +description: > + Use non-object value returned from @@toPrimitive method +info: | + isNaN (number) + + 1. Let num be ? ToNumber(number). + + ToPrimitive ( input [ , PreferredType ] ) + + [...] + 4. Let exoticToPrim be ? GetMethod(input, @@toPrimitive). + 5. If exoticToPrim is not undefined, then + a. Let result be ? Call(exoticToPrim, input, « hint »). + b. If Type(result) is not Object, return result. +features: [Symbol.toPrimitive] +---*/ + +var called = 0; +var obj = { + valueOf: function() { + called = NaN; + return Infinity; + }, + toString: function() { + called = NaN; + return Infinity; + } +}; + +obj[Symbol.toPrimitive] = function() { + called += 1; + return 42; +}; +assert.sameValue(isNaN(obj), false, "use returned value - non-NaN number"); +assert.sameValue(called, 1, "toPrimitive is called - non-NaN number"); + +called = 0; +obj[Symbol.toPrimitive] = function() { + called += 1; + return "this is not a number"; +}; +assert.sameValue(isNaN(obj), true, "use returned value - string to NaN"); +assert.sameValue(called, 1, "toPrimitive is called - string to NaN"); + +called = 0; +obj[Symbol.toPrimitive] = function() { + called += 1; + return true; +}; +assert.sameValue(isNaN(obj), false, "use returned value - boolean true"); +assert.sameValue(called, 1, "toPrimitive is called - boolean true"); + +called = 0; +obj[Symbol.toPrimitive] = function() { + called += 1; + return false; +}; +assert.sameValue(isNaN(obj), false, "use returned value - boolean false"); +assert.sameValue(called, 1, "toPrimitive is called - boolean false"); + +called = 0; +obj[Symbol.toPrimitive] = function() { + called += 1; + return NaN; +}; +assert.sameValue(isNaN(obj), true, "use returned value - NaN"); +assert.sameValue(called, 1, "toPrimitive is called - NaN");