mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
![Erik Ernst](/assets/img/avatar_default.png)
Change-Id: If3912d75c5f89a741299b2fae4299d01ac928eec Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170424 Reviewed-by: Lasse R.H. Nielsen <lrn@google.com> Reviewed-by: Michael Thomsen <mit@google.com> Reviewed-by: Mayank Patke <fishythefish@google.com> Commit-Queue: Erik Ernst <eernst@google.com>
124 lines
3.6 KiB
Dart
124 lines
3.6 KiB
Dart
// 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";
|
|
|
|
// Dart test program for RegExp.allMatches.
|
|
|
|
class RegExpAllMatchesTest {
|
|
static testIterator() {
|
|
var matches = new RegExp("foo").allMatches("foo foo");
|
|
Iterator it = matches.iterator;
|
|
if (hasSoundNullSafety) {
|
|
Expect.throws(() => it.current);
|
|
} else {
|
|
Expect.isNull(it.current);
|
|
}
|
|
Expect.isTrue(it.moveNext());
|
|
Expect.equals('foo', it.current.group(0));
|
|
Expect.isTrue(it.moveNext());
|
|
Expect.equals('foo', it.current.group(0));
|
|
Expect.isFalse(it.moveNext());
|
|
|
|
// Run two iterators over the same results.
|
|
it = matches.iterator;
|
|
Iterator it2 = matches.iterator;
|
|
Expect.isTrue(it.moveNext());
|
|
Expect.isTrue(it2.moveNext());
|
|
Expect.equals('foo', it.current.group(0));
|
|
Expect.equals('foo', it2.current.group(0));
|
|
Expect.isTrue(it.moveNext());
|
|
Expect.isTrue(it2.moveNext());
|
|
Expect.equals('foo', it.current.group(0));
|
|
Expect.equals('foo', it2.current.group(0));
|
|
Expect.equals(false, it.moveNext());
|
|
Expect.equals(false, it2.moveNext());
|
|
}
|
|
|
|
static testForEach() {
|
|
var matches = new RegExp("foo").allMatches("foo foo");
|
|
var strbuf = new StringBuffer();
|
|
matches.forEach((Match m) {
|
|
strbuf.write(m.group(0));
|
|
});
|
|
Expect.equals("foofoo", strbuf.toString());
|
|
}
|
|
|
|
static testMap() {
|
|
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
|
|
var mapped = matches.map((Match m) => "${m.group(0)}bar");
|
|
Expect.equals(4, mapped.length);
|
|
var strbuf = new StringBuffer();
|
|
for (String s in mapped) {
|
|
strbuf.write(s);
|
|
}
|
|
Expect.equals("foobarfobarfoobarfobar", strbuf.toString());
|
|
}
|
|
|
|
static testFilter() {
|
|
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
|
|
var filtered = matches.where((Match m) {
|
|
return m.group(0) == 'foo';
|
|
});
|
|
Expect.equals(2, filtered.length);
|
|
var strbuf = new StringBuffer();
|
|
for (Match m in filtered) {
|
|
strbuf.write(m.group(0));
|
|
}
|
|
Expect.equals("foofoo", strbuf.toString());
|
|
}
|
|
|
|
static testEvery() {
|
|
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
|
|
Expect.equals(true, matches.every((Match m) {
|
|
return m.group(0)!.startsWith("fo");
|
|
}));
|
|
Expect.equals(false, matches.every((Match m) {
|
|
return m.group(0)!.startsWith("foo");
|
|
}));
|
|
}
|
|
|
|
static testSome() {
|
|
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
|
|
Expect.equals(true, matches.any((Match m) {
|
|
return m.group(0)!.startsWith("fo");
|
|
}));
|
|
Expect.equals(true, matches.any((Match m) {
|
|
return m.group(0)!.startsWith("foo");
|
|
}));
|
|
Expect.equals(false, matches.any((Match m) {
|
|
return m.group(0)!.startsWith("fooo");
|
|
}));
|
|
}
|
|
|
|
static testIsEmpty() {
|
|
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
|
|
Expect.equals(false, matches.isEmpty);
|
|
matches = new RegExp("fooo").allMatches("foo fo foo fo");
|
|
Expect.equals(true, matches.isEmpty);
|
|
}
|
|
|
|
static testGetCount() {
|
|
var matches = new RegExp("foo?").allMatches("foo fo foo fo");
|
|
Expect.equals(4, matches.length);
|
|
matches = new RegExp("fooo").allMatches("foo fo foo fo");
|
|
Expect.equals(0, matches.length);
|
|
}
|
|
|
|
static testMain() {
|
|
testIterator();
|
|
testForEach();
|
|
testMap();
|
|
testFilter();
|
|
testEvery();
|
|
testSome();
|
|
testIsEmpty();
|
|
testGetCount();
|
|
}
|
|
}
|
|
|
|
main() {
|
|
RegExpAllMatchesTest.testMain();
|
|
}
|