Improve String#toString and String#valueOf coverage (#2160)

* toString: rename "length" test

* toString: use verifyProperty in "name" test

* toString: rename & merge non-generic tests

* toString: add cross-realm non-generic test

* toString: rename happy path tests

* valueOf: use verifyProperty in "length" test

* valueOf: use verifyProperty in "name" test

* valueOf: rename & merge non-generic tests

* valueOf: add cross-realm non-generic test

* valueOf: rename happy path tests

* toString: add indirect invocation tests

* valueOf: add indirect invocation tests
This commit is contained in:
Aleksey Shvayka 2019-05-23 00:47:15 +03:00 committed by Leo Balter
parent ee960aefb5
commit 4838fc3167
26 changed files with 368 additions and 351 deletions

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.toString() returns this string value
es5id: 15.5.4.2_A1_T1
description: Create new String(number) and check its method toString()
---*/
var __string__obj = new String(1);
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.toString() !== "" + 1) {
$ERROR('#1: __string__obj = new String(1); __string__obj.toString() === ""+1. Actual: __string__obj.toString() ===' + __string__obj.toString());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.toString() returns this string value
es5id: 15.5.4.2_A1_T2
description: Create new String(boolean) and check its method toString()
---*/
var __string__obj = new String(true);
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.toString() !== "" + true) {
$ERROR('#1: __string__obj = new String(true); __string__obj.toString() === ""+true. Actual: __string__obj.toString() ===' + __string__obj.toString());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.toString() returns this string value
es5id: 15.5.4.2_A1_T3
description: Create new String(string) and check its method toString()
---*/
var __string__obj = new String("metal");
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.toString() !== "metal") {
$ERROR('#1: __string__obj = new String("metal"); __string__obj.toString() === "metal". Actual: __string__obj.toString() ===' + __string__obj.toString());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.toString() returns this string value
es5id: 15.5.4.2_A1_T4
description: Create new String(function(){}()) and check its method toString()
---*/
var __string__obj = new String(function() {}());
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.toString() !== "undefined") {
$ERROR('#1: __string__obj = new String(function(){}()); __string__obj.toString() === "undefined". Actual: __string__obj.toString() ===' + __string__obj.toString());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,35 +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 toString function is not generic; it throws a TypeError exception if
its this value is not a String object. Therefore, it cannot be
transferred to other kinds of objects for use as a method
es5id: 15.5.4.2_A2_T1
description: Checking if creating variable String.prototype.toString fails
---*/
var __toString = String.prototype.toString;
//////////////////////////////////////////////////////////////////////////////
//CHECK#1
if (typeof __toString !== "function") {
$ERROR('#1: __toString = String.prototype.toString; typeof __toString === "function". Actual: typeof __toString ===' + typeof __toString);
}
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//CHECK#2
try {
var x = __toString();
$ERROR('#2: "__toString = String.prototype.toString; var x = __toString();" lead to throwing exception');
} catch (e) {
if (!(e instanceof TypeError)) {
$ERROR('#2.1: "__toString = String.prototype.toString; var x = __toString();" lead to throwing exception. Exception is instance of TypeError. Actual: exception is ' + e);
}
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,37 +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 toString function is not generic; it throws a TypeError exception if
its this value is not a String object. Therefore, it cannot be
transferred to other kinds of objects for use as a method
es5id: 15.5.4.2_A2_T2
description: Checking if creating the object String.prototype.toString fails
---*/
var __obj = {
toString: String.prototype.toString
};
//////////////////////////////////////////////////////////////////////////////
//CHECK#1
if (typeof __obj["toString"] !== "function") {
$ERROR('#1: var __obj={toString : String.prototype.toString}; typeof __obj["toString"] === "function". Actual: typeof __obj["toString"] ===' + typeof __obj["toString"]);
}
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//CHECK#2
try {
var x = (__obj == 1);
$ERROR('#2: "var x = (__obj == 1)" lead to throwing exception');
} catch (e) {
if (!(e instanceof TypeError)) {
$ERROR('#2.1: Exception is instance of TypeError. Actual: exception is ' + e);
}
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,30 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.toString() is equal String.prototype.valueOf()
es5id: 15.5.4.2_A3_T1
description: >
Create new String() with various arguments and compare returned
results of toString() and valueOf()
---*/
//CHECK#1
var str = new String();
if (!(str.valueOf() == str.toString()))
$ERROR('#1: str = new String(),str.valueOf() == str.toString()');
//CHECK#2
str = new String(true);
if (!(str.valueOf() == str.toString()))
$ERROR('#2: str = new String(true),str.valueOf() == str.toString()');
//CHECK#3
str = new String(false);
if (!(str.valueOf() == str.toString()))
$ERROR('#3: str = new String(false),str.valueOf() == str.toString()');
//CHECK#4
str = new String(Math.PI);
if (!(str.valueOf() == str.toString()))
$ERROR('#4: str = new String(Math.PI),str.valueOf() == str.toString()');

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.toString have length property and it is equal 0
es5id: 15.5.4.2_A4_T1
description: Checking String.prototype.toString.length property
---*/
//CHECK#1
if (String.prototype.toString.hasOwnProperty('length') !== true) {
$ERROR('#1: String.prototype.toString.hasOwnProperty(\'length\')===true. Actual: ' + String.prototype.toString.hasOwnProperty('length'));
}
else {
//CHECK#2
if (String.prototype.toString.length !== 0)
$ERROR('#2: String.prototype.toString.length===0. Actual: String.prototype.toString.length===' + String.prototype.toString.length);
}

View File

@ -0,0 +1,27 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tostring
description: >
String.prototype.toString.length is 0.
info: |
ECMAScript Standard Built-in Objects
...
Every built-in Function object, including constructors, has a length
property whose value is an integer. Unless otherwise specified, this value
is equal to the largest number of named arguments shown in the subclause
headings for the function description, including optional parameters.
...
Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
---*/
verifyProperty(String.prototype.toString, 'length', {
value: 0,
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -2,6 +2,7 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tostring
es6id: 21.1.3.23
description: >
String.prototype.toString.name is "toString".
@ -19,8 +20,9 @@ info: |
includes: [propertyHelper.js]
---*/
assert.sameValue(String.prototype.toString.name, "toString");
verifyNotEnumerable(String.prototype.toString, "name");
verifyNotWritable(String.prototype.toString, "name");
verifyConfigurable(String.prototype.toString, "name");
verifyProperty(String.prototype.toString, 'name', {
value: 'toString',
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,58 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tostring
description: >
Throws a TypeError if called on neither String primitive nor String object
(honoring the Realm of the current execution context)
info: |
String.prototype.toString ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
[...]
3. Throw a TypeError exception.
features: [cross-realm]
---*/
var other = $262.createRealm().global;
var otherToString = other.String.prototype.toString;
assert.throws(other.TypeError, function() {
otherToString.call(true);
});
assert.throws(other.TypeError, function() {
otherToString.call(0);
});
assert.throws(other.TypeError, function() {
otherToString.call(null);
});
assert.throws(other.TypeError, function() {
otherToString.call();
});
assert.throws(other.TypeError, function() {
otherToString.call(Symbol('desc'));
});
assert.throws(other.TypeError, function() {
otherToString.call({
valueOf: function() {
return 'str';
},
});
});
assert.throws(other.TypeError, function() {
otherToString.call([1]);
});
assert.throws(other.TypeError, function() {
'str'.concat({toString: otherToString});
});

View File

@ -0,0 +1,55 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tostring
description: >
Throws a TypeError if called on neither String primitive nor String object
info: |
String.prototype.toString ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
[...]
3. Throw a TypeError exception.
---*/
var toString = String.prototype.toString;
assert.throws(TypeError, function() {
toString.call(false);
});
assert.throws(TypeError, function() {
toString.call(1);
});
assert.throws(TypeError, function() {
toString.call(null);
});
assert.throws(TypeError, function() {
toString.call();
});
assert.throws(TypeError, function() {
toString.call(Symbol('desc'));
});
assert.throws(TypeError, function() {
toString.call({
toString: function() {
return 'str';
},
});
});
assert.throws(TypeError, function() {
toString.call(['s', 't', 'r']);
});
assert.throws(TypeError, function() {
''.concat({toString: toString});
});

View File

@ -0,0 +1,26 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tostring
description: >
If called on a String object, returns [[StringData]] slot
info: |
String.prototype.toString ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
[...]
2. If Type(value) is Object and value has a [[StringData]] internal slot, then
a. Let s be value.[[StringData]].
b. Assert: Type(s) is String.
c. Return s.
---*/
var toString = String.prototype.toString;
assert.sameValue(Object('str').toString(), 'str');
assert.sameValue(toString.call(new String('')), '');
assert.sameValue('a'.concat(Object('b')), 'ab');

View File

@ -0,0 +1,21 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.tostring
description: >
If called on String primitive, returns it
info: |
String.prototype.toString ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
1. If Type(value) is String, return value.
---*/
var toString = String.prototype.toString;
assert.sameValue(''.toString(), '');
assert.sameValue(toString.call('str'), 'str');

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.valueOf() returns this string value
es5id: 15.5.4.3_A1_T1
description: Create String object as new String(1) and check its valueOf()
---*/
var __string__obj = new String(1);
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.valueOf() !== "" + 1) {
$ERROR('#1: __string__obj = new String(1); __string__obj.valueOf() === ""+1. Actual: __string__obj.valueOf() ===' + __string__obj.valueOf());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.valueOf() returns this string value
es5id: 15.5.4.3_A1_T2
description: Create String object as new String(true) and check its valueOf()
---*/
var __string__obj = new String(true);
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.valueOf() !== "" + true) {
$ERROR('#1: __string__obj = new String(true); __string__obj.valueOf() === ""+true. Actual: __string__obj.valueOf() ===' + __string__obj.valueOf());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,18 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.valueOf() returns this string value
es5id: 15.5.4.3_A1_T3
description: Create String object as new String(string) and check its valueOf()
---*/
var __string__obj = new String("metal");
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.valueOf() !== "metal") {
$ERROR('#1: __string__obj = new String("metal"); __string__obj.valueOf() === "metal". Actual: __string__obj.valueOf() ===' + __string__obj.valueOf());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,20 +0,0 @@
// Copyright 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
info: String.prototype.valueOf() returns this string value
es5id: 15.5.4.3_A1_T4
description: >
Create String object as new String(function(){}()) and check its
valueOf()
---*/
var __string__obj = new String(function() {}());
//////////////////////////////////////////////////////////////////////////////
//CHECK#
if (__string__obj.valueOf() !== "undefined") {
$ERROR('#1: __string__obj = new String(function(){}()); __string__obj.valueOf() === "undefined". Actual: __string__obj.valueOf() ===' + __string__obj.valueOf());
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,34 +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 valueOf function is not generic; it throws a TypeError exception if its this value is not a String object.
Therefore, it cannot be transferred to other kinds of objects for use as a method
es5id: 15.5.4.3_A2_T1
description: Checking if creating variable String.prototype.valueOf fails
---*/
var __valueOf = String.prototype.valueOf;
//////////////////////////////////////////////////////////////////////////////
//CHECK#1
if (typeof __valueOf !== "function") {
$ERROR('#1: __valueOf = String.prototype.valueOf; typeof __valueOf === "function". Actual: typeof __valueOf ===' + typeof __valueOf);
}
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//CHECK#2
try {
var x = __valueOf();
$ERROR('#2: "__valueOf = String.prototype.valueOf; var x = __valueOf()" lead to throwing exception');
} catch (e) {
if (!(e instanceof TypeError)) {
$ERROR('#2.1: Exception is instance of TypeError. Actual: exception is ' + e);
}
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -1,36 +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 valueOf function is not generic; it throws a TypeError exception if its this value is not a String object.
Therefore, it cannot be transferred to other kinds of objects for use as a method
es5id: 15.5.4.3_A2_T2
description: Checking if creating the object String.prototype.valueOf fails
---*/
var __obj = {
valueOf: String.prototype.valueOf
};
//////////////////////////////////////////////////////////////////////////////
//CHECK#1
if (typeof __obj["valueOf"] !== "function") {
$ERROR('#1: var __obj={valueOf : String.prototype.valueOf}; typeof __obj["valueOf"] === "function". Actual: typeof __obj["valueOf"] ===' + typeof __obj["valueOf"]);
}
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//CHECK#2
try {
var x = (__obj == 1);
$ERROR('#2: "var __obj={valueOf : String.prototype.valueOf}; var x = (__obj == 1)" lead to throwing exception');
} catch (e) {
if (!(e instanceof TypeError)) {
$ERROR('#2.1: Exception is instance of TypeError. Actual: exception is ' + e);
}
}
//
//////////////////////////////////////////////////////////////////////////////

View File

@ -2,6 +2,7 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.valueof
es6id: 21.1.3.26
description: >
String.prototype.valueOf.length is 0.
@ -22,8 +23,9 @@ info: |
includes: [propertyHelper.js]
---*/
assert.sameValue(String.prototype.valueOf.length, 0);
verifyNotEnumerable(String.prototype.valueOf, "length");
verifyNotWritable(String.prototype.valueOf, "length");
verifyConfigurable(String.prototype.valueOf, "length");
verifyProperty(String.prototype.valueOf, 'length', {
value: 0,
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -2,6 +2,7 @@
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.valueof
es6id: 21.1.3.26
description: >
String.prototype.valueOf.name is "valueOf".
@ -19,8 +20,9 @@ info: |
includes: [propertyHelper.js]
---*/
assert.sameValue(String.prototype.valueOf.name, "valueOf");
verifyNotEnumerable(String.prototype.valueOf, "name");
verifyNotWritable(String.prototype.valueOf, "name");
verifyConfigurable(String.prototype.valueOf, "name");
verifyProperty(String.prototype.valueOf, 'name', {
value: 'valueOf',
writable: false,
enumerable: false,
configurable: true,
});

View File

@ -0,0 +1,58 @@
// Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.valueof
description: >
Throws a TypeError if called on neither String primitive nor String object
(honoring the Realm of the current execution context)
info: |
String.prototype.valueOf ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
[...]
3. Throw a TypeError exception.
features: [cross-realm]
---*/
var other = $262.createRealm().global;
var otherValueOf = other.String.prototype.valueOf;
assert.throws(other.TypeError, function() {
otherValueOf.call(false);
});
assert.throws(other.TypeError, function() {
otherValueOf.call(-1);
});
assert.throws(other.TypeError, function() {
otherValueOf.call(null);
});
assert.throws(other.TypeError, function() {
otherValueOf.call();
});
assert.throws(other.TypeError, function() {
otherValueOf.call(Symbol('desc'));
});
assert.throws(other.TypeError, function() {
otherValueOf.call({
valueOf: function() {
return '';
},
});
});
assert.throws(other.TypeError, function() {
otherValueOf.call([3]);
});
assert.throws(other.TypeError, function() {
'' + {valueOf: otherValueOf};
});

View File

@ -0,0 +1,55 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.valueof
description: >
Throws a TypeError if called on neither String primitive nor String object
info: |
String.prototype.valueOf ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
[...]
3. Throw a TypeError exception.
---*/
var valueOf = String.prototype.valueOf;
assert.throws(TypeError, function() {
valueOf.call(true);
});
assert.throws(TypeError, function() {
valueOf.call(-0);
});
assert.throws(TypeError, function() {
valueOf.call(null);
});
assert.throws(TypeError, function() {
valueOf.call();
});
assert.throws(TypeError, function() {
valueOf.call(Symbol('desc'));
});
assert.throws(TypeError, function() {
valueOf.call({
toString: function() {
return 'str';
},
});
});
assert.throws(TypeError, function() {
valueOf.call(['s', 't', 'r']);
});
assert.throws(TypeError, function() {
'str' + {valueOf: valueOf};
});

View File

@ -0,0 +1,26 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.valueof
description: >
If called on a String object, returns [[StringData]] slot
info: |
String.prototype.valueOf ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
[...]
2. If Type(value) is Object and value has a [[StringData]] internal slot, then
a. Let s be value.[[StringData]].
b. Assert: Type(s) is String.
c. Return s.
---*/
var valueOf = String.prototype.valueOf;
assert.sameValue(Object('').valueOf(), '');
assert.sameValue(valueOf.call(new String('str')), 'str');
assert.sameValue('a' + new String('b'), 'ab');

View File

@ -0,0 +1,21 @@
// Copyright (C) 2009 the Sputnik authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-string.prototype.valueof
description: >
If called on String primitive, returns it
info: |
String.prototype.valueOf ( )
1. Return ? thisStringValue(this value).
thisStringValue ( value )
1. If Type(value) is String, return value.
---*/
var valueOf = String.prototype.valueOf;
assert.sameValue('str'.valueOf(), 'str');
assert.sameValue(valueOf.call(''), '');