| OLD | NEW |
| 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 "components/autofill/core/common/autofill_regexes.h" | 5 #include "components/autofill/core/common/autofill_regexes.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "components/autofill/core/browser/autofill_regex_constants.h" | 12 #include "components/autofill/core/browser/autofill_regex_constants.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using base::ASCIIToUTF16; | 15 using base::ASCIIToUTF16; |
| 16 | 16 |
| 17 namespace autofill { | 17 namespace autofill { |
| 18 | 18 |
| 19 TEST(AutofillRegexesTest, SampleRegexes) { | 19 struct InputPatternTestCase { |
| 20 struct TestCase { | 20 const char* const input; |
| 21 const char* const input; | 21 const char* const pattern; |
| 22 const char* const pattern; | |
| 23 }; | 22 }; |
| 24 | 23 |
| 25 const TestCase kPositiveCases[] = { | 24 class PositiveSampleTest |
| 26 // Empty pattern | 25 : public testing::TestWithParam<InputPatternTestCase> {}; |
| 27 {"", ""}, | 26 |
| 28 {"Look, ma' -- a non-empty string!", ""}, | 27 TEST_P(PositiveSampleTest, SampleRegexes) { |
| 29 // Substring | 28 auto test_case = GetParam(); |
| 30 {"string", "tri"}, | |
| 31 // Substring at beginning | |
| 32 {"string", "str"}, | |
| 33 {"string", "^str"}, | |
| 34 // Substring at end | |
| 35 {"string", "ring"}, | |
| 36 {"string", "ring$"}, | |
| 37 // Case-insensitive | |
| 38 {"StRiNg", "string"}, | |
| 39 }; | |
| 40 for (const auto& test_case : kPositiveCases) { | |
| 41 SCOPED_TRACE(test_case.input); | 29 SCOPED_TRACE(test_case.input); |
| 42 SCOPED_TRACE(test_case.pattern); | 30 SCOPED_TRACE(test_case.pattern); |
| 43 EXPECT_TRUE(MatchesPattern(ASCIIToUTF16(test_case.input), | 31 EXPECT_TRUE(MatchesPattern(ASCIIToUTF16(test_case.input), |
| 44 ASCIIToUTF16(test_case.pattern))); | 32 ASCIIToUTF16(test_case.pattern))); |
| 45 } | 33 } |
| 46 | 34 |
| 47 const TestCase kNegativeCases[] = { | 35 INSTANTIATE_TEST_CASE_P(AutofillRegexes, |
| 48 // Empty string | 36 PositiveSampleTest, |
| 49 {"", "Look, ma' -- a non-empty pattern!"}, | 37 testing::Values( |
| 50 // Substring | 38 // Empty pattern |
| 51 {"string", "trn"}, | 39 InputPatternTestCase{"", ""}, |
| 52 // Substring at beginning | 40 InputPatternTestCase{ |
| 53 {"string", " str"}, | 41 "Look, ma' -- a non-empty string!", ""}, |
| 54 {"string", "^tri"}, | 42 // Substring |
| 55 // Substring at end | 43 InputPatternTestCase{"string", "tri"}, |
| 56 {"string", "ring "}, | 44 // Substring at beginning |
| 57 {"string", "rin$"}, | 45 InputPatternTestCase{"string", "str"}, |
| 58 }; | 46 InputPatternTestCase{"string", "^str"}, |
| 59 for (const auto& test_case : kNegativeCases) { | 47 // Substring at end |
| 48 InputPatternTestCase{"string", "ring"}, |
| 49 InputPatternTestCase{"string", "ring$"}, |
| 50 // Case-insensitive |
| 51 InputPatternTestCase{"StRiNg", "string"})); |
| 52 |
| 53 class NegativeSampleTest |
| 54 : public testing::TestWithParam<InputPatternTestCase> {}; |
| 55 |
| 56 TEST_P(NegativeSampleTest, SampleRegexes) { |
| 57 auto test_case = GetParam(); |
| 60 SCOPED_TRACE(test_case.input); | 58 SCOPED_TRACE(test_case.input); |
| 61 SCOPED_TRACE(test_case.pattern); | 59 SCOPED_TRACE(test_case.pattern); |
| 62 EXPECT_FALSE(MatchesPattern(ASCIIToUTF16(test_case.input), | 60 EXPECT_FALSE(MatchesPattern(ASCIIToUTF16(test_case.input), |
| 63 ASCIIToUTF16(test_case.pattern))); | 61 ASCIIToUTF16(test_case.pattern))); |
| 64 } | |
| 65 } | 62 } |
| 66 | 63 |
| 67 TEST(AutofillRegexesTest, ExpirationDate2DigitYearRegexes) { | 64 INSTANTIATE_TEST_CASE_P(AutofillRegexes, |
| 68 struct TestCase { | 65 NegativeSampleTest, |
| 69 const char* const input; | 66 testing::Values( |
| 67 // Empty string |
| 68 InputPatternTestCase{ |
| 69 "", "Look, ma' -- a non-empty pattern!"}, |
| 70 // Substring |
| 71 InputPatternTestCase{"string", "trn"}, |
| 72 // Substring at beginning |
| 73 InputPatternTestCase{"string", " str"}, |
| 74 InputPatternTestCase{"string", "^tri"}, |
| 75 // Substring at end |
| 76 InputPatternTestCase{"string", "ring "}, |
| 77 InputPatternTestCase{"string", "rin$"})); |
| 78 |
| 79 struct InputTestCase { |
| 80 const char* const input; |
| 70 }; | 81 }; |
| 71 | 82 |
| 72 const base::string16 pattern = ASCIIToUTF16(kExpirationDate2DigitYearRe); | 83 class ExpirationDate2DigitYearPositive |
| 73 | 84 : public testing::TestWithParam<InputTestCase> {}; |
| 74 const TestCase kPositiveCases[] = { | 85 |
| 75 // Simple two year cases | 86 TEST_P(ExpirationDate2DigitYearPositive, ExpirationDate2DigitYearRegexes) { |
| 76 {"mm / yy"}, | 87 auto test_case = GetParam(); |
| 77 {"mm/ yy"}, | 88 SCOPED_TRACE(test_case.input); |
| 78 {"mm /yy"}, | 89 const base::string16 pattern = ASCIIToUTF16(kExpirationDate2DigitYearRe); |
| 79 {"mm/yy"}, | 90 EXPECT_TRUE(MatchesPattern(ASCIIToUTF16(test_case.input), pattern)); |
| 80 {"mm - yy"}, | 91 } |
| 81 {"mm- yy"}, | 92 |
| 82 {"mm -yy"}, | 93 INSTANTIATE_TEST_CASE_P( |
| 83 {"mm-yy"}, | 94 AutofillRegexes, |
| 84 {"mmyy"}, | 95 ExpirationDate2DigitYearPositive, |
| 85 // Complex two year cases | 96 testing::Values(InputTestCase{"mm / yy"}, |
| 86 {"Expiration Date (MM / YY)"}, | 97 InputTestCase{"mm/ yy"}, |
| 87 {"Expiration Date (MM/YY)"}, | 98 InputTestCase{"mm /yy"}, |
| 88 {"Expiration Date (MM - YY)"}, | 99 InputTestCase{"mm/yy"}, |
| 89 {"Expiration Date (MM-YY)"}, | 100 InputTestCase{"mm - yy"}, |
| 90 {"Expiration Date MM / YY"}, | 101 InputTestCase{"mm- yy"}, |
| 91 {"Expiration Date MM/YY"}, | 102 InputTestCase{"mm -yy"}, |
| 92 {"Expiration Date MM - YY"}, | 103 InputTestCase{"mm-yy"}, |
| 93 {"Expiration Date MM-YY"}, | 104 InputTestCase{"mmyy"}, |
| 94 {"expiration date yy"}, | 105 // Complex two year cases |
| 95 {"Exp Date (MM / YY)"}, | 106 InputTestCase{"Expiration Date (MM / YY)"}, |
| 96 }; | 107 InputTestCase{"Expiration Date (MM/YY)"}, |
| 97 | 108 InputTestCase{"Expiration Date (MM - YY)"}, |
| 98 for (const auto& test_case : kPositiveCases) { | 109 InputTestCase{"Expiration Date (MM-YY)"}, |
| 99 SCOPED_TRACE(test_case.input); | 110 InputTestCase{"Expiration Date MM / YY"}, |
| 100 EXPECT_TRUE(MatchesPattern(ASCIIToUTF16(test_case.input),pattern)); | 111 InputTestCase{"Expiration Date MM/YY"}, |
| 101 } | 112 InputTestCase{"Expiration Date MM - YY"}, |
| 102 | 113 InputTestCase{"Expiration Date MM-YY"}, |
| 103 const TestCase kNegativeCases[] = { | 114 InputTestCase{"expiration date yy"}, |
| 104 {""}, | 115 InputTestCase{"Exp Date (MM / YY)"})); |
| 105 {"Look, ma' -- an invalid string!"}, | 116 |
| 106 {"mmfavouritewordyy"}, | 117 class ExpirationDate2DigitYearNegative |
| 107 {"mm a yy"}, | 118 : public testing::TestWithParam<InputTestCase> {}; |
| 108 {"mm a yyyy"}, | 119 |
| 109 // Simple four year cases | 120 TEST_P(ExpirationDate2DigitYearNegative, ExpirationDate2DigitYearRegexes) { |
| 110 {"mm / yyyy"}, | 121 auto test_case = GetParam(); |
| 111 {"mm/ yyyy"}, | 122 SCOPED_TRACE(test_case.input); |
| 112 {"mm /yyyy"}, | 123 const base::string16 pattern = ASCIIToUTF16(kExpirationDate2DigitYearRe); |
| 113 {"mm/yyyy"}, | |
| 114 {"mm - yyyy"}, | |
| 115 {"mm- yyyy"}, | |
| 116 {"mm -yyyy"}, | |
| 117 {"mm-yyyy"}, | |
| 118 {"mmyyyy"}, | |
| 119 // Complex four year cases | |
| 120 {"Expiration Date (MM / YYYY)"}, | |
| 121 {"Expiration Date (MM/YYYY)"}, | |
| 122 {"Expiration Date (MM - YYYY)"}, | |
| 123 {"Expiration Date (MM-YYYY)"}, | |
| 124 {"Expiration Date MM / YYYY"}, | |
| 125 {"Expiration Date MM/YYYY"}, | |
| 126 {"Expiration Date MM - YYYY"}, | |
| 127 {"Expiration Date MM-YYYY"}, | |
| 128 {"expiration date yyyy"}, | |
| 129 {"Exp Date (MM / YYYY)"}, | |
| 130 }; | |
| 131 | |
| 132 for (const auto& test_case : kNegativeCases) { | |
| 133 SCOPED_TRACE(test_case.input); | |
| 134 EXPECT_FALSE(MatchesPattern(ASCIIToUTF16(test_case.input), pattern)); | 124 EXPECT_FALSE(MatchesPattern(ASCIIToUTF16(test_case.input), pattern)); |
| 135 } | 125 } |
| 126 |
| 127 INSTANTIATE_TEST_CASE_P( |
| 128 AutofillRegexes, |
| 129 ExpirationDate2DigitYearNegative, |
| 130 testing::Values(InputTestCase{""}, |
| 131 InputTestCase{"Look, ma' -- an invalid string!"}, |
| 132 InputTestCase{"mmfavouritewordyy"}, |
| 133 InputTestCase{"mm a yy"}, |
| 134 InputTestCase{"mm a yyyy"}, |
| 135 // Simple four year cases |
| 136 InputTestCase{"mm / yyyy"}, |
| 137 InputTestCase{"mm/ yyyy"}, |
| 138 InputTestCase{"mm /yyyy"}, |
| 139 InputTestCase{"mm/yyyy"}, |
| 140 InputTestCase{"mm - yyyy"}, |
| 141 InputTestCase{"mm- yyyy"}, |
| 142 InputTestCase{"mm -yyyy"}, |
| 143 InputTestCase{"mm-yyyy"}, |
| 144 InputTestCase{"mmyyyy"}, |
| 145 // Complex four year cases |
| 146 InputTestCase{"Expiration Date (MM / YYYY)"}, |
| 147 InputTestCase{"Expiration Date (MM/YYYY)"}, |
| 148 InputTestCase{"Expiration Date (MM - YYYY)"}, |
| 149 InputTestCase{"Expiration Date (MM-YYYY)"}, |
| 150 InputTestCase{"Expiration Date MM / YYYY"}, |
| 151 InputTestCase{"Expiration Date MM/YYYY"}, |
| 152 InputTestCase{"Expiration Date MM - YYYY"}, |
| 153 InputTestCase{"Expiration Date MM-YYYY"}, |
| 154 InputTestCase{"expiration date yyyy"}, |
| 155 InputTestCase{"Exp Date (MM / YYYY)"})); |
| 156 |
| 157 class ExpirationDate4DigitYearPositive |
| 158 : public testing::TestWithParam<InputTestCase> {}; |
| 159 |
| 160 TEST_P(ExpirationDate4DigitYearPositive, ExpirationDate4DigitYearRegexes) { |
| 161 auto test_case = GetParam(); |
| 162 const base::string16 pattern = ASCIIToUTF16(kExpirationDate4DigitYearRe); |
| 163 SCOPED_TRACE(test_case.input); |
| 164 EXPECT_TRUE(MatchesPattern(ASCIIToUTF16(test_case.input), pattern)); |
| 165 } |
| 166 |
| 167 INSTANTIATE_TEST_CASE_P(AutofillRegexes, |
| 168 ExpirationDate4DigitYearPositive, |
| 169 testing::Values( |
| 170 // Simple four year cases |
| 171 InputTestCase{"mm / yyyy"}, |
| 172 InputTestCase{"mm/ yyyy"}, |
| 173 InputTestCase{"mm /yyyy"}, |
| 174 InputTestCase{"mm/yyyy"}, |
| 175 InputTestCase{"mm - yyyy"}, |
| 176 InputTestCase{"mm- yyyy"}, |
| 177 InputTestCase{"mm -yyyy"}, |
| 178 InputTestCase{"mm-yyyy"}, |
| 179 InputTestCase{"mmyyyy"}, |
| 180 // Complex four year cases |
| 181 InputTestCase{"Expiration Date (MM / YYYY)"}, |
| 182 InputTestCase{"Expiration Date (MM/YYYY)"}, |
| 183 InputTestCase{"Expiration Date (MM - YYYY)"}, |
| 184 InputTestCase{"Expiration Date (MM-YYYY)"}, |
| 185 InputTestCase{"Expiration Date MM / YYYY"}, |
| 186 InputTestCase{"Expiration Date MM/YYYY"}, |
| 187 InputTestCase{"Expiration Date MM - YYYY"}, |
| 188 InputTestCase{"Expiration Date MM-YYYY"}, |
| 189 InputTestCase{"expiration date yyyy"}, |
| 190 InputTestCase{"Exp Date (MM / YYYY)"})); |
| 191 |
| 192 class ExpirationDate4DigitYearNegative |
| 193 : public testing::TestWithParam<InputTestCase> {}; |
| 194 |
| 195 TEST_P(ExpirationDate4DigitYearNegative, ExpirationDate4DigitYearRegexes) { |
| 196 auto test_case = GetParam(); |
| 197 const base::string16 pattern = ASCIIToUTF16(kExpirationDate4DigitYearRe); |
| 198 SCOPED_TRACE(test_case.input); |
| 199 EXPECT_FALSE(MatchesPattern(ASCIIToUTF16(test_case.input), pattern)); |
| 136 } | 200 } |
| 137 | 201 |
| 138 TEST(AutofillRegexesTest, ExpirationDate4DigitYearRegexes) { | 202 INSTANTIATE_TEST_CASE_P( |
| 139 struct TestCase { | 203 AutofillRegexes, |
| 140 const char* const input; | 204 ExpirationDate4DigitYearNegative, |
| 141 }; | 205 testing::Values(InputTestCase{""}, |
| 142 | 206 InputTestCase{"Look, ma' -- an invalid string!"}, |
| 143 const base::string16 pattern = ASCIIToUTF16(kExpirationDate4DigitYearRe); | 207 InputTestCase{"mmfavouritewordyy"}, |
| 144 | 208 InputTestCase{"mm a yy"}, |
| 145 const TestCase kPositiveCases[] = { | 209 InputTestCase{"mm a yyyy"}, |
| 146 // Simple four year cases | 210 // Simple two year cases |
| 147 {"mm / yyyy"}, | 211 InputTestCase{"mm / yy"}, |
| 148 {"mm/ yyyy"}, | 212 InputTestCase{"mm/ yy"}, |
| 149 {"mm /yyyy"}, | 213 InputTestCase{"mm /yy"}, |
| 150 {"mm/yyyy"}, | 214 InputTestCase{"mm/yy"}, |
| 151 {"mm - yyyy"}, | 215 InputTestCase{"mm - yy"}, |
| 152 {"mm- yyyy"}, | 216 InputTestCase{"mm- yy"}, |
| 153 {"mm -yyyy"}, | 217 InputTestCase{"mm -yy"}, |
| 154 {"mm-yyyy"}, | 218 InputTestCase{"mm-yy"}, |
| 155 {"mmyyyy"}, | 219 InputTestCase{"mmyy"}, |
| 156 // Complex four year cases | 220 // Complex two year cases |
| 157 {"Expiration Date (MM / YYYY)"}, | 221 InputTestCase{"Expiration Date (MM / YY)"}, |
| 158 {"Expiration Date (MM/YYYY)"}, | 222 InputTestCase{"Expiration Date (MM/YY)"}, |
| 159 {"Expiration Date (MM - YYYY)"}, | 223 InputTestCase{"Expiration Date (MM - YY)"}, |
| 160 {"Expiration Date (MM-YYYY)"}, | 224 InputTestCase{"Expiration Date (MM-YY)"}, |
| 161 {"Expiration Date MM / YYYY"}, | 225 InputTestCase{"Expiration Date MM / YY"}, |
| 162 {"Expiration Date MM/YYYY"}, | 226 InputTestCase{"Expiration Date MM/YY"}, |
| 163 {"Expiration Date MM - YYYY"}, | 227 InputTestCase{"Expiration Date MM - YY"}, |
| 164 {"Expiration Date MM-YYYY"}, | 228 InputTestCase{"Expiration Date MM-YY"}, |
| 165 {"expiration date yyyy"}, | 229 InputTestCase{"expiration date yy"}, |
| 166 {"Exp Date (MM / YYYY)"}, | 230 InputTestCase{"Exp Date (MM / YY)"})); |
| 167 }; | |
| 168 | |
| 169 for (const auto& test_case : kPositiveCases) { | |
| 170 SCOPED_TRACE(test_case.input); | |
| 171 EXPECT_TRUE(MatchesPattern(ASCIIToUTF16(test_case.input),pattern)); | |
| 172 } | |
| 173 | |
| 174 const TestCase kNegativeCases[] = { | |
| 175 {""}, | |
| 176 {"Look, ma' -- an invalid string!"}, | |
| 177 {"mmfavouritewordyy"}, | |
| 178 {"mm a yy"}, | |
| 179 {"mm a yyyy"}, | |
| 180 // Simple two year cases | |
| 181 {"mm / yy"}, | |
| 182 {"mm/ yy"}, | |
| 183 {"mm /yy"}, | |
| 184 {"mm/yy"}, | |
| 185 {"mm - yy"}, | |
| 186 {"mm- yy"}, | |
| 187 {"mm -yy"}, | |
| 188 {"mm-yy"}, | |
| 189 {"mmyy"}, | |
| 190 // Complex two year cases | |
| 191 {"Expiration Date (MM / YY)"}, | |
| 192 {"Expiration Date (MM/YY)"}, | |
| 193 {"Expiration Date (MM - YY)"}, | |
| 194 {"Expiration Date (MM-YY)"}, | |
| 195 {"Expiration Date MM / YY"}, | |
| 196 {"Expiration Date MM/YY"}, | |
| 197 {"Expiration Date MM - YY"}, | |
| 198 {"Expiration Date MM-YY"}, | |
| 199 {"expiration date yy"}, | |
| 200 {"Exp Date (MM / YY)"}, | |
| 201 }; | |
| 202 | |
| 203 for (const auto& test_case : kNegativeCases) { | |
| 204 SCOPED_TRACE(test_case.input); | |
| 205 EXPECT_FALSE(MatchesPattern(ASCIIToUTF16(test_case.input), pattern)); | |
| 206 } | |
| 207 } | |
| 208 | 231 |
| 209 } // namespace autofill | 232 } // namespace autofill |
| OLD | NEW |