Skip to content

Commit

Permalink
test for String.prototype.indexOf first parameter type coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Wolfe authored and rwaldron committed Sep 8, 2017
1 parent 0d9ef34 commit f83adad
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
61 changes: 58 additions & 3 deletions harness/typeCoercion.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,15 @@ function testCoercibleToPrimitiveWithMethod(hint, method, test) {
[methodNames[0]]: method,
[methodNames[1]]: function() { throw new Test262Error(); },
});
test({
[methodNames[1]]: method,
});
if (hint === "number") {
// The default valueOf returns an object, which is unsuitable.
// The default toString returns a String, which is suitable.
// Therefore this test only works for valueOf falling back to toString.
test({
// this is toString:
[methodNames[1]]: method,
});
}

// GetMethod: if func is undefined or null, return undefined.
test({
Expand Down Expand Up @@ -228,3 +234,52 @@ function testNotCoercibleToPrimitive(hint, test) {
testUnsuitableMethod(function() { return Object(1); });
testUnsuitableMethod(function() { return {}; });
}

function testCoercibleToString(test) {
function testPrimitiveValue(value, expectedString) {
test(value, expectedString);
// ToPrimitive
testPrimitiveWrappers(value, "string", function(value) {
test(value, expectedString);
});
}

testPrimitiveValue(undefined, "undefined");
testPrimitiveValue(null, "null");
testPrimitiveValue(true, "true");
testPrimitiveValue(false, "false");
testPrimitiveValue(0, "0");
testPrimitiveValue(-0, "0");
testPrimitiveValue(Infinity, "Infinity");
testPrimitiveValue(-Infinity, "-Infinity");
testPrimitiveValue(123.456, "123.456");
testPrimitiveValue(-123.456, "-123.456");
testPrimitiveValue("", "");
testPrimitiveValue("foo", "foo");

if (typeof BigInt !== "undefined") {
// BigInt -> TypeError
testPrimitiveValue(BigInt(0), "0");
}

// toString of a few objects
test([], "");
test(["foo", "bar"], "foo,bar");
test({}, "[object Object]");
}

function testNotCoercibleToString(test) {
function testPrimitiveValue(value) {
test(TypeError, value);
// ToPrimitive
testPrimitiveWrappers(value, "string", function(value) {
test(TypeError, value);
});
}

// Symbol -> TypeError
testPrimitiveValue(Symbol("1"));

// ToPrimitive
testNotCoercibleToPrimitive("string", test);
}
25 changes: 25 additions & 0 deletions test/built-ins/String/prototype/indexOf/searchstring-tostring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.indexof
description: String.prototype.indexOf type coercion for searchString parameter
info: >
String.prototype.indexOf ( searchString [ , position ] )
3. Let searchStr be ? ToString(searchString).
includes: [typeCoercion.js]
---*/

testCoercibleToString(function(value, expectedString) {
if (expectedString.length === 0) {
assert.sameValue(("x_x_x").indexOf(value), 0);
} else {
assert.sameValue(expectedString.indexOf("\x00"), -1, "sanity check");
assert.sameValue(("\x00\x00" + expectedString + "\x00\x00").indexOf(value), 2);
}
});

testNotCoercibleToString(function(error, value) {
assert.throws(error, function() { "".indexOf(value); });
});

0 comments on commit f83adad

Please sign in to comment.