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 // Dart test for testing String.allMatches. |
| 5 |
| 6 import "package:expect/expect.dart"; |
| 7 |
| 8 String str = "this is a string with hello here and hello there"; |
| 9 |
| 10 main() { |
| 11 testNoMatch(); |
| 12 testOneMatch(); |
| 13 testTwoMatches(); |
| 14 testEmptyPattern(); |
| 15 testEmptyString(); |
| 16 testEmptyPatternAndString(); |
| 17 testMatchAsPrefix(); |
| 18 testAllMatchesStart(); |
| 19 } |
| 20 |
| 21 testNoMatch() { |
| 22 // Also tests that RegExp groups don't work. |
| 23 String helloPattern = "with (hello)"; |
| 24 Iterable<Match> matches = helloPattern.allMatches(str); |
| 25 Expect.isFalse(matches.iterator.moveNext()); |
| 26 } |
| 27 |
| 28 testOneMatch() { |
| 29 String helloPattern = "with hello"; |
| 30 Iterable<Match> matches = helloPattern.allMatches(str); |
| 31 var iterator = matches.iterator; |
| 32 Expect.isTrue(iterator.moveNext()); |
| 33 Match match = iterator.current; |
| 34 Expect.isFalse(iterator.moveNext()); |
| 35 Expect.equals(str.indexOf('with', 0), match.start); |
| 36 Expect.equals(str.indexOf('with', 0) + helloPattern.length, match.end); |
| 37 Expect.equals(helloPattern, match.pattern); |
| 38 Expect.equals(str, match.input); |
| 39 Expect.equals(helloPattern, match[0]); |
| 40 Expect.equals(0, match.groupCount); |
| 41 } |
| 42 |
| 43 testTwoMatches() { |
| 44 String helloPattern = "hello"; |
| 45 Iterable<Match> matches = helloPattern.allMatches(str); |
| 46 |
| 47 int count = 0; |
| 48 int start = 0; |
| 49 for (var match in matches) { |
| 50 count++; |
| 51 Expect.equals(str.indexOf('hello', start), match.start); |
| 52 Expect.equals( |
| 53 str.indexOf('hello', start) + helloPattern.length, match.end); |
| 54 Expect.equals(helloPattern, match.pattern); |
| 55 Expect.equals(str, match.input); |
| 56 Expect.equals(helloPattern, match[0]); |
| 57 Expect.equals(0, match.groupCount); |
| 58 start = match.end; |
| 59 } |
| 60 Expect.equals(2, count); |
| 61 } |
| 62 |
| 63 testEmptyPattern() { |
| 64 String pattern = ""; |
| 65 Iterable<Match> matches = pattern.allMatches(str); |
| 66 Expect.isTrue(matches.iterator.moveNext()); |
| 67 } |
| 68 |
| 69 testEmptyString() { |
| 70 String pattern = "foo"; |
| 71 String str = ""; |
| 72 Iterable<Match> matches = pattern.allMatches(str); |
| 73 Expect.isFalse(matches.iterator.moveNext()); |
| 74 } |
| 75 |
| 76 testEmptyPatternAndString() { |
| 77 String pattern = ""; |
| 78 String str = ""; |
| 79 Iterable<Match> matches = pattern.allMatches(str); |
| 80 Expect.isTrue(matches.iterator.moveNext()); |
| 81 } |
| 82 |
| 83 testMatchAsPrefix() { |
| 84 String pattern = "an"; |
| 85 String str = "banana"; |
| 86 Expect.isNull(pattern.matchAsPrefix(str)); |
| 87 Expect.isNull(pattern.matchAsPrefix(str, 0)); |
| 88 var m = pattern.matchAsPrefix(str, 1); |
| 89 Expect.equals("an", m[0]); |
| 90 Expect.equals(1, m.start); |
| 91 Expect.isNull(pattern.matchAsPrefix(str, 2)); |
| 92 m = pattern.matchAsPrefix(str, 3); |
| 93 Expect.equals("an", m[0]); |
| 94 Expect.equals(3, m.start); |
| 95 Expect.isNull(pattern.matchAsPrefix(str, 4)); |
| 96 Expect.isNull(pattern.matchAsPrefix(str, 5)); |
| 97 Expect.isNull(pattern.matchAsPrefix(str, 6)); |
| 98 Expect.throws(() => pattern.matchAsPrefix(str, -1)); |
| 99 Expect.throws(() => pattern.matchAsPrefix(str, 7)); |
| 100 } |
| 101 |
| 102 testAllMatchesStart() { |
| 103 String p = "ass"; |
| 104 String s = "assassin"; |
| 105 Expect.equals(2, p.allMatches(s).length); |
| 106 Expect.equals(2, p.allMatches(s, 0).length); |
| 107 Expect.equals(1, p.allMatches(s, 1).length); |
| 108 Expect.equals(0, p.allMatches(s, 4).length); |
| 109 Expect.equals(0, p.allMatches(s, s.length).length); |
| 110 Expect.throws(() => p.allMatches(s, -1)); |
| 111 Expect.throws(() => p.allMatches(s, s.length + 1)); |
| 112 } |
OLD | NEW |