OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 'util.dart'; |
| 6 import 'package:expect/expect.dart'; |
| 7 |
| 8 void main() { |
| 9 // Tests captures in positive and negative look-ahead in regular expressions. |
| 10 |
| 11 dynamic testRE(re, input, expected_result) { |
| 12 if (expected_result) { |
| 13 assertTrue(re.hasMatch(input)); |
| 14 } else { |
| 15 assertFalse(re.hasMatch(input)); |
| 16 } |
| 17 } |
| 18 |
| 19 dynamic execRE(re, input, expected_result) { |
| 20 shouldBe(re.firstMatch(input), expected_result); |
| 21 } |
| 22 |
| 23 // Test of simple positive lookahead. |
| 24 |
| 25 var re = new RegExp(r"^(?=a)"); |
| 26 testRE(re, "a", true); |
| 27 testRE(re, "b", false); |
| 28 execRE(re, "a", [""]); |
| 29 |
| 30 re = new RegExp(r"^(?=\woo)f\w"); |
| 31 testRE(re, "foo", true); |
| 32 testRE(re, "boo", false); |
| 33 testRE(re, "fao", false); |
| 34 testRE(re, "foa", false); |
| 35 execRE(re, "foo", ["fo"]); |
| 36 |
| 37 re = new RegExp(r"(?=\w).(?=\W)"); |
| 38 testRE(re, ".a! ", true); |
| 39 testRE(re, ".! ", false); |
| 40 testRE(re, ".ab! ", true); |
| 41 execRE(re, ".ab! ", ["b"]); |
| 42 |
| 43 re = new RegExp(r"(?=f(?=[^f]o)).."); |
| 44 testRE(re, ", foo!", true); |
| 45 testRE(re, ", fo!", false); |
| 46 testRE(re, ", ffo", false); |
| 47 execRE(re, ", foo!", ["fo"]); |
| 48 |
| 49 // Positive lookahead with captures. |
| 50 re = new RegExp("^[^\'\"]*(?=([\'\"])).*\\1(\\w+)\\1"); |
| 51 testRE(re, " 'foo' ", true); |
| 52 testRE(re, ' "foo" ', true); |
| 53 testRE(re, " \" 'foo' ", false); |
| 54 testRE(re, " ' \"foo\" ", false); |
| 55 testRE(re, " 'foo\" ", false); |
| 56 testRE(re, " \"foo' ", false); |
| 57 execRE(re, " 'foo' ", [" 'foo'", "'", "foo"]); |
| 58 execRE(re, ' "foo" ', [' "foo"', '"', 'foo']); |
| 59 |
| 60 // Captures are cleared on backtrack past the look-ahead. |
| 61 re = new RegExp(r"^(?:(?=(.))a|b)\1$"); |
| 62 testRE(re, "aa", true); |
| 63 testRE(re, "b", true); |
| 64 testRE(re, "bb", false); |
| 65 testRE(re, "a", false); |
| 66 execRE(re, "aa", ["aa", "a"]); |
| 67 execRE(re, "b", ["b", null]); |
| 68 |
| 69 re = new RegExp(r"^(?=(.)(?=(.)\1\2)\2\1)\1\2"); |
| 70 testRE(re, "abab", true); |
| 71 testRE(re, "ababxxxxxxxx", true); |
| 72 testRE(re, "aba", false); |
| 73 execRE(re, "abab", ["ab", "a", "b"]); |
| 74 |
| 75 re = new RegExp(r"^(?:(?=(.))a|b|c)$"); |
| 76 testRE(re, "a", true); |
| 77 testRE(re, "b", true); |
| 78 testRE(re, "c", true); |
| 79 testRE(re, "d", false); |
| 80 execRE(re, "a", ["a", "a"]); |
| 81 execRE(re, "b", ["b", null]); |
| 82 execRE(re, "c", ["c", null]); |
| 83 |
| 84 execRE(new RegExp(r"^(?=(b))b"), "b", ["b", "b"]); |
| 85 execRE(new RegExp(r"^(?:(?=(b))|a)b"), "ab", ["ab", null]); |
| 86 execRE(new RegExp(r"^(?:(?=(b)(?:(?=(c))|d))|)bd"), "bd", ["bd", "b", null]); |
| 87 |
| 88 |
| 89 |
| 90 // Test of Negative Look-Ahead. |
| 91 |
| 92 re = new RegExp(r"(?!x)."); |
| 93 testRE(re, "y", true); |
| 94 testRE(re, "x", false); |
| 95 execRE(re, "y", ["y"]); |
| 96 |
| 97 re = new RegExp(r"(?!(\d))|\d"); |
| 98 testRE(re, "4", true); |
| 99 execRE(re, "4", ["4", null]); |
| 100 execRE(re, "x", ["", null]); |
| 101 |
| 102 |
| 103 // Test mixed nested look-ahead with captures. |
| 104 |
| 105 re = new RegExp(r"^(?=(x)(?=(y)))"); |
| 106 testRE(re, "xy", true); |
| 107 testRE(re, "xz", false); |
| 108 execRE(re, "xy", ["", "x", "y"]); |
| 109 |
| 110 re = new RegExp(r"^(?!(x)(?!(y)))"); |
| 111 testRE(re, "xy", true); |
| 112 testRE(re, "xz", false); |
| 113 execRE(re, "xy", ["", null, null]); |
| 114 |
| 115 re = new RegExp(r"^(?=(x)(?!(y)))"); |
| 116 testRE(re, "xz", true); |
| 117 testRE(re, "xy", false); |
| 118 execRE(re, "xz", ["", "x", null]); |
| 119 |
| 120 re = new RegExp(r"^(?!(x)(?=(y)))"); |
| 121 testRE(re, "xz", true); |
| 122 testRE(re, "xy", false); |
| 123 execRE(re, "xz", ["", null, null]); |
| 124 |
| 125 re = new RegExp(r"^(?=(x)(?!(y)(?=(z))))"); |
| 126 testRE(re, "xaz", true); |
| 127 testRE(re, "xya", true); |
| 128 testRE(re, "xyz", false); |
| 129 testRE(re, "a", false); |
| 130 execRE(re, "xaz", ["", "x", null, null]); |
| 131 execRE(re, "xya", ["", "x", null, null]); |
| 132 |
| 133 re = new RegExp(r"^(?!(x)(?=(y)(?!(z))))"); |
| 134 testRE(re, "a", true); |
| 135 testRE(re, "xa", true); |
| 136 testRE(re, "xyz", true); |
| 137 testRE(re, "xya", false); |
| 138 execRE(re, "a", ["", null, null, null]); |
| 139 execRE(re, "xa", ["", null, null, null]); |
| 140 execRE(re, "xyz", ["", null, null, null]); |
| 141 } |
OLD | NEW |