OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 // Some simple look-behind tests. |
| 30 |
| 31 import 'package:expect/expect.dart'; |
| 32 |
| 33 // Utility functions to easily port V8 tests. |
| 34 |
| 35 void assertTrue(actual, [String message = null]) { Expect.isTrue(actual, message
); } |
| 36 void assertFalse(actual, [String message = null]) { Expect.isFalse(actual, messa
ge); } |
| 37 |
| 38 void shouldBe(actual, expected, [String message = null]) { |
| 39 if (expected == null) { |
| 40 Expect.isNull(actual, message); |
| 41 } else { |
| 42 Expect.equals(expected.length, actual.groupCount + 1); |
| 43 for (int i = 0; i <= actual.groupCount; i++) { |
| 44 Expect.equals(expected[i], actual.group(i), message); |
| 45 } |
| 46 } |
| 47 } |
| 48 |
| 49 Match firstMatch(String str, RegExp pattern) => pattern.firstMatch(str); |
| 50 List<String> allStringMatches(String str, RegExp pattern) => |
| 51 pattern.allMatches(str).map((Match m) => m.group(0)).toList(); |
| 52 |
| 53 void main() { |
| 54 // Tests captures in positive and negative look-behind in regular expressions. |
| 55 |
| 56 dynamic testRE(re, input, expected_result) { |
| 57 if (expected_result) { |
| 58 assertTrue(re.hasMatch(input)); |
| 59 } else { |
| 60 assertFalse(re.hasMatch(input)); |
| 61 } |
| 62 } |
| 63 |
| 64 dynamic execRE(re, input, expected_result) { |
| 65 shouldBe(re.firstMatch(input), expected_result); |
| 66 } |
| 67 |
| 68 // Test of simple positive lookbehind. |
| 69 |
| 70 var re = new RegExp(r"^.(?<=a)"); |
| 71 testRE(re, "a", true); |
| 72 testRE(re, "b", false); |
| 73 execRE(re, "a", ["a"]); |
| 74 |
| 75 re = new RegExp(r"^f..(?<=.oo)"); |
| 76 testRE(re, "foo1", true); |
| 77 |
| 78 re = new RegExp(r"^f\w\w(?<=\woo)"); |
| 79 testRE(re, "foo2", true); |
| 80 testRE(re, "boo", false); |
| 81 testRE(re, "fao", false); |
| 82 testRE(re, "foa", false); |
| 83 execRE(re, "foo", ["foo"]); |
| 84 |
| 85 // Positive lookbehind with captures. |
| 86 re = new RegExp("(?<=(.))(\\w+)(?=\\1)"); |
| 87 testRE(re, " 'foo' ", true); |
| 88 testRE(re, ' "foo" ', true); |
| 89 testRE(re, ' .foo" ', false); |
| 90 execRE(re, " 'foo' ", ["foo", "'", "foo"]); |
| 91 execRE(re, ' "foo" ', ['foo', '"', 'foo']); |
| 92 |
| 93 // Alternations are tried in left to right order and we don't backtrack into |
| 94 // a lookbehind. |
| 95 re = new RegExp(r".*(?<=(..|...|....))(.*)"); |
| 96 testRE(re, "xabcd", true); |
| 97 execRE(re, "xabcd", ["xabcd", "cd", ""]); |
| 98 |
| 99 re = new RegExp(r".*(?<=(xx|...|....))(.*)"); |
| 100 testRE(re, "xabcd", true); |
| 101 execRE(re, "xabcd", ["xabcd", "bcd", ""]); |
| 102 |
| 103 // Misc. |
| 104 testRE(new RegExp("^foo(?<=foo)\$"), "foo", true); |
| 105 testRE(new RegExp("^f.o(?<=foo)\$"), "foo", true); |
| 106 testRE(new RegExp("^f.o(?<=foo)\$"), "fno", false); |
| 107 |
| 108 testRE(new RegExp("^foo(?<!foo)\$"), "foo", false); |
| 109 testRE(new RegExp("^f.o(?<!foo)\$"), "foo", false); |
| 110 testRE(new RegExp("^f.o(?<!foo)\$"), "fno", true); |
| 111 |
| 112 testRE(new RegExp("^foooo(?<=fo+)\$"), "foooo", true); |
| 113 testRE(new RegExp("^foooo(?<=^fo+)\$"), "foooo", true); |
| 114 testRE(new RegExp("^foooo(?<=^o+)\$"), "foooo", false); |
| 115 testRE(new RegExp("^foooo(?<=fo*)\$"), "foooo", true); |
| 116 testRE(new RegExp("^foooo(?<=^fo*)\$"), "foooo", true); |
| 117 testRE(new RegExp("^foooo(?<=^o*)\$"), "foooo", false); |
| 118 |
| 119 testRE(new RegExp("^faaao?(?<=^f[oa]+(?=o))"), "faaao", true); |
| 120 |
| 121 re = new RegExp(r"(.)(?<=\1\1\1)"); |
| 122 testRE(re, "ab", false); |
| 123 testRE(re, "abb", false); |
| 124 testRE(re, "abbb", true); |
| 125 re = new RegExp(r"(..)(?<=\1\1\1)"); |
| 126 testRE(re, "ab", false); |
| 127 testRE(re, "abb", false); |
| 128 testRE(re, "aabb", false); |
| 129 testRE(re, "abab", false); |
| 130 testRE(re, "fababab", true); |
| 131 testRE(re, "fabxbab", false); |
| 132 testRE(re, "faxabab", false); |
| 133 } |
OLD | NEW |