1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/common/autofill_regexes.h"
// Keep these tests in sync with
// components/autofill/core/browser/pattern_provider/default_regex_patterns_unittest.cc
// These tests wil be superceded once the pattern provider launches.
#include <stddef.h>
#include <string>
#include <string_view>
#include "base/memory/ptr_util.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
bool MatchesRegex(std::u16string_view input,
std::u16string_view regex,
std::vector<std::u16string>* groups = nullptr) {
static base::NoDestructor<AutofillRegexCache> cache(ThreadSafe(true));
return autofill::MatchesRegex(input, *cache->GetRegexPattern(regex), groups);
}
std::optional<std::vector<std::u16string>> SplitByRegex(
std::u16string_view input,
std::string_view regex,
size_t max_groups) {
UErrorCode status = U_ZERO_ERROR;
std::unique_ptr<icu::RegexPattern> pattern = base::WrapUnique(
icu::RegexPattern::compile(icu::UnicodeString::fromUTF8(regex),
UREGEX_CASE_INSENSITIVE, status));
if (U_FAILURE(status)) {
return std::nullopt;
}
return autofill::SplitByRegex(input, *pattern, max_groups);
}
struct InputPatternTestCase {
const char16_t* const input;
const char16_t* const pattern;
};
class PositiveSampleTest : public testing::TestWithParam<InputPatternTestCase> {
};
TEST_P(PositiveSampleTest, SampleRegexes) {
auto test_case = GetParam();
SCOPED_TRACE(base::UTF16ToUTF8(test_case.input));
SCOPED_TRACE(base::UTF16ToUTF8(test_case.pattern));
EXPECT_TRUE(MatchesRegex(test_case.input, test_case.pattern));
}
INSTANTIATE_TEST_SUITE_P(AutofillRegexesTest,
PositiveSampleTest,
testing::Values(
// Empty pattern
InputPatternTestCase{u"", u""},
InputPatternTestCase{
u"Look, ma' -- a non-empty string!", u""},
// Substring
InputPatternTestCase{u"string", u"tri"},
// Substring at beginning
InputPatternTestCase{u"string", u"str"},
InputPatternTestCase{u"string", u"^str"},
// Substring at end
InputPatternTestCase{u"string", u"ring"},
InputPatternTestCase{u"string", u"ring$"},
// Case-insensitive
InputPatternTestCase{u"StRiNg", u"string"}));
class NegativeSampleTest : public testing::TestWithParam<InputPatternTestCase> {
};
TEST_P(NegativeSampleTest, SampleRegexes) {
auto test_case = GetParam();
SCOPED_TRACE(base::UTF16ToUTF8(test_case.input));
SCOPED_TRACE(base::UTF16ToUTF8(test_case.pattern));
EXPECT_FALSE(MatchesRegex(test_case.input, test_case.pattern));
}
INSTANTIATE_TEST_SUITE_P(AutofillRegexesTest,
NegativeSampleTest,
testing::Values(
// Empty string
InputPatternTestCase{
u"", u"Look, ma' -- a non-empty pattern!"},
// Substring
InputPatternTestCase{u"string", u"trn"},
// Substring at beginning
InputPatternTestCase{u"string", u" str"},
InputPatternTestCase{u"string", u"^tri"},
// Substring at end
InputPatternTestCase{u"string", u"ring "},
InputPatternTestCase{u"string", u"rin$"}));
// Tests for capture groups.
struct CapturePatternTestCase {
const char16_t* const input;
const char16_t* const pattern;
const bool matches;
const std::vector<std::u16string> groups;
};
class CaptureTest : public testing::TestWithParam<CapturePatternTestCase> {};
TEST_P(CaptureTest, SampleRegexes) {
auto test_case = GetParam();
std::vector<std::u16string> groups;
EXPECT_EQ(test_case.matches,
MatchesRegex(test_case.input, test_case.pattern, &groups));
EXPECT_THAT(groups, testing::Eq(test_case.groups));
}
INSTANTIATE_TEST_SUITE_P(
AutofillRegexes,
CaptureTest,
testing::Values(
// Find substrings in the input.
CapturePatternTestCase{u"Foo abcde Bar",
u"a(b+)c(d+)e",
true,
{u"abcde", u"b", u"d"}},
// Deal with optional capture groups.
CapturePatternTestCase{u"Foo acde Bar",
u"a(b+)?c(d+)e", // There is no b in the input.
true,
{u"acde", u"", u"d"}},
// Deal with non-matching capture groups.
CapturePatternTestCase{u"Foo acde Bar",
u"a(b+)c(d+)e", // There is no b in the input.
false,
{}}));
TEST(AutofillRegexes, SplitByRegex) {
EXPECT_EQ(SplitByRegex(u"이영 호", "[", 10), std::nullopt);
EXPECT_EQ(SplitByRegex(u"이영 호", " ", 10),
std::vector<std::u16string>({u"이영", u"호"}));
EXPECT_EQ(SplitByRegex(u"이영 호", " ", 1),
std::vector<std::u16string>({u"이영 호"}));
EXPECT_EQ(SplitByRegex(u"regex", " ", 10),
std::vector<std::u16string>({u"regex"}));
EXPECT_EQ(SplitByRegex(u"1 2 3", " ", 2),
std::vector<std::u16string>({u"1", u" 2 3"}));
EXPECT_EQ(SplitByRegex(u"", " ", 10), std::nullopt);
EXPECT_EQ(SplitByRegex(u" ", "\\s*", 10),
std::vector<std::u16string>({u"", u""}));
EXPECT_EQ(SplitByRegex(u"abcd", "\\s*", 10),
std::vector<std::u16string>({u"", u"a", u"b", u"c", u"d", u""}));
EXPECT_EQ(SplitByRegex(u"", "", 10), std::nullopt);
}
} // namespace
} // namespace autofill
|