1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRegularExpression.h"
7 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCaseSensitivity.h"
10 using WebKit::WebRegularExpression
;
11 using WebKit::WebString
;
12 using WebKit::WebTextCaseInsensitive
;
13 using WebKit::WebTextCaseSensitive
;
14 using WebKit::WebUChar
;
18 class RegexTest
: public testing::Test
{
24 const int matchPosition
;
25 const int matchLength
;
28 void testMatches(const WebRegularExpression
& regex
,
30 const size_t nMatches
) {
32 for (size_t i
= 0; i
< nMatches
; ++i
) {
33 int matchedLength
= matches
[i
].textLength
;
34 EXPECT_EQ(matches
[i
].matchPosition
, regex
.match(
35 WebString(matches
[i
].text
, matches
[i
].textLength
), 0, &matchedLength
));
36 if (matches
[i
].matchPosition
!= -1)
37 EXPECT_EQ(matches
[i
].matchLength
, matchedLength
);
44 #define MATCH_DESC(webuchar, matchPosition, matchLength) \
45 { webuchar, arraysize(webuchar), matchPosition, matchLength }
48 TEST(RegexTest
, Basic
) {
49 // Just make sure we're not completely broken.
50 WebRegularExpression
regex("the quick brown fox", WebTextCaseSensitive
);
51 EXPECT_EQ(0, regex
.match("the quick brown fox"));
52 EXPECT_EQ(1, regex
.match(" the quick brown fox"));
53 EXPECT_EQ(3, regex
.match("foothe quick brown foxbar"));
55 EXPECT_EQ(-1, regex
.match("The quick brown FOX"));
56 EXPECT_EQ(-1, regex
.match("the quick brown fo"));
59 TEST(RegexTest
, Unicode
) {
60 // Make sure we get the right offsets for unicode strings.
61 WebUChar pattern
[] = {L
'\x6240', L
'\x6709', L
'\x7f51', L
'\x9875'};
62 WebRegularExpression
regex(WebString(pattern
, arraysize(pattern
)),
63 WebTextCaseInsensitive
);
65 WebUChar text1
[] = {L
'\x6240', L
'\x6709', L
'\x7f51', L
'\x9875'};
66 WebUChar text2
[] = {L
' ', L
'\x6240', L
'\x6709', L
'\x7f51', L
'\x9875'};
67 WebUChar text3
[] = {L
'f', L
'o', L
'o', L
'\x6240', L
'\x6709', L
'\x7f51', L
'\x9875', L
'b', L
'a', L
'r'};
68 WebUChar text4
[] = {L
'\x4e2d', L
'\x6587', L
'\x7f51', L
'\x9875', L
'\x6240', L
'\x6709', L
'\x7f51', L
'\x9875'};
70 const Match matches
[] = {
71 MATCH_DESC(text1
, 0, 4),
72 MATCH_DESC(text2
, 1, 4),
73 MATCH_DESC(text3
, 3, 4),
74 MATCH_DESC(text4
, 4, 4),
77 testMatches(regex
, matches
, arraysize(matches
));
80 TEST(RegexTest
, UnicodeMixedLength
) {
81 WebUChar pattern
[] = {L
':', L
'[', L
' ', L
'\x2000', L
']', L
'+', L
':'};
82 WebRegularExpression
regex(WebString(pattern
, arraysize(pattern
)),
83 WebTextCaseInsensitive
);
85 WebUChar text1
[] = {L
':', L
' ', L
' ', L
':'};
86 WebUChar text2
[] = {L
' ', L
' ', L
':', L
' ', L
' ', L
' ', L
' ', L
':', L
' ', L
' '};
87 WebUChar text3
[] = {L
' ', L
':', L
' ', L
'\x2000', L
' ', L
':', L
' '};
88 WebUChar text4
[] = {L
'\x6240', L
'\x6709', L
'\x7f51', L
'\x9875', L
' ', L
':', L
' ', L
'\x2000', L
' ', L
'\x2000', L
' ', L
':', L
' '};
89 WebUChar text5
[] = {L
' '};
90 WebUChar text6
[] = {L
':', L
':'};
92 const Match matches
[] = {
93 MATCH_DESC(text1
, 0, 4),
94 MATCH_DESC(text2
, 2, 6),
95 MATCH_DESC(text3
, 1, 5),
96 MATCH_DESC(text4
, 5, 7),
97 MATCH_DESC(text5
, -1, -1),
98 MATCH_DESC(text6
, -1, -1),
101 testMatches(regex
, matches
, arraysize(matches
));
104 TEST(RegexTest
, EmptyMatch
) {
105 WebRegularExpression
regex("|x", WebTextCaseInsensitive
);
106 int matchedLength
= 0;
107 EXPECT_EQ(0, regex
.match("", 0, &matchedLength
));
108 EXPECT_EQ(0, matchedLength
);