mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 11:03:19 +00:00
Migrated test block 28 to Dart 2.0.
Relatively simple block. Split string_replace_test into string_replace_test and string_replace_static test and updated status file to expect MissingCompileTimeError for vm + dart2js BUG= R=jcollins@google.com, rnystrom@google.com Review-Url: https://codereview.chromium.org/2983273002 .
This commit is contained in:
parent
e3c418a3db
commit
370938c4c2
|
@ -5,14 +5,17 @@
|
|||
# All static_tests have expected compile-time errors.
|
||||
[ $strong && $compiler != dart2analyzer && $compiler != dartdevc ]
|
||||
core_runtime_types_static_test: MissingCompileTimeError
|
||||
string_replace_static_test: MissingCompileTimeError
|
||||
string_static_test: MissingCompileTimeError
|
||||
|
||||
[ !$strong && $compiler != dartdevc && $checked ]
|
||||
core_runtime_types_static_test: MissingCompileTimeError
|
||||
string_replace_static_test: MissingCompileTimeError
|
||||
string_static_test: MissingCompileTimeError
|
||||
|
||||
[ !$strong && !$checked ]
|
||||
core_runtime_types_static_test: MissingCompileTimeError
|
||||
string_replace_static_test: MissingCompileTimeError
|
||||
string_static_test: MissingCompileTimeError
|
||||
|
||||
[ $compiler == dart2analyzer && !$strong ]
|
||||
|
@ -35,6 +38,14 @@ const_list_literal_test: RuntimeError # Issue 29921
|
|||
const_list_remove_range_test: RuntimeError # Issue 29921
|
||||
const_list_set_range_test: RuntimeError # Issue 29921
|
||||
compare_to2_test: RuntimeError # Issue 30170
|
||||
string_operations_with_null_test: RuntimeError # Issue 29921
|
||||
string_replace_test: RuntimeError # Issue 29921
|
||||
symbol_operator_test: RuntimeError # Issue 29921
|
||||
symbol_operator_test/03: RuntimeError # Issue 29921
|
||||
symbol_test/none: RuntimeError # Issue 29921
|
||||
symbol_reserved_word_test/06: RuntimeError # Issue 29921
|
||||
symbol_reserved_word_test/09: RuntimeError # Issue 29921
|
||||
symbol_reserved_word_test/12: RuntimeError # Issue 29921
|
||||
|
||||
[ ($compiler == dart2js || $compiler == dartdevc) && $runtime != none ]
|
||||
big_integer_arith_vm_test: RuntimeError # Issues 10245, 30170
|
||||
|
@ -104,7 +115,7 @@ bool_from_environment_test: Skip
|
|||
unicode_test: Fail
|
||||
|
||||
[ ($runtime == vm || $runtime == dart_precompiled) && $arch == simarmv5te ]
|
||||
big_integer_parsed_mul_div_vm_test: Pass, Slow
|
||||
big_integer_parsed_mul_div_vm_test: Pass, SloW
|
||||
|
||||
[ $compiler == precompiler ]
|
||||
big_integer_parsed_mul_div_vm_test: Pass, Timeout # --no_intrinsify
|
||||
|
|
26
tests/corelib_2/string_replace_static_test.dart
Normal file
26
tests/corelib_2/string_replace_static_test.dart
Normal file
|
@ -0,0 +1,26 @@
|
|||
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
void main() {
|
||||
// Test object startIndex
|
||||
"hello".replaceFirst("h", "X", new Object()); /*@compile-error=unspecified*/
|
||||
|
||||
// Test object startIndex
|
||||
"hello".replaceFirstMapped(
|
||||
"h", (_) => "X", new Object()); /*@compile-error=unspecified*/
|
||||
|
||||
"foo-bar".replaceFirstMapped("bar", (v) {
|
||||
return 42;
|
||||
}); /*@compile-error=unspecified*/
|
||||
|
||||
"hello".replaceRange(0, 0, 42); /*@compile-error=unspecified*/
|
||||
"hello".replaceRange(0, 0, ["x"]); /*@compile-error=unspecified*/
|
||||
}
|
||||
|
||||
// Fails to return a String on toString, throws if converted by "$naughty".
|
||||
class Naughty {
|
||||
toString() => this; /*@compile-error=unspecified*/
|
||||
}
|
|
@ -78,11 +78,7 @@ main() {
|
|||
Expect.throws(
|
||||
() => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError);
|
||||
|
||||
// Test object startIndex
|
||||
Expect.throws(() => "hello".replaceFirst("h", "X", new Object()));
|
||||
|
||||
// Test replaceFirstMapped.
|
||||
|
||||
Expect.equals(
|
||||
"AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
|
@ -160,10 +156,6 @@ main() {
|
|||
Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", null),
|
||||
(e) => e is ArgumentError);
|
||||
|
||||
// Test object startIndex
|
||||
Expect
|
||||
.throws(() => "hello".replaceFirstMapped("h", (_) => "X", new Object()));
|
||||
|
||||
// Test replacement depending on argument.
|
||||
Expect.equals("foo-BAR-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase()));
|
||||
|
@ -186,18 +178,6 @@ main() {
|
|||
return o;
|
||||
}));
|
||||
|
||||
Expect.equals(
|
||||
"foo-42",
|
||||
"foo-bar".replaceFirstMapped("bar", (v) {
|
||||
return 42;
|
||||
}));
|
||||
|
||||
// Test replacement returning object throwing on string-conversion.
|
||||
var n = new Naughty();
|
||||
Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) {
|
||||
return n;
|
||||
}));
|
||||
|
||||
for (var string in ["", "x", "foo", "x\u2000z"]) {
|
||||
for (var replacement in ["", "foo", string]) {
|
||||
for (int start = 0; start <= string.length; start++) {
|
||||
|
@ -214,14 +194,7 @@ main() {
|
|||
}
|
||||
}
|
||||
Expect.throws(() => string.replaceRange(0, 0, null));
|
||||
Expect.throws(() => string.replaceRange(0, 0, 42));
|
||||
Expect.throws(() => string.replaceRange(0, 0, ["x"]));
|
||||
Expect.throws(() => string.replaceRange(-1, 0, "x"));
|
||||
Expect.throws(() => string.replaceRange(0, string.length + 1, "x"));
|
||||
}
|
||||
}
|
||||
|
||||
// Fails to return a String on toString, throws if converted by "$naughty".
|
||||
class Naughty {
|
||||
toString() => this;
|
||||
}
|
|
@ -1,292 +0,0 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
import "dart:typed_data";
|
||||
|
||||
main() {
|
||||
iter(count, [values]) => values is List
|
||||
? new Iterable.generate(count, (x) => values[x])
|
||||
: new Iterable.generate(count, (x) => values);
|
||||
test(expect, iter, [start = 0, end]) {
|
||||
var actual = new String.fromCharCodes(iter, start, end);
|
||||
Expect.equals(expect, actual);
|
||||
}
|
||||
|
||||
testThrows(iterable, [start = 0, end]) {
|
||||
Expect.throws(() {
|
||||
new String.fromCharCodes(iterable, start, end);
|
||||
});
|
||||
}
|
||||
|
||||
test("", iter(0));
|
||||
test("", []);
|
||||
test("", const []);
|
||||
test("", new List(0));
|
||||
test("", new Uint8List(0));
|
||||
test("", new Uint16List(0));
|
||||
test("", new Uint32List(0));
|
||||
test("", "".codeUnits);
|
||||
|
||||
test("\x00", iter(1, 0));
|
||||
test("\x00", [0]);
|
||||
test("\x00", const [0]);
|
||||
test("\x00", new List(1)..[0] = 0);
|
||||
test("\x00", new Uint8List(1));
|
||||
test("\x00", new Uint16List(1));
|
||||
test("\x00", new Uint32List(1));
|
||||
test("\x00", "\x00".codeUnits);
|
||||
|
||||
test("\xff", iter(1, 255));
|
||||
test("\xFF", [255]);
|
||||
test("\xFF", const [255]);
|
||||
test("\xFF", new List(1)..[0] = 255);
|
||||
test("\xFF", new Uint8List(1)..[0] = 255);
|
||||
test("\xFF", new Uint16List(1)..[0] = 255);
|
||||
test("\xFF", new Uint32List(1)..[0] = 255);
|
||||
test("\xFF", "\xFF".codeUnits);
|
||||
|
||||
test("\u0100", iter(1, 256));
|
||||
test("\u0100", [256]);
|
||||
test("\u0100", const [256]);
|
||||
test("\u0100", new List(1)..[0] = 256);
|
||||
test("\u0100", new Uint16List(1)..[0] = 256);
|
||||
test("\u0100", new Uint32List(1)..[0] = 256);
|
||||
test("\u0100", "\u0100".codeUnits);
|
||||
|
||||
test("\uffff", iter(1, 65535));
|
||||
test("\uffff", [65535]);
|
||||
test("\uffff", const [65535]);
|
||||
test("\uffff", new List(1)..[0] = 65535);
|
||||
test("\uffff", new Uint16List(1)..[0] = 65535);
|
||||
test("\uffff", new Uint32List(1)..[0] = 65535);
|
||||
test("\uffff", "\uffff".codeUnits);
|
||||
|
||||
test("\u{10000}", iter(1, 65536));
|
||||
test("\u{10000}", [65536]);
|
||||
test("\u{10000}", const [65536]);
|
||||
test("\u{10000}", new List(1)..[0] = 65536);
|
||||
test("\u{10000}", new Uint32List(1)..[0] = 65536);
|
||||
test("\u{10000}", "\u{10000}".codeUnits);
|
||||
|
||||
test("\u{10FFFF}", iter(1, 0x10FFFF));
|
||||
test("\u{10FFFF}", [0x10FFFF]);
|
||||
test("\u{10FFFF}", const [0x10FFFF]);
|
||||
test("\u{10FFFF}", new List(1)..[0] = 0x10FFFF);
|
||||
test("\u{10FFFF}", new Uint32List(1)..[0] = 0x10FFFF);
|
||||
|
||||
test("\u{10ffff}", iter(2, [0xDBFF, 0xDFFF]));
|
||||
test("\u{10ffff}", [0xDBFF, 0xDFFF]);
|
||||
test("\u{10ffff}", const [0xDBFF, 0xDFFF]);
|
||||
test(
|
||||
"\u{10ffff}",
|
||||
new List(2)
|
||||
..[0] = 0xDBFF
|
||||
..[1] = 0xDFFF);
|
||||
test(
|
||||
"\u{10ffff}",
|
||||
new Uint16List(2)
|
||||
..[0] = 0xDBFF
|
||||
..[1] = 0xDFFF);
|
||||
test(
|
||||
"\u{10ffff}",
|
||||
new Uint32List(2)
|
||||
..[0] = 0xDBFF
|
||||
..[1] = 0xDFFF);
|
||||
test("\u{10FFFF}", "\u{10FFFF}".codeUnits);
|
||||
|
||||
var leadSurrogate = "\u{10ffff}"[0];
|
||||
test(leadSurrogate, iter(1, 0xDBFF));
|
||||
test(leadSurrogate, [0xDBFF]);
|
||||
test(leadSurrogate, const [0xDBFF]);
|
||||
test(leadSurrogate, new List(1)..[0] = 0xDBFF);
|
||||
test(leadSurrogate, new Uint16List(1)..[0] = 0xDBFF);
|
||||
test(leadSurrogate, new Uint32List(1)..[0] = 0xDBFF);
|
||||
test(leadSurrogate, leadSurrogate.codeUnits);
|
||||
|
||||
var tailSurrogate = "\u{10ffff}"[1];
|
||||
test(tailSurrogate, iter(1, 0xDFFF));
|
||||
test(tailSurrogate, [0xDFFF]);
|
||||
test(tailSurrogate, const [0xDFFF]);
|
||||
test(tailSurrogate, new List(1)..[0] = 0xDFFF);
|
||||
test(tailSurrogate, new Uint16List(1)..[0] = 0xDFFF);
|
||||
test(tailSurrogate, new Uint32List(1)..[0] = 0xDFFF);
|
||||
test(tailSurrogate, tailSurrogate.codeUnits);
|
||||
|
||||
testThrows(null);
|
||||
testThrows("not an iterable");
|
||||
testThrows(42);
|
||||
testThrows([-1]);
|
||||
testThrows(new List(1)..[0] = -1);
|
||||
testThrows(const [-1]);
|
||||
testThrows(new Int8List(1)..[0] = -1);
|
||||
testThrows(new Int16List(1)..[0] = -1);
|
||||
testThrows(new Int32List(1)..[0] = -1);
|
||||
testThrows([0x110000]);
|
||||
testThrows(new List(1)..[0] = 0x110000);
|
||||
testThrows(const [0x110000]);
|
||||
testThrows(new Int32List(1)..[0] = 0x110000);
|
||||
|
||||
// Check start/end
|
||||
var list = const [0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48];
|
||||
for (var iterable in [
|
||||
iter(list.length, list),
|
||||
list.toList(growable: true),
|
||||
list.toList(growable: false),
|
||||
list,
|
||||
new Uint8List(8)..setRange(0, 8, list),
|
||||
new Uint16List(8)..setRange(0, 8, list),
|
||||
new Uint32List(8)..setRange(0, 8, list),
|
||||
"ABCDEFGH".codeUnits,
|
||||
]) {
|
||||
test("ABCDEFGH", iterable);
|
||||
// start varies, end is null.
|
||||
test("ABCDEFGH", iterable, 0);
|
||||
test("BCDEFGH", iterable, 1);
|
||||
test("H", iterable, 7);
|
||||
test("", iterable, 8);
|
||||
// start = 0, end varies.
|
||||
test("ABCDEFGH", iterable, 0);
|
||||
test("A", iterable, 0, 1);
|
||||
test("AB", iterable, 0, 2);
|
||||
test("ABCDEFG", iterable, 0, 7);
|
||||
test("ABCDEFGH", iterable, 0, 8);
|
||||
test("", iterable, 0, 0);
|
||||
// Both varying.
|
||||
test("ABCDEFGH", iterable, 0, 8);
|
||||
test("AB", iterable, 0, 2);
|
||||
test("GH", iterable, 6, 8);
|
||||
test("DE", iterable, 3, 5);
|
||||
test("", iterable, 3, 3);
|
||||
}
|
||||
// Can split surrogates in input, but not a single big code point.
|
||||
test(leadSurrogate, [0xDBFF, 0xDFFF], 0, 1);
|
||||
test(tailSurrogate, [0xDBFF, 0xDFFF], 1);
|
||||
test("\u{10FFFF}", [0x10FFFF], 0, 1);
|
||||
|
||||
void testThrowsRange(iterable, [start = 0, end]) {
|
||||
Expect.throws(() => new String.fromCharCodes(iterable, start, end),
|
||||
(e) => e is RangeError);
|
||||
}
|
||||
|
||||
// Test varying slices of the code units of a string.
|
||||
testSubstring(string) {
|
||||
var codes = string.codeUnits;
|
||||
int length = string.length;
|
||||
for (var iterable in [
|
||||
iter(length, codes),
|
||||
codes.toList(growable: true),
|
||||
codes.toList(growable: false),
|
||||
new Uint16List(length)..setRange(0, length, codes),
|
||||
new Int32List(length)..setRange(0, length, codes),
|
||||
new Uint32List(length)..setRange(0, length, codes),
|
||||
codes,
|
||||
]) {
|
||||
var newString = new String.fromCharCodes(iterable);
|
||||
Expect.equals(string, newString);
|
||||
for (int i = 0; i < length; i = i * 2 + 1) {
|
||||
test(string.substring(i), iterable, i);
|
||||
test(string.substring(0, i), iterable, 0, i);
|
||||
for (int j = 0; i + j < length; j = j * 2 + 1) {
|
||||
test(string.substring(i, i + j), iterable, i, i + j);
|
||||
}
|
||||
}
|
||||
|
||||
testThrowsRange(iterable, -1);
|
||||
testThrowsRange(iterable, 0, -1);
|
||||
testThrowsRange(iterable, 2, 1);
|
||||
testThrowsRange(iterable, 0, length + 1);
|
||||
testThrowsRange(iterable, length + 1);
|
||||
testThrowsRange(iterable, length + 1, length + 2);
|
||||
}
|
||||
}
|
||||
|
||||
testSubstring("");
|
||||
testSubstring("ABCDEFGH");
|
||||
// length > 128
|
||||
testSubstring("ABCDEFGH" * 33);
|
||||
testSubstring("\x00" * 357);
|
||||
// length > 128 and non-ASCII.
|
||||
testSubstring("\uFFFD\uFFFE\u{10000}\u{10ffff}c\x00" * 37);
|
||||
|
||||
// Large List.
|
||||
var megaList = ("abcde" * 200000).codeUnits.toList();
|
||||
test("abcde" * 199998, megaList, 5, 999995);
|
||||
// Large Uint8List.
|
||||
test("abcde" * 199998, new Uint8List.fromList(megaList), 5, 999995);
|
||||
|
||||
const cLatin1 = const [0x00, 0xff];
|
||||
const cUtf16 = const [0x00, 0xffff, 0xdfff, 0xdbff, 0xdfff, 0xdbff];
|
||||
const cCodepoints = const [0x00, 0xffff, 0xdfff, 0x10ffff, 0xdbff];
|
||||
List gLatin1 = cLatin1.toList(growable: true);
|
||||
List gUtf16 = cUtf16.toList(growable: true);
|
||||
List gCodepoints = cCodepoints.toList(growable: true);
|
||||
List fLatin1 = cLatin1.toList(growable: false);
|
||||
List fUtf16 = cUtf16.toList(growable: false);
|
||||
List fCodepoints = cCodepoints.toList(growable: false);
|
||||
Uint8List bLatin1 = new Uint8List(2)..setRange(0, 2, cLatin1);
|
||||
Uint16List wLatin1 = new Uint16List(2)..setRange(0, 2, cLatin1);
|
||||
Uint16List wUtf16 = new Uint16List(6)..setRange(0, 6, cUtf16);
|
||||
Uint32List lLatin1 = new Uint32List(2)..setRange(0, 2, cLatin1);
|
||||
Uint32List lUtf16 = new Uint32List(6)..setRange(0, 6, cUtf16);
|
||||
Uint32List lCodepoints = new Uint32List(5)..setRange(0, 5, cCodepoints);
|
||||
Uint8List bvLatin1 = new Uint8List.view(bLatin1.buffer);
|
||||
Uint16List wvLatin1 = new Uint16List.view(wLatin1.buffer);
|
||||
Uint16List wvUtf16 = new Uint16List.view(wUtf16.buffer);
|
||||
Uint32List lvLatin1 = new Uint32List.view(lLatin1.buffer);
|
||||
Uint32List lvUtf16 = new Uint32List.view(lUtf16.buffer);
|
||||
Uint32List lvCodepoints = new Uint32List.view(lCodepoints.buffer);
|
||||
var buffer = new Uint8List(200).buffer;
|
||||
Uint8List bbLatin1 = new Uint8List.view(buffer, 3, 2)..setAll(0, bLatin1);
|
||||
Uint16List wbLatin1 = new Uint16List.view(buffer, 8, 2)..setAll(0, wLatin1);
|
||||
Uint16List wbUtf16 = new Uint16List.view(buffer, 16, 6)..setAll(0, wUtf16);
|
||||
Uint32List lbLatin1 = new Uint32List.view(buffer, 32, 2)..setAll(0, lLatin1);
|
||||
Uint32List lbUtf16 = new Uint32List.view(buffer, 64, 6)..setAll(0, lUtf16);
|
||||
Uint32List lbCodepoints = new Uint32List.view(buffer, 128, 5)
|
||||
..setAll(0, lCodepoints);
|
||||
|
||||
String sLatin1 = "\x00\xff";
|
||||
String sUnicode =
|
||||
"\x00\uffff$tailSurrogate$leadSurrogate$tailSurrogate$leadSurrogate";
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = i + 1; j < 2; j++) {
|
||||
test(sLatin1.substring(i, j), cLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), gLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), fLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), bLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), wLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), lLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), bvLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), wvLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), lvLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), bbLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), wbLatin1, i, j);
|
||||
test(sLatin1.substring(i, j), lbLatin1, i, j);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 6; i++) {
|
||||
for (int j = i + 1; j < 6; j++) {
|
||||
test(sUnicode.substring(i, j), cUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), gUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), fUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), wUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), lUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), wvUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), lvUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), wbUtf16, i, j);
|
||||
test(sUnicode.substring(i, j), lbUtf16, i, j);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
for (int j = i + 1; j < 5; j++) {
|
||||
int stringEnd = j < 4 ? j : j + 1;
|
||||
test(sUnicode.substring(i, stringEnd), cCodepoints, i, j);
|
||||
test(sUnicode.substring(i, stringEnd), gCodepoints, i, j);
|
||||
test(sUnicode.substring(i, stringEnd), fCodepoints, i, j);
|
||||
test(sUnicode.substring(i, stringEnd), lCodepoints, i, j);
|
||||
test(sUnicode.substring(i, stringEnd), lvCodepoints, i, j);
|
||||
test(sUnicode.substring(i, stringEnd), lbCodepoints, i, j);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +0,0 @@
|
|||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
@NoInline()
|
||||
@AssumeDynamic()
|
||||
returnStringOrNull() {
|
||||
return new DateTime.now().millisecondsSinceEpoch == 0 ? 'foo' : null;
|
||||
}
|
||||
|
||||
main() {
|
||||
Expect.throws(() => 'foo' + returnStringOrNull(), (e) => e is ArgumentError);
|
||||
Expect.throws(() => 'foo'.split(returnStringOrNull()),
|
||||
(e) => e is ArgumentError || e is NoSuchMethodError);
|
||||
}
|
|
@ -1,111 +0,0 @@
|
|||
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
// Dart test for testing String.allMatches.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
String str = "this is a string with hello here and hello there";
|
||||
|
||||
main() {
|
||||
testNoMatch();
|
||||
testOneMatch();
|
||||
testTwoMatches();
|
||||
testEmptyPattern();
|
||||
testEmptyString();
|
||||
testEmptyPatternAndString();
|
||||
testMatchAsPrefix();
|
||||
testAllMatchesStart();
|
||||
}
|
||||
|
||||
testNoMatch() {
|
||||
// Also tests that RegExp groups don't work.
|
||||
String helloPattern = "with (hello)";
|
||||
Iterable<Match> matches = helloPattern.allMatches(str);
|
||||
Expect.isFalse(matches.iterator.moveNext());
|
||||
}
|
||||
|
||||
testOneMatch() {
|
||||
String helloPattern = "with hello";
|
||||
Iterable<Match> matches = helloPattern.allMatches(str);
|
||||
var iterator = matches.iterator;
|
||||
Expect.isTrue(iterator.moveNext());
|
||||
Match match = iterator.current;
|
||||
Expect.isFalse(iterator.moveNext());
|
||||
Expect.equals(str.indexOf('with', 0), match.start);
|
||||
Expect.equals(str.indexOf('with', 0) + helloPattern.length, match.end);
|
||||
Expect.equals(helloPattern, match.pattern);
|
||||
Expect.equals(str, match.input);
|
||||
Expect.equals(helloPattern, match[0]);
|
||||
Expect.equals(0, match.groupCount);
|
||||
}
|
||||
|
||||
testTwoMatches() {
|
||||
String helloPattern = "hello";
|
||||
Iterable<Match> matches = helloPattern.allMatches(str);
|
||||
|
||||
int count = 0;
|
||||
int start = 0;
|
||||
for (var match in matches) {
|
||||
count++;
|
||||
Expect.equals(str.indexOf('hello', start), match.start);
|
||||
Expect.equals(str.indexOf('hello', start) + helloPattern.length, match.end);
|
||||
Expect.equals(helloPattern, match.pattern);
|
||||
Expect.equals(str, match.input);
|
||||
Expect.equals(helloPattern, match[0]);
|
||||
Expect.equals(0, match.groupCount);
|
||||
start = match.end;
|
||||
}
|
||||
Expect.equals(2, count);
|
||||
}
|
||||
|
||||
testEmptyPattern() {
|
||||
String pattern = "";
|
||||
Iterable<Match> matches = pattern.allMatches(str);
|
||||
Expect.isTrue(matches.iterator.moveNext());
|
||||
}
|
||||
|
||||
testEmptyString() {
|
||||
String pattern = "foo";
|
||||
String str = "";
|
||||
Iterable<Match> matches = pattern.allMatches(str);
|
||||
Expect.isFalse(matches.iterator.moveNext());
|
||||
}
|
||||
|
||||
testEmptyPatternAndString() {
|
||||
String pattern = "";
|
||||
String str = "";
|
||||
Iterable<Match> matches = pattern.allMatches(str);
|
||||
Expect.isTrue(matches.iterator.moveNext());
|
||||
}
|
||||
|
||||
testMatchAsPrefix() {
|
||||
String pattern = "an";
|
||||
String str = "banana";
|
||||
Expect.isNull(pattern.matchAsPrefix(str));
|
||||
Expect.isNull(pattern.matchAsPrefix(str, 0));
|
||||
var m = pattern.matchAsPrefix(str, 1);
|
||||
Expect.equals("an", m[0]);
|
||||
Expect.equals(1, m.start);
|
||||
Expect.isNull(pattern.matchAsPrefix(str, 2));
|
||||
m = pattern.matchAsPrefix(str, 3);
|
||||
Expect.equals("an", m[0]);
|
||||
Expect.equals(3, m.start);
|
||||
Expect.isNull(pattern.matchAsPrefix(str, 4));
|
||||
Expect.isNull(pattern.matchAsPrefix(str, 5));
|
||||
Expect.isNull(pattern.matchAsPrefix(str, 6));
|
||||
Expect.throws(() => pattern.matchAsPrefix(str, -1));
|
||||
Expect.throws(() => pattern.matchAsPrefix(str, 7));
|
||||
}
|
||||
|
||||
testAllMatchesStart() {
|
||||
String p = "ass";
|
||||
String s = "assassin";
|
||||
Expect.equals(2, p.allMatches(s).length);
|
||||
Expect.equals(2, p.allMatches(s, 0).length);
|
||||
Expect.equals(1, p.allMatches(s, 1).length);
|
||||
Expect.equals(0, p.allMatches(s, 4).length);
|
||||
Expect.equals(0, p.allMatches(s, s.length).length);
|
||||
Expect.throws(() => p.allMatches(s, -1));
|
||||
Expect.throws(() => p.allMatches(s, s.length + 1));
|
||||
}
|
|
@ -1,140 +0,0 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
testReplaceAll() {
|
||||
Expect.equals("aXXcaXXdae", "abcabdae".replaceAll("b", "XX"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("XXbcXXbdXXe", "abcabdae".replaceAll("a", "XX"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabdaXX", "abcabdae".replaceAll("e", "XX"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAll("f", "XX"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceAll("from", "to"));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("toto", "fromfrom".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceAll("from", "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAll("a", ""));
|
||||
Expect.equals("AB", "AfromB".replaceAll("from", ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceAll("", "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
|
||||
|
||||
// Pattern strings containing RegExp metacharacters - these are not
|
||||
// interpreted as RegExps.
|
||||
Expect.equals(r"$$", "||".replaceAll("|", r"$"));
|
||||
Expect.equals(r"$$$$", "||".replaceAll("|", r"$$"));
|
||||
Expect.equals(r"x$|x", "x|.|x".replaceAll("|.", r"$"));
|
||||
Expect.equals(r"$$", "..".replaceAll(".", r"$"));
|
||||
Expect.equals(r"[$$$$]", "[..]".replaceAll(".", r"$$"));
|
||||
Expect.equals(r"[$]", "[..]".replaceAll("..", r"$"));
|
||||
Expect.equals(r"$$", r"\\".replaceAll(r"\", r"$"));
|
||||
}
|
||||
|
||||
testReplaceAllMapped() {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
Expect.equals("a[b]ca[b]dae", "abcabdae".replaceAllMapped("b", mark));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".replaceAllMapped("a", mark));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("abcabda[e]", "abcabdae".replaceAllMapped("e", mark));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("abcabdae", "abcabdae".replaceAllMapped("f", mark));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAllMapped("from", mark));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAllMapped("from", mark));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("[from][from]", "fromfrom".replaceAllMapped("from", mark));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("bcbde", "abcabdae".replaceAllMapped("a", (m) => ""));
|
||||
Expect.equals("AB", "AfromB".replaceAllMapped("from", (m) => ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("[]", "".replaceAllMapped("", mark));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("[]A[]B[]C[]", "ABC".replaceAllMapped("", mark));
|
||||
}
|
||||
|
||||
testSplitMapJoin() {
|
||||
String mark(Match m) => "[${m[0]}]";
|
||||
String wrap(String s) => "<${s}>";
|
||||
|
||||
Expect.equals("<a>[b]<ca>[b]<dae>",
|
||||
"abcabdae".splitMapJoin("b", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("<>[a]<bc>[a]<bd>[a]<e>",
|
||||
"abcabdae".splitMapJoin("a", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("<abcabda>[e]<>",
|
||||
"abcabdae".splitMapJoin("e", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("<abcabdae>",
|
||||
"abcabdae".splitMapJoin("f", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("<>", "".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals(
|
||||
"<fro>", "fro".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test when matches are adjacent
|
||||
Expect.equals("<>[from]<>[from]<>",
|
||||
"fromfrom".splitMapJoin("from", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("<>[]<>", "".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("<>[]<A>[]<B>[]<C>[]<>",
|
||||
"ABC".splitMapJoin("", onMatch: mark, onNonMatch: wrap));
|
||||
|
||||
// Test with only onMatch.
|
||||
Expect.equals("[a]bc[a]bd[a]e", "abcabdae".splitMapJoin("a", onMatch: mark));
|
||||
|
||||
// Test with only onNonMatch
|
||||
Expect.equals(
|
||||
"<>a<bc>a<bd>a<e>", "abcabdae".splitMapJoin("a", onNonMatch: wrap));
|
||||
}
|
||||
|
||||
main() {
|
||||
testReplaceAll();
|
||||
testReplaceAllMapped();
|
||||
testSplitMapJoin();
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
String jsText = r"""'$'
|
||||
""";
|
||||
String htmlStr = '%%DART';
|
||||
String htmlOut = htmlStr.replaceAll("%%DART", jsText);
|
||||
Expect.equals(jsText, htmlOut);
|
||||
htmlOut = htmlStr.replaceFirst("%%DART", jsText);
|
||||
Expect.equals(jsText, htmlOut);
|
||||
htmlOut = htmlStr.replaceAll(new RegExp("%%DART"), jsText);
|
||||
Expect.equals(jsText, htmlOut);
|
||||
htmlOut = htmlStr.replaceFirst(new RegExp("%%DART"), jsText);
|
||||
Expect.equals(jsText, htmlOut);
|
||||
|
||||
// Regression test, http://dartbug.com/17886
|
||||
String doubleDollar = r"$'$`";
|
||||
var string = r"flip-flip-flop";
|
||||
var result = string.replaceFirst("flip", doubleDollar);
|
||||
Expect.equals(r"$'$`-flip-flop", result);
|
||||
result = string.replaceAll("flip", doubleDollar);
|
||||
Expect.equals(r"$'$`-$'$`-flop", result);
|
||||
}
|
|
@ -1,227 +0,0 @@
|
|||
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
// Test replaceFirst.
|
||||
Expect.equals("AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals("toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("ABC", "ABC".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceFirst("from", "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("", "from".replaceFirst("from", ""));
|
||||
Expect.equals("AB", "AfromB".replaceFirst("from", ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceFirst("", "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to"));
|
||||
|
||||
// Test startIndex.
|
||||
Expect.equals(
|
||||
"foo-AAA-foo-bar", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 4));
|
||||
|
||||
// Test startIndex skipping one case at the beginning.
|
||||
Expect.equals(
|
||||
"foo-bar-AAA-bar", "foo-bar-foo-bar".replaceFirst("foo", "AAA", 1));
|
||||
|
||||
// Test startIndex skipping one case at the beginning.
|
||||
Expect.equals(
|
||||
"foo-bar-foo-AAA", "foo-bar-foo-bar".replaceFirst("bar", "AAA", 5));
|
||||
|
||||
// Test startIndex replacing with the empty string.
|
||||
Expect.equals("foo-bar--bar", "foo-bar-foo-bar".replaceFirst("foo", "", 1));
|
||||
|
||||
// Test startIndex with a RegExp with carat
|
||||
Expect.equals("foo-bar-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirst(new RegExp(r"^foo"), "", 8));
|
||||
|
||||
// Test startIndex with a RegExp
|
||||
Expect.equals(
|
||||
"aaa{3}X{3}", "aaa{3}aaa{3}".replaceFirst(new RegExp(r"a{3}"), "X", 1));
|
||||
|
||||
// Test startIndex with regexp-looking String
|
||||
Expect.equals("aaa{3}aaX", "aaa{3}aaa{3}".replaceFirst("a{3}", "X", 3));
|
||||
|
||||
// Test negative startIndex
|
||||
Expect.throws(
|
||||
() => "hello".replaceFirst("h", "X", -1), (e) => e is RangeError);
|
||||
|
||||
// Test startIndex too large
|
||||
Expect.throws(
|
||||
() => "hello".replaceFirst("h", "X", 6), (e) => e is RangeError);
|
||||
|
||||
// Test null startIndex
|
||||
Expect.throws(
|
||||
() => "hello".replaceFirst("h", "X", null), (e) => e is ArgumentError);
|
||||
|
||||
// Test object startIndex
|
||||
Expect.throws(() => "hello".replaceFirst("h", "X", new Object()));
|
||||
|
||||
// Test replaceFirstMapped.
|
||||
|
||||
Expect.equals(
|
||||
"AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test with the replaced string at the beginning.
|
||||
Expect.equals(
|
||||
"toABtoCDtoE", "fromABtoCDtoE".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals("toABtoCDtoEto",
|
||||
"fromABtoCDtoEto".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("ABC", "ABC".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceFirstMapped("from", (_) => "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("", "from".replaceFirstMapped("from", (_) => ""));
|
||||
Expect.equals("AB", "AfromB".replaceFirstMapped("from", (_) => ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceFirstMapped("", (_) => "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirstMapped("", (_) => "to"));
|
||||
|
||||
// Test startIndex.
|
||||
Expect.equals("foo-AAA-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 4));
|
||||
|
||||
// Test startIndex skipping one case at the beginning.
|
||||
Expect.equals("foo-bar-AAA-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "AAA", 1));
|
||||
|
||||
// Test startIndex skipping one case at the beginning.
|
||||
Expect.equals("foo-bar-foo-AAA",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("bar", (_) => "AAA", 5));
|
||||
|
||||
// Test startIndex replacing with the empty string.
|
||||
Expect.equals("foo-bar--bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("foo", (_) => "", 1));
|
||||
|
||||
// Test startIndex with a RegExp with carat
|
||||
Expect.equals("foo-bar-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped(new RegExp(r"^foo"), (_) => "", 8));
|
||||
|
||||
// Test startIndex with a RegExp
|
||||
Expect.equals("aaa{3}X{3}",
|
||||
"aaa{3}aaa{3}".replaceFirstMapped(new RegExp(r"a{3}"), (_) => "X", 1));
|
||||
|
||||
// Test startIndex with regexp-looking String
|
||||
Expect.equals(
|
||||
"aaa{3}aaX", "aaa{3}aaa{3}".replaceFirstMapped("a{3}", (_) => "X", 3));
|
||||
|
||||
// Test negative startIndex
|
||||
Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", -1),
|
||||
(e) => e is RangeError);
|
||||
|
||||
// Test startIndex too large
|
||||
Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", 6),
|
||||
(e) => e is RangeError);
|
||||
|
||||
// Test null startIndex
|
||||
Expect.throws(() => "hello".replaceFirstMapped("h", (_) => "X", null),
|
||||
(e) => e is ArgumentError);
|
||||
|
||||
// Test object startIndex
|
||||
Expect
|
||||
.throws(() => "hello".replaceFirstMapped("h", (_) => "X", new Object()));
|
||||
|
||||
// Test replacement depending on argument.
|
||||
Expect.equals("foo-BAR-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v[0].toUpperCase()));
|
||||
|
||||
Expect.equals("foo-[bar]-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => "[${v[0]}]"));
|
||||
|
||||
Expect.equals("foo-foo-bar-foo-bar-foo-bar",
|
||||
"foo-bar-foo-bar".replaceFirstMapped("bar", (v) => v.input));
|
||||
|
||||
// Test replacement throwing.
|
||||
Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) => throw 42),
|
||||
(e) => e == 42);
|
||||
|
||||
// Test replacement returning non-String.
|
||||
var o = new Object();
|
||||
Expect.equals(
|
||||
"foo-$o",
|
||||
"foo-bar".replaceFirstMapped("bar", (v) {
|
||||
return o;
|
||||
}));
|
||||
|
||||
Expect.equals(
|
||||
"foo-42",
|
||||
"foo-bar".replaceFirstMapped("bar", (v) {
|
||||
return 42;
|
||||
}));
|
||||
|
||||
// Test replacement returning object throwing on string-conversion.
|
||||
var n = new Naughty();
|
||||
Expect.throws(() => "foo-bar".replaceFirstMapped("bar", (v) {
|
||||
return n;
|
||||
}));
|
||||
|
||||
for (var string in ["", "x", "foo", "x\u2000z"]) {
|
||||
for (var replacement in ["", "foo", string]) {
|
||||
for (int start = 0; start <= string.length; start++) {
|
||||
var expect;
|
||||
for (int end = start; end <= string.length; end++) {
|
||||
expect =
|
||||
string.substring(0, start) + replacement + string.substring(end);
|
||||
Expect.equals(expect, string.replaceRange(start, end, replacement),
|
||||
'"$string"[$start:$end]="$replacement"');
|
||||
}
|
||||
// Reuse expect from "end == string.length" case when omitting end.
|
||||
Expect.equals(expect, string.replaceRange(start, null, replacement),
|
||||
'"$string"[$start:]="$replacement"');
|
||||
}
|
||||
}
|
||||
Expect.throws(() => string.replaceRange(0, 0, null));
|
||||
Expect.throws(() => string.replaceRange(0, 0, 42));
|
||||
Expect.throws(() => string.replaceRange(0, 0, ["x"]));
|
||||
Expect.throws(() => string.replaceRange(-1, 0, "x"));
|
||||
Expect.throws(() => string.replaceRange(0, string.length + 1, "x"));
|
||||
}
|
||||
}
|
||||
|
||||
// Fails to return a String on toString, throws if converted by "$naughty".
|
||||
class Naughty {
|
||||
toString() => this;
|
||||
}
|
|
@ -1,84 +0,0 @@
|
|||
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
test(String s, List<int> expectedRunes) {
|
||||
Runes runes = s.runes;
|
||||
Expect.identical(s, runes.string);
|
||||
|
||||
// for-in
|
||||
var res = [];
|
||||
for (int rune in runes) {
|
||||
res.add(rune);
|
||||
}
|
||||
Expect.listEquals(expectedRunes, res);
|
||||
|
||||
// manual iteration, backwards.
|
||||
res = [];
|
||||
for (var it = runes.iterator..reset(s.length); it.movePrevious();) {
|
||||
res.add(it.current);
|
||||
}
|
||||
Expect.listEquals(expectedRunes.reversed.toList(), res);
|
||||
|
||||
// Setting rawIndex.
|
||||
RuneIterator it = runes.iterator;
|
||||
it.rawIndex = 1;
|
||||
Expect.equals(expectedRunes[1], it.current);
|
||||
|
||||
it = runes.iterator;
|
||||
it.moveNext();
|
||||
Expect.equals(0, it.rawIndex);
|
||||
it.moveNext();
|
||||
Expect.equals(1, it.rawIndex);
|
||||
it.moveNext();
|
||||
Expect.isTrue(1 < it.rawIndex);
|
||||
it.rawIndex = 1;
|
||||
Expect.equals(1, it.rawIndex);
|
||||
Expect.equals(expectedRunes[1], it.current);
|
||||
|
||||
// Reset, moveNext.
|
||||
it.reset(1);
|
||||
Expect.equals(null, it.rawIndex);
|
||||
Expect.equals(null, it.current);
|
||||
it.moveNext();
|
||||
Expect.equals(1, it.rawIndex);
|
||||
Expect.equals(expectedRunes[1], it.current);
|
||||
|
||||
// Reset, movePrevious.
|
||||
it.reset(1);
|
||||
Expect.equals(null, it.rawIndex);
|
||||
Expect.equals(null, it.current);
|
||||
it.movePrevious();
|
||||
Expect.equals(0, it.rawIndex);
|
||||
Expect.equals(expectedRunes[0], it.current);
|
||||
|
||||
// .map
|
||||
Expect.listEquals(expectedRunes.map((x) => x.toRadixString(16)).toList(),
|
||||
runes.map((x) => x.toRadixString(16)).toList());
|
||||
}
|
||||
|
||||
// First character must be single-code-unit for test.
|
||||
test("abc", [0x61, 0x62, 0x63]);
|
||||
test("\x00\u0000\u{000000}", [0, 0, 0]);
|
||||
test("\u{ffff}\u{10000}\u{10ffff}", [0xffff, 0x10000, 0x10ffff]);
|
||||
String string = new String.fromCharCodes(
|
||||
[0xdc00, 0xd800, 61, 0xd800, 0xdc00, 62, 0xdc00, 0xd800]);
|
||||
test(string, [0xdc00, 0xd800, 61, 0x10000, 62, 0xdc00, 0xd800]);
|
||||
|
||||
// Setting position in the middle of a surrogate pair is not allowed.
|
||||
var r = new Runes("\u{10000}");
|
||||
var it = r.iterator;
|
||||
it.moveNext();
|
||||
Expect.equals(0x10000, it.current);
|
||||
|
||||
// Setting rawIndex inside surrogate pair.
|
||||
Expect.throws(() {
|
||||
it.rawIndex = 1;
|
||||
}, (e) => e is Error);
|
||||
Expect.throws(() {
|
||||
it.reset(1);
|
||||
}, (e) => e is Error);
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// Test that different representations of the same string are all equal.
|
||||
|
||||
import "dart:convert";
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
var base = "\u{10412}";
|
||||
var strings = [
|
||||
"\u{10412}",
|
||||
"𐐒",
|
||||
new String.fromCharCodes([0xd801, 0xdc12]),
|
||||
base[0] + base[1],
|
||||
"$base",
|
||||
"${base[0]}${base[1]}",
|
||||
"${base[0]}${base.substring(1)}",
|
||||
new String.fromCharCodes([0x10412]),
|
||||
("a" + base).substring(1),
|
||||
(new StringBuffer()..writeCharCode(0xd801)..writeCharCode(0xdc12))
|
||||
.toString(),
|
||||
(new StringBuffer()..writeCharCode(0x10412)).toString(),
|
||||
JSON.decode('"\u{10412}"'),
|
||||
(JSON.decode('{"\u{10412}":[]}') as Map).keys.first
|
||||
];
|
||||
for (String string in strings) {
|
||||
Expect.equals(base.length, string.length);
|
||||
Expect.equals(base, string);
|
||||
Expect.equals(base.hashCode, string.hashCode);
|
||||
Expect.listEquals(base.codeUnits.toList(), string.codeUnits.toList());
|
||||
}
|
||||
}
|
|
@ -1,135 +0,0 @@
|
|||
// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
main() {
|
||||
testSplitString();
|
||||
testSplitRegExp();
|
||||
testSplitPattern();
|
||||
}
|
||||
|
||||
testSplit(List expect, String string, Pattern pattern) {
|
||||
String patternString;
|
||||
if (pattern is String) {
|
||||
patternString = '"$pattern"';
|
||||
} else if (pattern is RegExp) {
|
||||
patternString = "/${pattern.pattern}/";
|
||||
} else {
|
||||
patternString = pattern.toString();
|
||||
}
|
||||
Expect.listEquals(
|
||||
expect, string.split(pattern), '"$string".split($patternString)');
|
||||
}
|
||||
|
||||
/** String patterns. */
|
||||
void testSplitString() {
|
||||
// Normal match.
|
||||
testSplit(["a", "b", "c"], "a b c", " ");
|
||||
testSplit(["a", "b", "c"], "adbdc", "d");
|
||||
testSplit(["a", "b", "c"], "addbddc", "dd");
|
||||
// No match.
|
||||
testSplit(["abc"], "abc", " ");
|
||||
testSplit(["a"], "a", "b");
|
||||
testSplit([""], "", "b");
|
||||
// Empty match matches everywhere except start/end.
|
||||
testSplit(["a", "b", "c"], "abc", "");
|
||||
// All empty parts.
|
||||
testSplit(["", "", "", "", ""], "aaaa", "a");
|
||||
testSplit(["", "", "", "", ""], " ", " ");
|
||||
testSplit(["", ""], "a", "a");
|
||||
// No overlapping matches. Match as early as possible.
|
||||
testSplit(["", "", "", "a"], "aaaaaaa", "aa");
|
||||
// Cannot split the empty string.
|
||||
testSplit([], "", ""); // Match.
|
||||
testSplit([""], "", "a"); // No match.
|
||||
}
|
||||
|
||||
/** RegExp patterns. */
|
||||
void testSplitRegExp() {
|
||||
testSplitWithRegExp((s) => new RegExp(s));
|
||||
}
|
||||
|
||||
/** Non-String, non-RegExp patterns. */
|
||||
void testSplitPattern() {
|
||||
testSplitWithRegExp((s) => new RegExpWrap(s));
|
||||
}
|
||||
|
||||
void testSplitWithRegExp(makePattern) {
|
||||
testSplit(["a", "b", "c"], "a b c", makePattern(r" "));
|
||||
|
||||
testSplit(["a", "b", "c"], "adbdc", makePattern(r"[dz]"));
|
||||
|
||||
testSplit(["a", "b", "c"], "addbddc", makePattern(r"dd"));
|
||||
|
||||
testSplit(["abc"], "abc", makePattern(r"b$"));
|
||||
|
||||
testSplit(["a", "b", "c"], "abc", makePattern(r""));
|
||||
|
||||
testSplit(["", "", "", ""], " ", makePattern(r"[ ]"));
|
||||
|
||||
// Non-zero-length match at end.
|
||||
testSplit(["aa", ""], "aaa", makePattern(r"a$"));
|
||||
|
||||
// Zero-length match at end.
|
||||
testSplit(["aaa"], "aaa", makePattern(r"$"));
|
||||
|
||||
// Non-zero-length match at start.
|
||||
testSplit(["", "aa"], "aaa", makePattern(r"^a"));
|
||||
|
||||
// Zero-length match at start.
|
||||
testSplit(["aaa"], "aaa", makePattern(r"^"));
|
||||
|
||||
// Picks first match, not longest or shortest.
|
||||
testSplit(["", "", "", "a"], "aaaaaaa", makePattern(r"aa|aaa"));
|
||||
|
||||
testSplit(["", "", "", "a"], "aaaaaaa", makePattern(r"aa|"));
|
||||
|
||||
testSplit(["", "", "a"], "aaaaaaa", makePattern(r"aaa|aa"));
|
||||
|
||||
// Zero-width match depending on the following.
|
||||
testSplit(["a", "bc"], "abc", makePattern(r"(?=[ab])"));
|
||||
|
||||
testSplit(["a", "b", "c"], "abc", makePattern(r"(?!^)"));
|
||||
|
||||
// Cannot split empty string.
|
||||
testSplit([], "", makePattern(r""));
|
||||
|
||||
testSplit([], "", makePattern(r"(?:)"));
|
||||
|
||||
testSplit([], "", makePattern(r"$|(?=.)"));
|
||||
|
||||
testSplit([""], "", makePattern(r"a"));
|
||||
|
||||
// Can split singleton string if it matches.
|
||||
testSplit(["", ""], "a", makePattern(r"a"));
|
||||
|
||||
testSplit(["a"], "a", makePattern(r"b"));
|
||||
|
||||
// Do not include captures.
|
||||
testSplit(["a", "", "a"], "abba", makePattern(r"(b)"));
|
||||
|
||||
testSplit(["a", "a"], "abba", makePattern(r"(bb)"));
|
||||
|
||||
testSplit(["a", "a"], "abba", makePattern(r"(b*)"));
|
||||
|
||||
testSplit(["a", "a"], "aa", makePattern(r"(b*)"));
|
||||
|
||||
// But captures are still there, and do work with backreferences.
|
||||
testSplit(["a", "cba"], "abcba", makePattern(r"([bc])(?=.*\1)"));
|
||||
}
|
||||
|
||||
// A Pattern implementation with the same capabilities as a RegExp, but not
|
||||
// directly recognizable as a RegExp.
|
||||
class RegExpWrap implements Pattern {
|
||||
final regexp;
|
||||
RegExpWrap(String source) : regexp = new RegExp(source);
|
||||
Iterable<Match> allMatches(String string, [int start = 0]) =>
|
||||
regexp.allMatches(string, start);
|
||||
|
||||
Match matchAsPrefix(String string, [int start = 0]) =>
|
||||
regexp.matchAsPrefix(string, start);
|
||||
|
||||
String toString() => "Wrap(/${regexp.pattern}/)";
|
||||
}
|
Loading…
Reference in a new issue