| 1 | /* |
| 2 | * Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2.0, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is designed to work with certain software (including |
| 9 | * but not limited to OpenSSL) that is licensed under separate terms, |
| 10 | * as designated in a particular file or component or in included license |
| 11 | * documentation. The authors of MySQL hereby grant you an additional |
| 12 | * permission to link the program and your derivative works with the |
| 13 | * separately licensed software that they have either included with |
| 14 | * the program or referenced in the documentation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License, version 2.0, for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <gmock/gmock.h> |
| 27 | |
| 28 | #include "plugin/x/src/xpl_regex.h" |
| 29 | |
| 30 | namespace xpl { |
| 31 | namespace test { |
| 32 | |
| 33 | struct Param_regex_match { |
| 34 | bool expect; |
| 35 | std::string value; |
| 36 | }; |
| 37 | |
| 38 | class Regex_match_test : public testing::TestWithParam<Param_regex_match> { |
| 39 | public: |
| 40 | const Regex m_re{"DECIMAL(?:\\([0-9]+(?:,[0-9]+)?\\))?"}; |
| 41 | }; |
| 42 | |
| 43 | TEST_P(Regex_match_test, regex_match) { |
| 44 | const auto ¶m = GetParam(); |
| 45 | EXPECT_EQ(param.expect, m_re.match(param.value.c_str())); |
| 46 | } |
| 47 | |
| 48 | Param_regex_match regex_match_param[] = { |
| 49 | {true, "DECIMAL"}, |
| 50 | {true, "decimal"}, |
| 51 | {true, "decimal(10)"}, |
| 52 | {true, "decimal(10,5)"}, |
| 53 | {false, "DEC"}, |
| 54 | {false, "decimal()"}, |
| 55 | {false, "decimal(abc)"}, |
| 56 | {false, "decimal(a,c)"}, |
| 57 | {false, "decimal(10,5,1)"}, |
| 58 | {false, "decimal(10)(5)"}, |
| 59 | {false, "decimal(10.5)"}, |
| 60 | {false, "(10,5)"}, |
| 61 | {false, " decimal(10,5)"}, |
| 62 | {false, "decimal (10,5)"}, |
| 63 | {false, "decimal( 10,5)"}, |
| 64 | {false, "decimal(10 ,5)"}, |
| 65 | {false, "decimal(10, 5)"}, |
| 66 | {false, "decimal(10,5 )"}, |
| 67 | {false, "decimal(10,5) "}, |
| 68 | }; |
| 69 | |
| 70 | INSTANTIATE_TEST_SUITE_P(regex_match_test, Regex_match_test, |
| 71 | testing::ValuesIn(regex_match_param)); |
| 72 | |
| 73 | struct Param_regex_match_groups { |
| 74 | std::vector<std::string> expect_groups; |
| 75 | std::string value; |
| 76 | }; |
| 77 | |
| 78 | class Regex_match_groups_test |
| 79 | : public testing::TestWithParam<Param_regex_match_groups> { |
| 80 | public: |
| 81 | const Regex m_re{ |
| 82 | "(INT)|" |
| 83 | "(CHAR|TEXT)(?:\\(([0-9]+)\\))?(?: CHARSET \\w+)?(?: COLLATE \\w+)?|" |
| 84 | "(DECIMAL)(?:\\(([0-9]+)(?:,([0-9]+))?\\))?|" |
| 85 | "\\w+(?:\\(([0-9]+)(?:,([0-9]+))?\\))?( UNSIGNED)?"}; |
| 86 | }; |
| 87 | |
| 88 | TEST_P(Regex_match_groups_test, regex_match_groups) { |
| 89 | const auto ¶m = GetParam(); |
| 90 | Regex::Group_list groups; |
| 91 | EXPECT_TRUE(m_re.match_groups(param.value.c_str(), &groups)); |
| 92 | EXPECT_EQ(param.expect_groups, groups); |
| 93 | } |
| 94 | |
| 95 | Param_regex_match_groups regex_match_groups_param[] = { |
| 96 | {{"int", "int"}, "int"}, |
| 97 | {{"char(5)", "char", "5"}, "char(5)"}, |
| 98 | {{"char", "char"}, "char"}, |
| 99 | {{"text(64)", "text", "64"}, "text(64)"}, |
| 100 | {{"text", "text"}, "text"}, |
| 101 | {{"decimal(10,5)", "decimal", "10", "5"}, "decimal(10,5)"}, |
| 102 | {{"decimal(10)", "decimal", "10"}, "decimal(10)"}, |
| 103 | {{"decimal", "decimal"}, "decimal"}, |
| 104 | {{"qwe(10,5) unsigned", "10", "5", " unsigned"}, "qwe(10,5) unsigned"}, |
| 105 | {{"qwe(10) unsigned", "10", " unsigned"}, "qwe(10) unsigned"}, |
| 106 | {{"char(20) charset latin1", "char", "20"}, "char(20) charset latin1"}, |
| 107 | {{"text(30) charset latin1 collate latin1_bin", "text", "30"}, |
| 108 | "text(30) charset latin1 collate latin1_bin"}, |
| 109 | }; |
| 110 | |
| 111 | INSTANTIATE_TEST_SUITE_P(regex_match_groups_test, Regex_match_groups_test, |
| 112 | testing::ValuesIn(regex_match_groups_param)); |
| 113 | |
| 114 | } // namespace test |
| 115 | } // namespace xpl |