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