Skip to content

Commit

Permalink
Merge pull request #352 from bocoup/es6-regexp-u
Browse files Browse the repository at this point in the history
Add tests for RegExp `u` flag
  • Loading branch information
bterlson committed Jul 24, 2015
2 parents f673136 + 9068608 commit bdd84fb
Show file tree
Hide file tree
Showing 29 changed files with 793 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/built-ins/RegExp/prototype/Symbol.match/get-unicode-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
Errors thrown by `unicode` accessor are forwarded to the runtime for global patterns
es6id: 21.2.5.6
info: >
21.2.5.6 RegExp.prototype [ @@match ] ( string )
[...]
8. Else global is true,
a. Let fullUnicode be ToBoolean(Get(rx, "unicode")).
b. ReturnIfAbrupt(fullUnicode).
features: [Symbol.match]
---*/

var nonGlobalRe = /./;
var globalRe = /./g;
var accessor = function() {
throw new Test262Error();
};
Object.defineProperty(nonGlobalRe, 'unicode', {
get: accessor
});
Object.defineProperty(globalRe, 'unicode', {
get: accessor
});

nonGlobalRe[Symbol.match]('');

assert.throws(Test262Error, function() {
globalRe[Symbol.match]('');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: lastIndex is advanced according to width of astral symbols
es6id: 21.2.5.6
info: >
21.2.5.6 RegExp.prototype [ @@match ] ( string )
[...]
8. Else global is true,
a. Let fullUnicode be ToBoolean(Get(rx, "unicode")).
[...]
g. Repeat,
[...]
iv. Else result is not null,
[...]
5. If matchStr is the empty String, then
[...]
c. Let nextIndex be AdvanceStringIndex(S, thisIndex,
fullUnicode).
d. Let setStatus be Set(rx, "lastIndex", nextIndex, true).
features: [Symbol.match]
---*/

var match = /^|\udf06/ug[Symbol.match]('\ud834\udf06');

assert(match !== null);
assert.sameValue(match.length, 1);
assert.sameValue(match[0], '');
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
Errors thrown by `unicode` accessor are forwarded to the runtime for global patterns
es6id: 21.2.5.8
info: >
21.2.5.8 RegExp.prototype [ @@replace ] ( string, replaceValue )
[...]
10. If global is true, then
a. Let fullUnicode be ToBoolean(Get(rx, "unicode")).
b. ReturnIfAbrupt(fullUnicode).
features: [Symbol.replace]
---*/

var nonGlobalRe = /./;
var globalRe = /./g;
var accessor = function() {
throw new Test262Error();
};
Object.defineProperty(nonGlobalRe, 'unicode', {
get: accessor
});
Object.defineProperty(globalRe, 'unicode', {
get: accessor
});

nonGlobalRe[Symbol.replace]('', '');

assert.throws(Test262Error, function() {
globalRe[Symbol.replace]('', '');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: lastIndex is advanced according to width of astral symbols
es6id: 21.2.5.8
info: >
21.2.5.8 RegExp.prototype [ @@replace ] ( string, replaceValue )
[...]
10. If global is true, then
a. Let fullUnicode be ToBoolean(Get(rx, "unicode")).
b. ReturnIfAbrupt(fullUnicode).
[...]
13. Repeat, while done is false
[...]
d. Else result is not null,
[...]
iii. Else,
[...]
3. If matchStr is the empty String, then
[...]
c. Let nextIndex be AdvanceStringIndex(S, thisIndex,
fullUnicode).
d. Let setStatus be Set(rx, "lastIndex", nextIndex, true).
features: [Symbol.replace]
---*/

var str = /^|\udf06/ug[Symbol.replace]('\ud834\udf06', 'XXX');
assert.sameValue(str, 'XXX\ud834\udf06');
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Advancement of lastIndex
es6id: 21.2.5.9
info: >
21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S )
[...]
12. Let flags be the value of R’s [[OriginalFlags]] internal slot.
13. If flags contains "u", let fullUnicode be true, else let fullUnicode be
false.
[...]
15. Repeat, while matchSucceeded is false
[...]
c. If r is failure, then
[...]
ii. Let lastIndex be AdvanceStringIndex(S, lastIndex, fullUnicode).
features: [Symbol.search]
---*/

assert.sameValue(/\udf06/u[Symbol.search]('\ud834\udf06'), -1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: >
lastIndex is advanced according to width of astral symbols following match failure
es6id: 21.2.5.11
info: >
21.2.5.11 RegExp.prototype [ @@split ] ( string, limit )
[...]
7. Let flags be ToString(Get(rx, "flags")).
8. ReturnIfAbrupt(flags).
9. If flags contains "u", let unicodeMatching be true.
10. Else, let unicodeMatching be false.
[...]
24. Repeat, while q < size
a. Let setStatus be Set(splitter, "lastIndex", q, true).
b. ReturnIfAbrupt(setStatus).
c. Let z be RegExpExec(splitter, S).
d. ReturnIfAbrupt(z).
e. If z is null, let q be AdvanceStringIndex(S, q, unicodeMatching).
features: [Symbol.split]
---*/

var result = /\udf06/u[Symbol.split]('\ud834\udf06');

assert.sameValue(result.length, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description:
lastIndex is advanced according to width of astral symbols following match success
es6id: 21.2.5.11
info: >
21.2.5.11 RegExp.prototype [ @@split ] ( string, limit )
[...]
7. Let flags be ToString(Get(rx, "flags")).
8. ReturnIfAbrupt(flags).
9. If flags contains "u", let unicodeMatching be true.
10. Else, let unicodeMatching be false.
[...]
24. Repeat, while q < size
a. Let setStatus be Set(splitter, "lastIndex", q, true).
b. ReturnIfAbrupt(setStatus).
c. Let z be RegExpExec(splitter, S).
d. ReturnIfAbrupt(z).
e. If z is null, let q be AdvanceStringIndex(S, q, unicodeMatching).
f. Else z is not null,
i. Let e be ToLength(Get(splitter, "lastIndex")).
ii. ReturnIfAbrupt(e).
iii. If e = p, let q be AdvanceStringIndex(S, q, unicodeMatching).
features: [Symbol.split]
---*/

var result = /./u[Symbol.split]('\ud834\udf06');

assert.sameValue(result.length, 2);
assert.sameValue(result[0], '');
assert.sameValue(result[1], '');
33 changes: 33 additions & 0 deletions test/built-ins/RegExp/prototype/exec/u-captured-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Encoding of `capturedValue`
es6id: 21.2.5.2.2
info: >
21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S )
[...]
12. Let flags be the value of R’s [[OriginalFlags]] internal slot.
13. If flags contains "u", let fullUnicode be true, else let fullUnicode be
false.
[...]
28. For each integer i such that i > 0 and i ≤ n
a. Let captureI be ith element of r's captures List.
b. If captureI is undefined, let capturedValue be undefined.
c. Else if fullUnicode is true,
i. Assert: captureI is a List of code points.
ii. Let capturedValue be a string whose code units are the
UTF16Encoding (10.1.1) of the code points of captureI.
[...]
e. Perform CreateDataProperty(A, ToString(i) , capturedValue).
29. Return A.
---*/

var match = /./u.exec('𝌆');

assert(match !== null);
assert.sameValue(match.length, 1);
assert.sameValue(match[0].length, 2);
assert.sameValue(match[0][0], '\ud834');
assert.sameValue(match[0][1], '\udf06');
22 changes: 22 additions & 0 deletions test/built-ins/RegExp/prototype/exec/u-lastindex-adv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Advancement of lastIndex
es6id: 21.2.5.2.2
info: >
21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S )
[...]
12. Let flags be the value of R’s [[OriginalFlags]] internal slot.
13. If flags contains "u", let fullUnicode be true, else let fullUnicode be
false.
[...]
15. Repeat, while matchSucceeded is false
[...]
c. If r is failure, then
[...]
ii. Let lastIndex be AdvanceStringIndex(S, lastIndex, fullUnicode).
---*/

assert.sameValue(/\udf06/u.exec('\ud834\udf06'), null);
30 changes: 30 additions & 0 deletions test/built-ins/RegExp/prototype/exec/u-lastindex-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Definition of `lastIndex` property value
es6id: 21.2.5.2.2
info: >
21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S )
[...]
12. Let flags be the value of R’s [[OriginalFlags]] internal slot.
13. If flags contains "u", let fullUnicode be true, else let fullUnicode be
false.
[...]
16. Let e be r's endIndex value.
17. If fullUnicode is true, then
a. e is an index into the Input character list, derived from S, matched
by matcher. Let eUTF be the smallest index into S that corresponds
to the character at element e of Input. If e is greater than or
equal to the length of Input, then eUTF is the number of code units
in S.
b. Let e be eUTF.
18. If global is true or sticky is true,
a. Let setStatus be Set(R, "lastIndex", e, true).
b. ReturnIfAbrupt(setStatus).
---*/

var r = /./ug;
r.exec('𝌆');
assert.sameValue(r.lastIndex, 2);
25 changes: 25 additions & 0 deletions test/built-ins/RegExp/prototype/flags/u-attr-err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Errors thrown when retrieving attribute
es6id: 21.2.5.3
info: >
21.2.5.3 get RegExp.prototype.flags
[...]
13. Let unicode be ToBoolean(Get(R, "unicode")).
14. ReturnIfAbrupt(unicode).
---*/

var re = /./;

Object.defineProperty(re, 'unicode', {
get: function() {
throw new Test262Error();
}
});

assert.throws(Test262Error, function() {
re.flags;
});
55 changes: 55 additions & 0 deletions test/built-ins/RegExp/prototype/flags/u-coercion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
description: Boolean coercion of `unicode` property
es6id: 21.2.5.3
info: >
21.2.5.3 get RegExp.prototype.flags
[...]
13. Let unicode be ToBoolean(Get(R, "unicode")).
14. ReturnIfAbrupt(unicode).
15. If unicode is true, append "u" as the last code unit of result.
features: [Symbol]
---*/

var r = /./;
var flags;
Object.defineProperty(r, 'unicode', { writable: true });

r.unicode = undefined;
flags = r.flags;
assert.sameValue(flags.length, 0);

r.unicode = null;
flags = r.flags;
assert.sameValue(flags.length, 0);

r.unicode = NaN;
flags = r.flags;
assert.sameValue(flags.length, 0);

r.unicode = 86;
flags = r.flags;
assert.sameValue(flags.length, 1);
assert.sameValue(flags[0], 'u');

r.unicode = '';
flags = r.flags;
assert.sameValue(flags.length, 0);

r.unicode = 'string';
flags = r.flags;
assert.sameValue(flags.length, 1);
assert.sameValue(flags[0], 'u');

r.unicode = Symbol();
flags = r.flags;
assert.sameValue(flags.length, 1);
assert.sameValue(flags[0], 'u');

r.unicode = {};
flags = r.flags;
assert.sameValue(flags.length, 1);
assert.sameValue(flags[0], 'u');
Loading

0 comments on commit bdd84fb

Please sign in to comment.