OLD | NEW |
| (Empty) |
1 // 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 | |
5 // Dart test program for RegExp.allMatches. | |
6 | |
7 class RegExpAllMatchesTest { | |
8 static testIterator() { | |
9 var matches = new RegExp("foo").allMatches("foo foo"); | |
10 Iterator it = matches.iterator(); | |
11 Expect.equals(true, it.hasNext()); | |
12 Expect.equals('foo', it.next().group(0)); | |
13 Expect.equals(true, it.hasNext()); | |
14 Expect.equals('foo', it.next().group(0)); | |
15 Expect.equals(false, it.hasNext()); | |
16 | |
17 // Run two iterators over the same results. | |
18 it = matches.iterator(); | |
19 Iterator it2 = matches.iterator(); | |
20 Expect.equals(true, it.hasNext()); | |
21 Expect.equals(true, it2.hasNext()); | |
22 Expect.equals('foo', it.next().group(0)); | |
23 Expect.equals('foo', it2.next().group(0)); | |
24 Expect.equals(true, it.hasNext()); | |
25 Expect.equals(true, it2.hasNext()); | |
26 Expect.equals('foo', it.next().group(0)); | |
27 Expect.equals('foo', it2.next().group(0)); | |
28 Expect.equals(false, it.hasNext()); | |
29 Expect.equals(false, it2.hasNext()); | |
30 } | |
31 | |
32 static testForEach() { | |
33 var matches = new RegExp("foo").allMatches("foo foo"); | |
34 var strbuf = new StringBuffer(); | |
35 matches.forEach((Match m) { | |
36 strbuf.add(m.group(0)); | |
37 }); | |
38 Expect.equals("foofoo", strbuf.toString()); | |
39 } | |
40 | |
41 static testMap() { | |
42 var matches = new RegExp("foo?").allMatches("foo fo foo fo"); | |
43 var mapped = matches.map((Match m) => "${m.group(0)}bar"); | |
44 Expect.equals(4, mapped.length); | |
45 var strbuf = new StringBuffer(); | |
46 for (String s in mapped) { | |
47 strbuf.add(s); | |
48 } | |
49 Expect.equals("foobarfobarfoobarfobar", strbuf.toString()); | |
50 } | |
51 | |
52 static testFilter() { | |
53 var matches = new RegExp("foo?").allMatches("foo fo foo fo"); | |
54 var filtered = matches.filter((Match m) { | |
55 return m.group(0) == 'foo'; | |
56 }); | |
57 Expect.equals(2, filtered.length); | |
58 var strbuf = new StringBuffer(); | |
59 for (Match m in filtered) { | |
60 strbuf.add(m.group(0)); | |
61 } | |
62 Expect.equals("foofoo", strbuf.toString()); | |
63 } | |
64 | |
65 static testEvery() { | |
66 var matches = new RegExp("foo?").allMatches("foo fo foo fo"); | |
67 Expect.equals(true, matches.every((Match m) { | |
68 return m.group(0).startsWith("fo"); | |
69 })); | |
70 Expect.equals(false, matches.every((Match m) { | |
71 return m.group(0).startsWith("foo"); | |
72 })); | |
73 } | |
74 | |
75 static testSome() { | |
76 var matches = new RegExp("foo?").allMatches("foo fo foo fo"); | |
77 Expect.equals(true, matches.some((Match m) { | |
78 return m.group(0).startsWith("fo"); | |
79 })); | |
80 Expect.equals(true, matches.some((Match m) { | |
81 return m.group(0).startsWith("foo"); | |
82 })); | |
83 Expect.equals(false, matches.some((Match m) { | |
84 return m.group(0).startsWith("fooo"); | |
85 })); | |
86 } | |
87 | |
88 static testIsEmpty() { | |
89 var matches = new RegExp("foo?").allMatches("foo fo foo fo"); | |
90 Expect.equals(false, matches.isEmpty()); | |
91 matches = new RegExp("fooo").allMatches("foo fo foo fo"); | |
92 Expect.equals(true, matches.isEmpty()); | |
93 } | |
94 | |
95 static testGetCount() { | |
96 var matches = new RegExp("foo?").allMatches("foo fo foo fo"); | |
97 Expect.equals(4, matches.length); | |
98 matches = new RegExp("fooo").allMatches("foo fo foo fo"); | |
99 Expect.equals(0, matches.length); | |
100 } | |
101 | |
102 static testMain() { | |
103 testIterator(); | |
104 testForEach(); | |
105 testMap(); | |
106 testFilter(); | |
107 testEvery(); | |
108 testSome(); | |
109 testIsEmpty(); | |
110 testGetCount(); | |
111 } | |
112 } | |
113 | |
114 main() { | |
115 RegExpAllMatchesTest.testMain(); | |
116 } | |
OLD | NEW |