blob: a28972307498c5e944b3090d070dcbdfec7c2b65 [file] [log] [blame]
Robert Nystromb998b102019-12-17 23:06:24 +00001// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5import "package:expect/expect.dart";
6
7// Dart test program for RegExp.allMatches.
8
9class RegExpAllMatchesTest {
10 static testIterator() {
11 var matches = new RegExp("foo").allMatches("foo foo");
12 Iterator it = matches.iterator;
Erik Ernst3a7eeb62020-11-05 14:26:53 +000013 if (hasSoundNullSafety) {
Nate Bosch735bcbe2020-09-18 17:43:57 +000014 Expect.throws(() => it.current);
15 } else {
16 Expect.isNull(it.current);
17 }
Robert Nystromb998b102019-12-17 23:06:24 +000018 Expect.isTrue(it.moveNext());
19 Expect.equals('foo', it.current.group(0));
20 Expect.isTrue(it.moveNext());
21 Expect.equals('foo', it.current.group(0));
22 Expect.isFalse(it.moveNext());
23
24 // Run two iterators over the same results.
25 it = matches.iterator;
26 Iterator it2 = matches.iterator;
27 Expect.isTrue(it.moveNext());
28 Expect.isTrue(it2.moveNext());
29 Expect.equals('foo', it.current.group(0));
30 Expect.equals('foo', it2.current.group(0));
31 Expect.isTrue(it.moveNext());
32 Expect.isTrue(it2.moveNext());
33 Expect.equals('foo', it.current.group(0));
34 Expect.equals('foo', it2.current.group(0));
35 Expect.equals(false, it.moveNext());
36 Expect.equals(false, it2.moveNext());
37 }
38
39 static testForEach() {
40 var matches = new RegExp("foo").allMatches("foo foo");
41 var strbuf = new StringBuffer();
42 matches.forEach((Match m) {
43 strbuf.write(m.group(0));
44 });
45 Expect.equals("foofoo", strbuf.toString());
46 }
47
48 static testMap() {
49 var matches = new RegExp("foo?").allMatches("foo fo foo fo");
50 var mapped = matches.map((Match m) => "${m.group(0)}bar");
51 Expect.equals(4, mapped.length);
52 var strbuf = new StringBuffer();
53 for (String s in mapped) {
54 strbuf.write(s);
55 }
56 Expect.equals("foobarfobarfoobarfobar", strbuf.toString());
57 }
58
59 static testFilter() {
60 var matches = new RegExp("foo?").allMatches("foo fo foo fo");
61 var filtered = matches.where((Match m) {
62 return m.group(0) == 'foo';
63 });
64 Expect.equals(2, filtered.length);
65 var strbuf = new StringBuffer();
66 for (Match m in filtered) {
67 strbuf.write(m.group(0));
68 }
69 Expect.equals("foofoo", strbuf.toString());
70 }
71
72 static testEvery() {
73 var matches = new RegExp("foo?").allMatches("foo fo foo fo");
74 Expect.equals(true, matches.every((Match m) {
75 return m.group(0)!.startsWith("fo");
76 }));
77 Expect.equals(false, matches.every((Match m) {
78 return m.group(0)!.startsWith("foo");
79 }));
80 }
81
82 static testSome() {
83 var matches = new RegExp("foo?").allMatches("foo fo foo fo");
84 Expect.equals(true, matches.any((Match m) {
85 return m.group(0)!.startsWith("fo");
86 }));
87 Expect.equals(true, matches.any((Match m) {
88 return m.group(0)!.startsWith("foo");
89 }));
90 Expect.equals(false, matches.any((Match m) {
91 return m.group(0)!.startsWith("fooo");
92 }));
93 }
94
95 static testIsEmpty() {
96 var matches = new RegExp("foo?").allMatches("foo fo foo fo");
97 Expect.equals(false, matches.isEmpty);
98 matches = new RegExp("fooo").allMatches("foo fo foo fo");
99 Expect.equals(true, matches.isEmpty);
100 }
101
102 static testGetCount() {
103 var matches = new RegExp("foo?").allMatches("foo fo foo fo");
104 Expect.equals(4, matches.length);
105 matches = new RegExp("fooo").allMatches("foo fo foo fo");
106 Expect.equals(0, matches.length);
107 }
108
109 static testMain() {
110 testIterator();
111 testForEach();
112 testMap();
113 testFilter();
114 testEvery();
115 testSome();
116 testIsEmpty();
117 testGetCount();
118 }
119}
120
121main() {
122 RegExpAllMatchesTest.testMain();
123}