OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. All rights reserved. |
| 2 // Copyright 2009 the V8 project authors. All rights reserved. |
| 3 // Redistribution and use in source and binary forms, with or without |
| 4 // modification, are permitted provided that the following conditions are |
| 5 // met: |
| 6 // |
| 7 // * Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. |
| 9 // * Redistributions in binary form must reproduce the above |
| 10 // copyright notice, this list of conditions and the following |
| 11 // disclaimer in the documentation and/or other materials provided |
| 12 // with the distribution. |
| 13 // * Neither the name of Google Inc. nor the names of its |
| 14 // contributors may be used to endorse or promote products derived |
| 15 // from this software without specific prior written permission. |
| 16 // |
| 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
| 29 import 'v8_regexp_utils.dart'; |
| 30 import 'package:expect/expect.dart'; |
| 31 |
| 32 void main() { |
| 33 // Tests captures in positive and negative look-ahead in regular expressions. |
| 34 |
| 35 dynamic testRE(re, input, expected_result) { |
| 36 if (expected_result) { |
| 37 assertTrue(re.hasMatch(input)); |
| 38 } else { |
| 39 assertFalse(re.hasMatch(input)); |
| 40 } |
| 41 } |
| 42 |
| 43 dynamic execRE(re, input, expected_result) { |
| 44 shouldBe(re.firstMatch(input), expected_result); |
| 45 } |
| 46 |
| 47 // Test of simple positive lookahead. |
| 48 |
| 49 var re = new RegExp(r"^(?=a)"); |
| 50 testRE(re, "a", true); |
| 51 testRE(re, "b", false); |
| 52 execRE(re, "a", [""]); |
| 53 |
| 54 re = new RegExp(r"^(?=\woo)f\w"); |
| 55 testRE(re, "foo", true); |
| 56 testRE(re, "boo", false); |
| 57 testRE(re, "fao", false); |
| 58 testRE(re, "foa", false); |
| 59 execRE(re, "foo", ["fo"]); |
| 60 |
| 61 re = new RegExp(r"(?=\w).(?=\W)"); |
| 62 testRE(re, ".a! ", true); |
| 63 testRE(re, ".! ", false); |
| 64 testRE(re, ".ab! ", true); |
| 65 execRE(re, ".ab! ", ["b"]); |
| 66 |
| 67 re = new RegExp(r"(?=f(?=[^f]o)).."); |
| 68 testRE(re, ", foo!", true); |
| 69 testRE(re, ", fo!", false); |
| 70 testRE(re, ", ffo", false); |
| 71 execRE(re, ", foo!", ["fo"]); |
| 72 |
| 73 // Positive lookahead with captures. |
| 74 re = new RegExp("^[^\'\"]*(?=([\'\"])).*\\1(\\w+)\\1"); |
| 75 testRE(re, " 'foo' ", true); |
| 76 testRE(re, ' "foo" ', true); |
| 77 testRE(re, " \" 'foo' ", false); |
| 78 testRE(re, " ' \"foo\" ", false); |
| 79 testRE(re, " 'foo\" ", false); |
| 80 testRE(re, " \"foo' ", false); |
| 81 execRE(re, " 'foo' ", [" 'foo'", "'", "foo"]); |
| 82 execRE(re, ' "foo" ', [' "foo"', '"', 'foo']); |
| 83 |
| 84 // Captures are cleared on backtrack past the look-ahead. |
| 85 re = new RegExp(r"^(?:(?=(.))a|b)\1$"); |
| 86 testRE(re, "aa", true); |
| 87 testRE(re, "b", true); |
| 88 testRE(re, "bb", false); |
| 89 testRE(re, "a", false); |
| 90 execRE(re, "aa", ["aa", "a"]); |
| 91 execRE(re, "b", ["b", null]); |
| 92 |
| 93 re = new RegExp(r"^(?=(.)(?=(.)\1\2)\2\1)\1\2"); |
| 94 testRE(re, "abab", true); |
| 95 testRE(re, "ababxxxxxxxx", true); |
| 96 testRE(re, "aba", false); |
| 97 execRE(re, "abab", ["ab", "a", "b"]); |
| 98 |
| 99 re = new RegExp(r"^(?:(?=(.))a|b|c)$"); |
| 100 testRE(re, "a", true); |
| 101 testRE(re, "b", true); |
| 102 testRE(re, "c", true); |
| 103 testRE(re, "d", false); |
| 104 execRE(re, "a", ["a", "a"]); |
| 105 execRE(re, "b", ["b", null]); |
| 106 execRE(re, "c", ["c", null]); |
| 107 |
| 108 execRE(new RegExp(r"^(?=(b))b"), "b", ["b", "b"]); |
| 109 execRE(new RegExp(r"^(?:(?=(b))|a)b"), "ab", ["ab", null]); |
| 110 execRE(new RegExp(r"^(?:(?=(b)(?:(?=(c))|d))|)bd"), "bd", ["bd", "b", null]); |
| 111 |
| 112 |
| 113 |
| 114 // Test of Negative Look-Ahead. |
| 115 |
| 116 re = new RegExp(r"(?!x)."); |
| 117 testRE(re, "y", true); |
| 118 testRE(re, "x", false); |
| 119 execRE(re, "y", ["y"]); |
| 120 |
| 121 re = new RegExp(r"(?!(\d))|\d"); |
| 122 testRE(re, "4", true); |
| 123 execRE(re, "4", ["4", null]); |
| 124 execRE(re, "x", ["", null]); |
| 125 |
| 126 |
| 127 // Test mixed nested look-ahead with captures. |
| 128 |
| 129 re = new RegExp(r"^(?=(x)(?=(y)))"); |
| 130 testRE(re, "xy", true); |
| 131 testRE(re, "xz", false); |
| 132 execRE(re, "xy", ["", "x", "y"]); |
| 133 |
| 134 re = new RegExp(r"^(?!(x)(?!(y)))"); |
| 135 testRE(re, "xy", true); |
| 136 testRE(re, "xz", false); |
| 137 execRE(re, "xy", ["", null, null]); |
| 138 |
| 139 re = new RegExp(r"^(?=(x)(?!(y)))"); |
| 140 testRE(re, "xz", true); |
| 141 testRE(re, "xy", false); |
| 142 execRE(re, "xz", ["", "x", null]); |
| 143 |
| 144 re = new RegExp(r"^(?!(x)(?=(y)))"); |
| 145 testRE(re, "xz", true); |
| 146 testRE(re, "xy", false); |
| 147 execRE(re, "xz", ["", null, null]); |
| 148 |
| 149 re = new RegExp(r"^(?=(x)(?!(y)(?=(z))))"); |
| 150 testRE(re, "xaz", true); |
| 151 testRE(re, "xya", true); |
| 152 testRE(re, "xyz", false); |
| 153 testRE(re, "a", false); |
| 154 execRE(re, "xaz", ["", "x", null, null]); |
| 155 execRE(re, "xya", ["", "x", null, null]); |
| 156 |
| 157 re = new RegExp(r"^(?!(x)(?=(y)(?!(z))))"); |
| 158 testRE(re, "a", true); |
| 159 testRE(re, "xa", true); |
| 160 testRE(re, "xyz", true); |
| 161 testRE(re, "xya", false); |
| 162 execRE(re, "a", ["", null, null, null]); |
| 163 execRE(re, "xa", ["", null, null, null]); |
| 164 execRE(re, "xyz", ["", null, null, null]); |
| 165 } |
OLD | NEW |