OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart test for testing String.allMatches. | 4 // Dart test for testing String.allMatches. |
5 | 5 |
6 String str = "this is a string with hello here and hello there"; | 6 String str = "this is a string with hello here and hello there"; |
7 | 7 |
8 main() { | 8 main() { |
9 testNoMatch(); | 9 testNoMatch(); |
10 testOneMatch(); | 10 testOneMatch(); |
11 testTwoMatches(); | 11 testTwoMatches(); |
12 testEmptyPattern(); | 12 testEmptyPattern(); |
13 testEmptyString(); | 13 testEmptyString(); |
14 testEmptyPatternAndString(); | 14 testEmptyPatternAndString(); |
15 } | 15 } |
16 | 16 |
17 testNoMatch() { | 17 testNoMatch() { |
18 // Also tests that RegExp groups don't work. | 18 // Also tests that RegExp groups don't work. |
19 String helloPattern = "with (hello)"; | 19 String helloPattern = "with (hello)"; |
20 Iterable<Match> matches = helloPattern.allMatches(str); | 20 Iterable<Match> matches = helloPattern.allMatches(str); |
21 Expect.isFalse(matches.iterator().hasNext); | 21 Expect.isFalse(matches.iterator().hasNext); |
22 } | 22 } |
23 | 23 |
24 testOneMatch() { | 24 testOneMatch() { |
25 String helloPattern = "with hello"; | 25 String helloPattern = "with hello"; |
26 Iterable<Match> matches = helloPattern.allMatches(str); | 26 Iterable<Match> matches = helloPattern.allMatches(str); |
27 var iterator = matches.iterator(); | 27 var iterator = matches.iterator(); |
28 Match match = iterator.next(); | 28 Match match = iterator.next(); |
29 Expect.isFalse(iterator.hasNext); | 29 Expect.isFalse(iterator.hasNext); |
30 Expect.equals(str.indexOf('with', 0), match.start()); | 30 Expect.equals(str.indexOf('with', 0), match.start); |
31 Expect.equals(str.indexOf('with', 0) + helloPattern.length, match.end()); | 31 Expect.equals(str.indexOf('with', 0) + helloPattern.length, match.end); |
32 Expect.equals(helloPattern, match.pattern); | 32 Expect.equals(helloPattern, match.pattern); |
33 Expect.equals(str, match.str); | 33 Expect.equals(str, match.str); |
34 Expect.equals(helloPattern, match[0]); | 34 Expect.equals(helloPattern, match[0]); |
35 Expect.equals(0, match.groupCount()); | 35 Expect.equals(0, match.groupCount); |
36 } | 36 } |
37 | 37 |
38 testTwoMatches() { | 38 testTwoMatches() { |
39 String helloPattern = "hello"; | 39 String helloPattern = "hello"; |
40 Iterable<Match> matches = helloPattern.allMatches(str); | 40 Iterable<Match> matches = helloPattern.allMatches(str); |
41 | 41 |
42 int count = 0; | 42 int count = 0; |
43 int start = 0; | 43 int start = 0; |
44 for (var match in matches) { | 44 for (var match in matches) { |
45 count++; | 45 count++; |
46 Expect.equals(str.indexOf('hello', start), match.start()); | 46 Expect.equals(str.indexOf('hello', start), match.start); |
47 Expect.equals( | 47 Expect.equals( |
48 str.indexOf('hello', start) + helloPattern.length, match.end()); | 48 str.indexOf('hello', start) + helloPattern.length, match.end); |
49 Expect.equals(helloPattern, match.pattern); | 49 Expect.equals(helloPattern, match.pattern); |
50 Expect.equals(str, match.str); | 50 Expect.equals(str, match.str); |
51 Expect.equals(helloPattern, match[0]); | 51 Expect.equals(helloPattern, match[0]); |
52 Expect.equals(0, match.groupCount()); | 52 Expect.equals(0, match.groupCount); |
53 start = match.end(); | 53 start = match.end; |
54 } | 54 } |
55 Expect.equals(2, count); | 55 Expect.equals(2, count); |
56 } | 56 } |
57 | 57 |
58 testEmptyPattern() { | 58 testEmptyPattern() { |
59 String pattern = ""; | 59 String pattern = ""; |
60 Iterable<Match> matches = pattern.allMatches(str); | 60 Iterable<Match> matches = pattern.allMatches(str); |
61 Expect.isTrue(matches.iterator().hasNext); | 61 Expect.isTrue(matches.iterator().hasNext); |
62 } | 62 } |
63 | 63 |
64 testEmptyString() { | 64 testEmptyString() { |
65 String pattern = "foo"; | 65 String pattern = "foo"; |
66 String str = ""; | 66 String str = ""; |
67 Iterable<Match> matches = pattern.allMatches(str); | 67 Iterable<Match> matches = pattern.allMatches(str); |
68 Expect.isFalse(matches.iterator().hasNext); | 68 Expect.isFalse(matches.iterator().hasNext); |
69 } | 69 } |
70 | 70 |
71 testEmptyPatternAndString() { | 71 testEmptyPatternAndString() { |
72 String pattern = ""; | 72 String pattern = ""; |
73 String str = ""; | 73 String str = ""; |
74 Iterable<Match> matches = pattern.allMatches(str); | 74 Iterable<Match> matches = pattern.allMatches(str); |
75 Expect.isTrue(matches.iterator().hasNext); | 75 Expect.isTrue(matches.iterator().hasNext); |
76 } | 76 } |
OLD | NEW |