Debian Codegraph @

unittest/gunit/test_utils.h

raw symbols SCIP
1 /* Copyright (c) 2011, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #ifndef TEST_UTILS_INCLUDED
25 #define TEST_UTILS_INCLUDED
26
27 #include <string.h>
28 #include <sys/types.h>
29
30 #include "gtest/gtest.h"
31 #include "my_compiler.h"
32 #include "my_sys.h"
33 #include "mysql/components/services/bits/mysql_mutex_bits.h"
34 #include "sql/error_handler.h"
35 #include "sql/sql_error.h"
36
37 class THD;
38 class my_decimal;
39 struct MEM_ROOT;
40
41 namespace my_testing {
42
43 inline int native_compare(size_t *length, unsigned char **a,
44 unsigned char **b) {
45 return memcmp(*a, *b, *length);
46 }
47
48 inline qsort2_cmp get_ptr_compare(size_t size [[maybe_unused]]) {
49 return (qsort2_cmp)native_compare;
50 }
51
52 void setup_server_for_unit_tests();
53 void teardown_server_for_unit_tests();
54 int chars_2_decimal(const char *chars, my_decimal *to);
55
56 /*
57 A class which wraps the necessary setup/teardown logic for
58 unit tests which depend on a working THD environment.
59 */
60 class Server_initializer {
61 public:
62 Server_initializer() : m_thd(nullptr) {}
63 ~Server_initializer() { TearDown(); }
64
65 // Invoke these from corresponding functions in test fixture classes.
66 void SetUp();
67 void TearDown();
68
69 // Sets expected error for error_handler_hook.
70 static void set_expected_error(uint val);
71
72 THD *thd() const { return m_thd; }
73
74 private:
75 THD *m_thd;
76 };
77
78 /**
79 A mock error handler which registers itself with the THD in the CTOR,
80 and unregisters in the DTOR. The function handle_condition() will
81 verify that it is called with the expected error number.
82 The DTOR will verify that handle_condition() has actually been called.
83 */
84 class Mock_error_handler : public Internal_error_handler {
85 public:
86 Mock_error_handler(THD *thd, uint expected_error);
87 ~Mock_error_handler() override;
88
89 bool handle_condition(THD *thd, uint sql_errno, const char *sqlstate,
90 Sql_condition::enum_severity_level *level,
91 const char *msg) override;
92
93 int handle_called() const { return m_handle_called; }
94
95 private:
96 THD *m_thd;
97 uint m_expected_error;
98 int m_handle_called;
99 };
100
101 /*
102 A class which wraps the necessary setup/teardown logic for
103 Data Dictionary.
104 */
105 class DD_initializer {
106 public:
107 static void SetUp();
108 static void TearDown();
109 };
110
111 } // namespace my_testing
112
113 #endif // TEST_UTILS_INCLUDED