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

Side by Side Diff: components/autofill/core/browser/form_field_unittest.cc

Issue 1453193002: autofill: switch autofill_regexes to RE2 library (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: another try Created 5 years 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_vector.h" 5 #include "base/memory/scoped_vector.h"
6 #include "base/strings/string16.h" 6 #include "base/strings/string16.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "components/autofill/core/browser/autofill_field.h" 8 #include "components/autofill/core/browser/autofill_field.h"
9 #include "components/autofill/core/browser/form_field.h" 9 #include "components/autofill/core/browser/form_field.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 using base::ASCIIToUTF16; 12 using base::ASCIIToUTF16;
13 13
14 namespace autofill { 14 namespace autofill {
15 15
16 TEST(FormFieldTest, Match) { 16 TEST(FormFieldTest, Match) {
17 AutofillField field; 17 AutofillField field;
18 18
19 // Empty strings match. 19 // Empty strings match.
20 EXPECT_TRUE(FormField::Match(&field, base::string16(), 20 EXPECT_TRUE(FormField::Match(&field, std::string(), FormField::MATCH_LABEL));
21 FormField::MATCH_LABEL));
22 21
23 // Empty pattern matches non-empty string. 22 // Empty pattern matches non-empty string.
24 field.label = ASCIIToUTF16("a"); 23 field.label = ASCIIToUTF16("a");
25 EXPECT_TRUE(FormField::Match(&field, base::string16(), 24 EXPECT_TRUE(FormField::Match(&field, std::string(), FormField::MATCH_LABEL));
26 FormField::MATCH_LABEL));
27 25
28 // Strictly empty pattern matches empty string. 26 // Strictly empty pattern matches empty string.
29 field.label = base::string16(); 27 field.label = base::string16();
30 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^$"), 28 EXPECT_TRUE(FormField::Match(&field, "^$", FormField::MATCH_LABEL));
31 FormField::MATCH_LABEL));
32 29
33 // Strictly empty pattern does not match non-empty string. 30 // Strictly empty pattern does not match non-empty string.
34 field.label = ASCIIToUTF16("a"); 31 field.label = ASCIIToUTF16("a");
35 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^$"), 32 EXPECT_FALSE(FormField::Match(&field, "^$", FormField::MATCH_LABEL));
36 FormField::MATCH_LABEL));
37 33
38 // Non-empty pattern doesn't match empty string. 34 // Non-empty pattern doesn't match empty string.
39 field.label = base::string16(); 35 field.label = base::string16();
40 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("a"), 36 EXPECT_FALSE(FormField::Match(&field, "a", FormField::MATCH_LABEL));
41 FormField::MATCH_LABEL));
42 37
43 // Beginning of line. 38 // Beginning of line.
44 field.label = ASCIIToUTF16("head_tail"); 39 field.label = ASCIIToUTF16("head_tail");
45 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head"), 40 EXPECT_TRUE(FormField::Match(&field, "^head", FormField::MATCH_LABEL));
46 FormField::MATCH_LABEL)); 41 EXPECT_FALSE(FormField::Match(&field, "^tail", FormField::MATCH_LABEL));
47 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail"),
48 FormField::MATCH_LABEL));
49 42
50 // End of line. 43 // End of line.
51 field.label = ASCIIToUTF16("head_tail"); 44 field.label = ASCIIToUTF16("head_tail");
52 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head$"), 45 EXPECT_FALSE(FormField::Match(&field, "head$", FormField::MATCH_LABEL));
53 FormField::MATCH_LABEL)); 46 EXPECT_TRUE(FormField::Match(&field, "tail$", FormField::MATCH_LABEL));
54 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail$"),
55 FormField::MATCH_LABEL));
56 47
57 // Exact. 48 // Exact.
58 field.label = ASCIIToUTF16("head_tail"); 49 field.label = ASCIIToUTF16("head_tail");
59 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^head$"), 50 EXPECT_FALSE(FormField::Match(&field, "^head$", FormField::MATCH_LABEL));
60 FormField::MATCH_LABEL)); 51 EXPECT_FALSE(FormField::Match(&field, "^tail$", FormField::MATCH_LABEL));
61 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("^tail$"), 52 EXPECT_TRUE(FormField::Match(&field, "^head_tail$", FormField::MATCH_LABEL));
62 FormField::MATCH_LABEL));
63 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("^head_tail$"),
64 FormField::MATCH_LABEL));
65 53
66 // Escaped dots. 54 // Escaped dots.
67 field.label = ASCIIToUTF16("m.i."); 55 field.label = ASCIIToUTF16("m.i.");
68 // Note: This pattern is misleading as the "." characters are wild cards. 56 // Note: This pattern is misleading as the "." characters are wild cards.
69 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), 57 EXPECT_TRUE(FormField::Match(&field, "m.i.", FormField::MATCH_LABEL));
70 FormField::MATCH_LABEL)); 58 EXPECT_TRUE(FormField::Match(&field, "m\\.i\\.", FormField::MATCH_LABEL));
71 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."),
72 FormField::MATCH_LABEL));
73 field.label = ASCIIToUTF16("mXiX"); 59 field.label = ASCIIToUTF16("mXiX");
74 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("m.i."), 60 EXPECT_TRUE(FormField::Match(&field, "m.i.", FormField::MATCH_LABEL));
75 FormField::MATCH_LABEL)); 61 EXPECT_FALSE(FormField::Match(&field, "m\\.i\\.", FormField::MATCH_LABEL));
76 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("m\\.i\\."),
77 FormField::MATCH_LABEL));
78 62
79 // Repetition. 63 // Repetition.
80 field.label = ASCIIToUTF16("headtail"); 64 field.label = ASCIIToUTF16("headtail");
81 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 65 EXPECT_TRUE(FormField::Match(&field, "head.*tail", FormField::MATCH_LABEL));
82 FormField::MATCH_LABEL));
83 field.label = ASCIIToUTF16("headXtail"); 66 field.label = ASCIIToUTF16("headXtail");
84 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 67 EXPECT_TRUE(FormField::Match(&field, "head.*tail", FormField::MATCH_LABEL));
85 FormField::MATCH_LABEL));
86 field.label = ASCIIToUTF16("headXXXtail"); 68 field.label = ASCIIToUTF16("headXXXtail");
87 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.*tail"), 69 EXPECT_TRUE(FormField::Match(&field, "head.*tail", FormField::MATCH_LABEL));
88 FormField::MATCH_LABEL));
89 field.label = ASCIIToUTF16("headtail"); 70 field.label = ASCIIToUTF16("headtail");
90 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 71 EXPECT_FALSE(FormField::Match(&field, "head.+tail", FormField::MATCH_LABEL));
91 FormField::MATCH_LABEL));
92 field.label = ASCIIToUTF16("headXtail"); 72 field.label = ASCIIToUTF16("headXtail");
93 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 73 EXPECT_TRUE(FormField::Match(&field, "head.+tail", FormField::MATCH_LABEL));
94 FormField::MATCH_LABEL));
95 field.label = ASCIIToUTF16("headXXXtail"); 74 field.label = ASCIIToUTF16("headXXXtail");
96 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head.+tail"), 75 EXPECT_TRUE(FormField::Match(&field, "head.+tail", FormField::MATCH_LABEL));
97 FormField::MATCH_LABEL));
98 76
99 // Alternation. 77 // Alternation.
100 field.label = ASCIIToUTF16("head_tail"); 78 field.label = ASCIIToUTF16("head_tail");
101 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head|other"), 79 EXPECT_TRUE(FormField::Match(&field, "head|other", FormField::MATCH_LABEL));
102 FormField::MATCH_LABEL)); 80 EXPECT_TRUE(FormField::Match(&field, "tail|other", FormField::MATCH_LABEL));
103 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("tail|other"), 81 EXPECT_FALSE(FormField::Match(&field, "bad|good", FormField::MATCH_LABEL));
104 FormField::MATCH_LABEL));
105 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("bad|good"),
106 FormField::MATCH_LABEL));
107 82
108 // Case sensitivity. 83 // Case sensitivity.
109 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx"); 84 field.label = ASCIIToUTF16("xxxHeAd_tAiLxxx");
110 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("head_tail"), 85 EXPECT_TRUE(FormField::Match(&field, "head_tail", FormField::MATCH_LABEL));
111 FormField::MATCH_LABEL));
112 86
113 // Word boundaries. 87 // Word boundaries.
114 field.label = ASCIIToUTF16("contains word:"); 88 field.label = ASCIIToUTF16("contains word:");
115 EXPECT_TRUE(FormField::Match(&field, ASCIIToUTF16("\\bword\\b"), 89 EXPECT_TRUE(FormField::Match(&field, "(\\A|\\z|\\PL)word(\\A|\\z|\\PL)",
Ilya Sherman 2015/12/07 23:49:25 nit: Please define a variable like const string k
tfarina 2015/12/08 12:46:27 Done.
116 FormField::MATCH_LABEL)); 90 FormField::MATCH_LABEL));
117 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcon\\b"), 91 EXPECT_FALSE(FormField::Match(&field, "(\\A|\\z|\\PL)con(\\A|\\z|\\PL)",
118 FormField::MATCH_LABEL)); 92 FormField::MATCH_LABEL));
119 // Make sure the circumflex in 'crepe' is not treated as a word boundary. 93 // Make sure the circumflex in 'crepe' is not treated as a word boundary.
120 field.label = base::UTF8ToUTF16("cr" "\xC3\xAA" "pe"); 94 field.label = base::UTF8ToUTF16("cr" "\xC3\xAA" "pe");
121 EXPECT_FALSE(FormField::Match(&field, ASCIIToUTF16("\\bcr\\b"), 95 EXPECT_FALSE(FormField::Match(&field, "(\\A|\\z|\\PL)cr(\\A|\\z|\\PL)",
122 FormField::MATCH_LABEL)); 96 FormField::MATCH_LABEL));
123 } 97 }
124 98
125 // Test that we ignore checkable elements. 99 // Test that we ignore checkable elements.
126 TEST(FormFieldTest, ParseFormFields) { 100 TEST(FormFieldTest, ParseFormFields) {
127 ScopedVector<AutofillField> fields; 101 ScopedVector<AutofillField> fields;
128 FormFieldData field_data; 102 FormFieldData field_data;
129 field_data.form_control_type = "text"; 103 field_data.form_control_type = "text";
130 104
131 field_data.label = ASCIIToUTF16("Address line1"); 105 field_data.label = ASCIIToUTF16("Address line1");
(...skipping 22 matching lines...) Expand all
154 FormField::ParseFormFields(fields.get(), true, &field_type_map); 128 FormField::ParseFormFields(fields.get(), true, &field_type_map);
155 ASSERT_EQ(3U, field_type_map.size()); 129 ASSERT_EQ(3U, field_type_map.size());
156 130
157 EXPECT_EQ(ADDRESS_HOME_LINE1, 131 EXPECT_EQ(ADDRESS_HOME_LINE1,
158 field_type_map.find(ASCIIToUTF16("Address line1"))->second); 132 field_type_map.find(ASCIIToUTF16("Address line1"))->second);
159 EXPECT_EQ(ADDRESS_HOME_LINE2, 133 EXPECT_EQ(ADDRESS_HOME_LINE2,
160 field_type_map.find(ASCIIToUTF16("Address line2"))->second); 134 field_type_map.find(ASCIIToUTF16("Address line2"))->second);
161 } 135 }
162 136
163 } // namespace autofill 137 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/form_field.cc ('k') | components/autofill/core/browser/name_field.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698