summaryrefslogtreecommitdiffstats
path: root/webkit/glue/regular_expression_unittest.cc
diff options
context:
space:
mode:
authorBen Murdoch <benm@google.com>2010-07-29 17:14:53 +0100
committerBen Murdoch <benm@google.com>2010-08-04 14:29:45 +0100
commitc407dc5cd9bdc5668497f21b26b09d988ab439de (patch)
tree7eaf8707c0309516bdb042ad976feedaf72b0bb1 /webkit/glue/regular_expression_unittest.cc
parent0998b1cdac5733f299c12d88bc31ef9c8035b8fa (diff)
downloadexternal_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.zip
external_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.tar.gz
external_chromium-c407dc5cd9bdc5668497f21b26b09d988ab439de.tar.bz2
Merge Chromium src@r53293
Change-Id: Ia79acf8670f385cee48c45b0a75371d8e950af34
Diffstat (limited to 'webkit/glue/regular_expression_unittest.cc')
-rw-r--r--webkit/glue/regular_expression_unittest.cc105
1 files changed, 105 insertions, 0 deletions
diff --git a/webkit/glue/regular_expression_unittest.cc b/webkit/glue/regular_expression_unittest.cc
new file mode 100644
index 0000000..39741f8
--- /dev/null
+++ b/webkit/glue/regular_expression_unittest.cc
@@ -0,0 +1,105 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebRegularExpression.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
+#include "third_party/WebKit/WebKit/chromium/public/WebTextCaseSensitivity.h"
+
+using namespace WebKit;
+
+namespace {
+
+class RegexTest : public testing::Test {
+};
+
+struct Match {
+ const WebUChar* text;
+ const int textLength;
+ const int matchPosition;
+ const int matchLength;
+};
+
+void testMatches(const WebRegularExpression& regex,
+ const Match* matches,
+ const size_t nMatches) {
+
+ for (size_t i = 0; i < nMatches; ++i) {
+ int matchedLength = matches[i].textLength;
+ EXPECT_EQ(matches[i].matchPosition, regex.match(
+ WebString(matches[i].text, matches[i].textLength), 0, &matchedLength));
+ if (matches[i].matchPosition != -1)
+ EXPECT_EQ(matches[i].matchLength, matchedLength);
+ }
+
+}
+
+} // namespace
+
+#define MATCH_DESC(webuchar, matchPosition, matchLength) \
+ { webuchar, arraysize(webuchar), matchPosition, matchLength }
+
+
+TEST(RegexTest, Basic) {
+ // Just make sure we're not completely broken.
+ WebRegularExpression regex("the quick brown fox", WebTextCaseSensitive);
+ EXPECT_EQ(0, regex.match("the quick brown fox"));
+ EXPECT_EQ(1, regex.match(" the quick brown fox"));
+ EXPECT_EQ(3, regex.match("foothe quick brown foxbar"));
+
+ EXPECT_EQ(-1, regex.match("The quick brown FOX"));
+ EXPECT_EQ(-1, regex.match("the quick brown fo"));
+}
+
+TEST(RegexTest, Unicode) {
+ // Make sure we get the right offsets for unicode strings.
+ WebUChar pattern[] = {L'\x6240', L'\x6709', L'\x7f51', L'\x9875'};
+ WebRegularExpression regex(WebString(pattern, arraysize(pattern)),
+ WebTextCaseInsensitive);
+
+ WebUChar text1[] = {L'\x6240', L'\x6709', L'\x7f51', L'\x9875'};
+ WebUChar text2[] = {L' ', L'\x6240', L'\x6709', L'\x7f51', L'\x9875'};
+ WebUChar text3[] = {L'f', L'o', L'o', L'\x6240', L'\x6709', L'\x7f51', L'\x9875', L'b', L'a', L'r'};
+ WebUChar text4[] = {L'\x4e2d', L'\x6587', L'\x7f51', L'\x9875', L'\x6240', L'\x6709', L'\x7f51', L'\x9875'};
+
+ const Match matches[] = {
+ MATCH_DESC(text1, 0, 4),
+ MATCH_DESC(text2, 1, 4),
+ MATCH_DESC(text3, 3, 4),
+ MATCH_DESC(text4, 4, 4),
+ };
+
+ testMatches(regex, matches, arraysize(matches));
+}
+
+TEST(RegexTest, UnicodeMixedLength) {
+ WebUChar pattern[] = {L':', L'[', L' ', L'\x2000', L']', L'+', L':'};
+ WebRegularExpression regex(WebString(pattern, arraysize(pattern)),
+ WebTextCaseInsensitive);
+
+ WebUChar text1[] = {L':', L' ', L' ', L':'};
+ WebUChar text2[] = {L' ', L' ', L':', L' ', L' ', L' ', L' ', L':', L' ', L' '};
+ WebUChar text3[] = {L' ', L':', L' ', L'\x2000', L' ', L':', L' '};
+ WebUChar text4[] = {L'\x6240', L'\x6709', L'\x7f51', L'\x9875', L' ', L':', L' ', L'\x2000', L' ', L'\x2000', L' ', L':', L' '};
+ WebUChar text5[] = {L' '};
+ WebUChar text6[] = {L':', L':'};
+
+ const Match matches[] = {
+ MATCH_DESC(text1, 0, 4),
+ MATCH_DESC(text2, 2, 6),
+ MATCH_DESC(text3, 1, 5),
+ MATCH_DESC(text4, 5, 7),
+ MATCH_DESC(text5, -1, -1),
+ MATCH_DESC(text6, -1, -1),
+ };
+
+ testMatches(regex, matches, arraysize(matches));
+}
+
+TEST(RegexTest, EmptyMatch) {
+ WebRegularExpression regex("|x", WebTextCaseInsensitive);
+ int matchedLength = 0;
+ EXPECT_EQ(0, regex.match("", 0, &matchedLength));
+ EXPECT_EQ(0, matchedLength);
+}