| 1 | /* |
| 2 | * Copyright (c) 2019, 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 <gtest/gtest.h> |
| 27 | |
| 28 | #include "plugin/x/src/capabilities/set_variable_adaptor.h" |
| 29 | |
| 30 | namespace xpl { |
| 31 | namespace test { |
| 32 | |
| 33 | const ulonglong k_label_a = 1 << 0; |
| 34 | const ulonglong k_label_b = 1 << 1; |
| 35 | const ulonglong k_label_c = 1 << 2; |
| 36 | const ulonglong k_label_zero = 0; |
| 37 | |
| 38 | class Set_variable_adaptor_test : public ::testing::Test { |
| 39 | public: |
| 40 | void set_variable_value(ulonglong val) { *m_variable.value() = val; } |
| 41 | Set_variable m_variable{{"LABEL_A", "LABEL_B", "LABEL_C"}}; |
| 42 | enum class Labels { k_B, k_C, k_A }; |
| 43 | Set_variable_adaptor<Labels> m_adaptor{ |
| 44 | m_variable, {Labels::k_A, Labels::k_B, Labels::k_C}}; |
| 45 | }; |
| 46 | |
| 47 | TEST_F(Set_variable_adaptor_test, get_value) { |
| 48 | EXPECT_EQ(Labels::k_A, m_adaptor.get_value("label_a")); |
| 49 | EXPECT_EQ(Labels::k_B, m_adaptor.get_value("label_b")); |
| 50 | EXPECT_EQ(Labels::k_C, m_adaptor.get_value("label_c")); |
| 51 | } |
| 52 | |
| 53 | TEST_F(Set_variable_adaptor_test, get_allowed_values_all) { |
| 54 | set_variable_value(k_label_a + k_label_b + k_label_c); |
| 55 | std::vector<std::string> result, expect{"label_a", "label_b", "label_c"}; |
| 56 | m_adaptor.get_allowed_values(&result); |
| 57 | EXPECT_EQ(expect, result); |
| 58 | } |
| 59 | |
| 60 | TEST_F(Set_variable_adaptor_test, get_allowed_values_some) { |
| 61 | set_variable_value(k_label_a + k_label_c); |
| 62 | std::vector<std::string> result, expect{"label_a", "label_c"}; |
| 63 | m_adaptor.get_allowed_values(&result); |
| 64 | EXPECT_EQ(expect, result); |
| 65 | } |
| 66 | |
| 67 | struct Param_is_allowed_value { |
| 68 | ulonglong m_value; |
| 69 | bool m_label_a, m_label_b, m_label_c; |
| 70 | }; |
| 71 | |
| 72 | class Set_variable_adaptor_is_allowed_value_test |
| 73 | : public Set_variable_adaptor_test, |
| 74 | public testing::WithParamInterface<Param_is_allowed_value> {}; |
| 75 | |
| 76 | TEST_P(Set_variable_adaptor_is_allowed_value_test, is_allowed_value) { |
| 77 | const auto ¶m = GetParam(); |
| 78 | set_variable_value(param.m_value); |
| 79 | EXPECT_EQ(param.m_label_a, m_adaptor.is_allowed_value("label_a")); |
| 80 | EXPECT_EQ(param.m_label_b, m_adaptor.is_allowed_value("label_b")); |
| 81 | EXPECT_EQ(param.m_label_c, m_adaptor.is_allowed_value("label_c")); |
| 82 | EXPECT_FALSE(m_adaptor.is_allowed_value("label_d")); |
| 83 | } |
| 84 | |
| 85 | Param_is_allowed_value is_allowed_value_param[] = { |
| 86 | {k_label_zero, false, false, false}, |
| 87 | {k_label_a, true, false, false}, |
| 88 | {k_label_b, false, true, false}, |
| 89 | {k_label_c, false, false, true}, |
| 90 | {k_label_a + k_label_b, true, true, false}, |
| 91 | {k_label_a + k_label_c, true, false, true}, |
| 92 | {k_label_b + k_label_c, false, true, true}, |
| 93 | {k_label_a + k_label_b + k_label_c, true, true, true}, |
| 94 | }; |
| 95 | |
| 96 | INSTANTIATE_TEST_SUITE_P(Set_variable_adaptor_is_allowed_value, |
| 97 | Set_variable_adaptor_is_allowed_value_test, |
| 98 | testing::ValuesIn(is_allowed_value_param)); |
| 99 | |
| 100 | } // namespace test |
| 101 | } // namespace xpl |