Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Unified Diff: tests/unsorted/lookbehind_test.dart

Issue 1398033002: Implement lookbehind in regexp engine Base URL: git@github.com:dart-lang/fletch.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/core/regexp.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/unsorted/lookbehind_test.dart
diff --git a/tests/unsorted/lookbehind_test.dart b/tests/unsorted/lookbehind_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..bfea05b7e5c0056f611e4a572adeddef7ff040dd
--- /dev/null
+++ b/tests/unsorted/lookbehind_test.dart
@@ -0,0 +1,133 @@
+// Copyright (c) 2015, the Dart project authors. All rights reserved.
+// Copyright 2009 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Some simple look-behind tests.
+
+import 'package:expect/expect.dart';
+
+// Utility functions to easily port V8 tests.
+
+void assertTrue(actual, [String message = null]) { Expect.isTrue(actual, message); }
+void assertFalse(actual, [String message = null]) { Expect.isFalse(actual, message); }
+
+void shouldBe(actual, expected, [String message = null]) {
+ if (expected == null) {
+ Expect.isNull(actual, message);
+ } else {
+ Expect.equals(expected.length, actual.groupCount + 1);
+ for (int i = 0; i <= actual.groupCount; i++) {
+ Expect.equals(expected[i], actual.group(i), message);
+ }
+ }
+}
+
+Match firstMatch(String str, RegExp pattern) => pattern.firstMatch(str);
+List<String> allStringMatches(String str, RegExp pattern) =>
+ pattern.allMatches(str).map((Match m) => m.group(0)).toList();
+
+void main() {
+ // Tests captures in positive and negative look-behind in regular expressions.
+
+ dynamic testRE(re, input, expected_result) {
+ if (expected_result) {
+ assertTrue(re.hasMatch(input));
+ } else {
+ assertFalse(re.hasMatch(input));
+ }
+ }
+
+ dynamic execRE(re, input, expected_result) {
+ shouldBe(re.firstMatch(input), expected_result);
+ }
+
+ // Test of simple positive lookbehind.
+
+ var re = new RegExp(r"^.(?<=a)");
+ testRE(re, "a", true);
+ testRE(re, "b", false);
+ execRE(re, "a", ["a"]);
+
+ re = new RegExp(r"^f..(?<=.oo)");
+ testRE(re, "foo1", true);
+
+ re = new RegExp(r"^f\w\w(?<=\woo)");
+ testRE(re, "foo2", true);
+ testRE(re, "boo", false);
+ testRE(re, "fao", false);
+ testRE(re, "foa", false);
+ execRE(re, "foo", ["foo"]);
+
+ // Positive lookbehind with captures.
+ re = new RegExp("(?<=(.))(\\w+)(?=\\1)");
+ testRE(re, " 'foo' ", true);
+ testRE(re, ' "foo" ', true);
+ testRE(re, ' .foo" ', false);
+ execRE(re, " 'foo' ", ["foo", "'", "foo"]);
+ execRE(re, ' "foo" ', ['foo', '"', 'foo']);
+
+ // Alternations are tried in left to right order and we don't backtrack into
+ // a lookbehind.
+ re = new RegExp(r".*(?<=(..|...|....))(.*)");
+ testRE(re, "xabcd", true);
+ execRE(re, "xabcd", ["xabcd", "cd", ""]);
+
+ re = new RegExp(r".*(?<=(xx|...|....))(.*)");
+ testRE(re, "xabcd", true);
+ execRE(re, "xabcd", ["xabcd", "bcd", ""]);
+
+ // Misc.
+ testRE(new RegExp("^foo(?<=foo)\$"), "foo", true);
+ testRE(new RegExp("^f.o(?<=foo)\$"), "foo", true);
+ testRE(new RegExp("^f.o(?<=foo)\$"), "fno", false);
+
+ testRE(new RegExp("^foo(?<!foo)\$"), "foo", false);
+ testRE(new RegExp("^f.o(?<!foo)\$"), "foo", false);
+ testRE(new RegExp("^f.o(?<!foo)\$"), "fno", true);
+
+ testRE(new RegExp("^foooo(?<=fo+)\$"), "foooo", true);
+ testRE(new RegExp("^foooo(?<=^fo+)\$"), "foooo", true);
+ testRE(new RegExp("^foooo(?<=^o+)\$"), "foooo", false);
+ testRE(new RegExp("^foooo(?<=fo*)\$"), "foooo", true);
+ testRE(new RegExp("^foooo(?<=^fo*)\$"), "foooo", true);
+ testRE(new RegExp("^foooo(?<=^o*)\$"), "foooo", false);
+
+ testRE(new RegExp("^faaao?(?<=^f[oa]+(?=o))"), "faaao", true);
+
+ re = new RegExp(r"(.)(?<=\1\1\1)");
+ testRE(re, "ab", false);
+ testRE(re, "abb", false);
+ testRE(re, "abbb", true);
+ re = new RegExp(r"(..)(?<=\1\1\1)");
+ testRE(re, "ab", false);
+ testRE(re, "abb", false);
+ testRE(re, "aabb", false);
+ testRE(re, "abab", false);
+ testRE(re, "fababab", true);
+ testRE(re, "fabxbab", false);
+ testRE(re, "faxabab", false);
+}
« no previous file with comments | « lib/core/regexp.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698