Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.prototype.{ trimStart, trimEnd, trimLeft, trimRight } tests #1246

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ regexp-unicode-property-escapes
Atomics
SharedArrayBuffer

# String Trimming
# https://github.com/tc39/proposal-string-left-right-trim
# Includes all tests for:
# String.prototype.{trimStart, trimEnd, trimLeft, trimRight }
string-trimming

# Array.prototype.flatten and Array.prototype.flatMap
# https://github.com/tc39/proposal-flatMap
Array.prototype.flatten
Array.prototype.flatMap


# Standard language features
#
# Language features that have been included in a published version of the
Expand Down
31 changes: 31 additions & 0 deletions test/annexB/built-ins/String/prototype/trimLeft/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimLeft
description: >
String.prototype.trimLeft.length is 0.
info: >
String.prototype.trimLeft ( )

17 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. However, rest parameters shown using the form “...name”
are not included in the default argument count.

Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

features: [string-trimming] (and all files in this PR)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add string-trimming to FEATURES.txt

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add it in the master branch yet. This is still a stage 2 feature. This should be added to this same branch.

features: [string-trimming]
---*/

verifyProperty(String.prototype.trimLeft, "length", {
value: 0,
enumerable: false,
writable: false,
configurable: true,
});
28 changes: 28 additions & 0 deletions test/annexB/built-ins/String/prototype/trimLeft/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimLeft
description: >
String.prototype.trimLeft.name is "trimLeft".
info: >
String.prototype.trimLeft ( )

17 ECMAScript Standard Built-in Objects:
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value
is a String.

Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype.trimLeft, "name", {
value: "trimLeft",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct? According to the proposal’s README the name should be trimStart, although I agree the currently proposed spec text doesn’t make that happen. +@sebmarkbage @evilpie @ljharb

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be trimStart. Nice catch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests in test/built-ins/String/prototype/* are trimEnd and trimStart, these are annex B

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is irrelevant.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the confusing part of how this is speced. The property name is what is defined in annex B, but even in annex B, this is the same function instance as trimStart so it has the same functionInstance.name field by definition.

Note that it otherwise gets impossible to pass both these name tests and the same value equality test:

https://github.com/tc39/test262/pull/1246/files#diff-c045566571ceee76729db55485a41779

You can't have the same value with two different names.

Copy link
Contributor

@rwaldron rwaldron Jan 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me a second to see what I misunderstood. I'll fix this locally before pushing to master

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function object that is the initial value of String.prototype.trimLeft is the same function object that is the initial value of String.prototype.trimStart.

Same as the function in Array.prototype[Symbol.iterator] is named values, trimLeft.name is "trimStart".

thanks for catching this.

enumerable: false,
writable: false,
configurable: true,
});
22 changes: 22 additions & 0 deletions test/annexB/built-ins/String/prototype/trimLeft/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2017 The Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimLeft
description: >
"trimLeft" property of String.prototype
info: >
17 ECMAScript Standard Built-in Objects:

Every other data property described in clauses 18 through 26 and in Annex B.2
has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype, "trimLeft", {
enumerable: false,
writable: true,
configurable: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimLeft
description: >
String.prototype.trimLeft is a reference to String.prototype.trimStart.
info: >
String.prototype.trimLeft ( )

The function object that is the initial value of String.prototype.trimLeft
is the same function object that is the initial value of
String.prototype.trimStart.
features: [string-trimming]
---*/

assert.sameValue(String.prototype.trimLeft, String.prototype.trimStart);
31 changes: 31 additions & 0 deletions test/annexB/built-ins/String/prototype/trimRight/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimRight
description: >
String.prototype.trimRight.length is 0.
info: >
String.prototype.trimRight ( )

17 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. However, rest parameters shown using the form “...name”
are not included in the default argument count.

Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

features: [String.prototype.trimRight] (here and all trimRight test files)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use features: [string-trimming] for consistency

features: [string-trimming]
---*/

verifyProperty(String.prototype.trimRight, "length", {
value: 0,
enumerable: false,
writable: false,
configurable: true,
});
28 changes: 28 additions & 0 deletions test/annexB/built-ins/String/prototype/trimRight/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimRight
description: >
String.prototype.trimRight.name is "trimRight".
info: >
String.prototype.trimRight ( )

17 ECMAScript Standard Built-in Objects:
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value
is a String.

Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype.trimRight, "name", {
value: "trimRight",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is annex B ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, trimLeft and trimRight are Annex B features, but the tests should still match the specified behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I see the oversight now, thanks for your patience :)

enumerable: false,
writable: false,
configurable: true,
});
22 changes: 22 additions & 0 deletions test/annexB/built-ins/String/prototype/trimRight/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2017 The Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimRight
description: >
"trimRight" property of String.prototype
info: >
17 ECMAScript Standard Built-in Objects:

Every other data property described in clauses 18 through 26 and in Annex B.2
has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype, "trimRight", {
enumerable: false,
writable: true,
configurable: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimRight
description: >
String.prototype.trimRight is a reference to String.prototype.trimEnd.
info: >
String.prototype.trimRight ( )

The function object that is the initial value of String.prototype.trimRight
is the same function object that is the initial value of
String.prototype.trimEnd.
features: [string-trimming]
---*/

assert.sameValue(String.prototype.trimRight, String.prototype.trimEnd);
31 changes: 31 additions & 0 deletions test/built-ins/String/prototype/trimEnd/length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimEnd
description: >
String.prototype.trimEnd.length is 0.
info: >
String.prototype.trimEnd ( )

17 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. However, rest parameters shown using the form “...name”
are not included in the default argument count.

Unless otherwise specified, the length property of a built-in Function
object has the attributes { [[Writable]]: false, [[Enumerable]]: false,
[[Configurable]]: true }.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype.trimEnd, "length", {
value: 0,
enumerable: false,
writable: false,
configurable: true,
});
28 changes: 28 additions & 0 deletions test/built-ins/String/prototype/trimEnd/name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimEnd
description: >
String.prototype.trimEnd.name is "trimEnd".
info: >
String.prototype.trimEnd ( )

17 ECMAScript Standard Built-in Objects:
Every built-in Function object, including constructors, that is not
identified as an anonymous function has a name property whose value
is a String.

Unless otherwise specified, the name property of a built-in Function
object, if it exists, has the attributes { [[Writable]]: false,
[[Enumerable]]: false, [[Configurable]]: true }.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype.trimEnd, "name", {
value: "trimEnd",
enumerable: false,
writable: false,
configurable: true,
});
22 changes: 22 additions & 0 deletions test/built-ins/String/prototype/trimEnd/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2017 The Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimEnd
description: >
"trimEnd" property of String.prototype
info: >
17 ECMAScript Standard Built-in Objects:

Every other data property described in clauses 18 through 26 and in Annex B.2
has the attributes { [[Writable]]: true, [[Enumerable]]: false,
[[Configurable]]: true } unless otherwise specified.
includes: [propertyHelper.js]
features: [string-trimming]
---*/

verifyProperty(String.prototype, "trimEnd", {
enumerable: false,
writable: true,
configurable: true,
});
31 changes: 31 additions & 0 deletions test/built-ins/String/prototype/trimEnd/this-value-boolean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimEnd
description: Behavior when "this" value is a boolean.
info: |
Runtime Semantics: TrimString ( string, where )
2. Let S be ? ToString(str).

ToString ( argument )
Argument Type: Boolean
Result:
If argument is true, return "true".
If argument is false, return "false".
features: [string-trimming]
---*/

var trimEnd = String.prototype.trimEnd

assert.sameValue(
trimEnd.call(true),
'true',
'String.prototype.trimEnd.call(true)'
);

assert.sameValue(
String.prototype.trimEnd.call(false),
'false',
'String.prototype.trimEnd.call(false)'
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimEnd
description: TrimEnd removes all line terminators from the end of a string.
info: |
Runtime Symantics: TrimString ( string, where )
...
4. Else if where is "end", let T be a String value that is a copy of S with
trailing white space removed.
...

The definition of white space is the union of WhiteSpace and LineTerminator.

features: [string-trimming]
---*/

var trimEnd = String.prototype.trimEnd;

// A string of all valid LineTerminator Unicode code points
var lt = '\u000A\u000D\u2028\u2029';

var str = lt + 'a' + lt + 'b' + lt;
var expected = lt + 'a' + lt + 'b';

assert.sameValue(
trimEnd.call(str),
expected,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2017 Valerie Young. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-string.prototype.trimEnd
description: The "this" value must be object-coercible
info: |
1. Let O be ? RequireObjectCoercible(this value).
features: [string-trimming]
---*/

var trimEnd = String.prototype.trimEnd;

assert.sameValue(typeof trimEnd, 'function');

assert.throws(TypeError, function() {
trimEnd.call(undefined);
}, 'undefined');

assert.throws(TypeError, function() {
trimEnd.call(null);
}, 'null');
Loading