Debian Codegraph @

src/testlib/cockpittest.h

raw
1 /*
2 * Copyright (C) 2013 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include <glib.h>
7
8 #ifndef __COCKPIT_TEST_H__
9 #define __COCKPIT_TEST_H__
10
11 #include <json-glib/json-glib.h>
12
13 G_BEGIN_DECLS
14
15 #define COCKPIT_TEST_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
16
17 void cockpit_test_init (int *argc,
18 char ***argv);
19
20 void _cockpit_expect_logged_msg (const char *domain,
21 const char *file,
22 int line,
23 const char *func,
24 GLogLevelFlags log_level,
25 const gchar *pattern,
26 gboolean skipable,
27 gboolean optional);
28
29 #define cockpit_expect_log(domain, level, pattern) \
30 (_cockpit_expect_logged_msg ((domain), __FILE__, __LINE__, G_STRFUNC, (level), (pattern), FALSE, FALSE))
31
32 #define cockpit_expect_unordered_log(domain, level, pattern) \
33 (_cockpit_expect_logged_msg ((domain), __FILE__, __LINE__, G_STRFUNC, (level), (pattern), TRUE, FALSE))
34
35 #define cockpit_expect_possible_log(domain, level, pattern) \
36 (_cockpit_expect_logged_msg ((domain), __FILE__, __LINE__, G_STRFUNC, (level), (pattern), TRUE, TRUE))
37
38 #define cockpit_expect_warning(pattern) \
39 (_cockpit_expect_logged_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, G_LOG_LEVEL_WARNING, (pattern), FALSE, FALSE))
40
41 #define cockpit_expect_critical(pattern) \
42 (_cockpit_expect_logged_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, G_LOG_LEVEL_CRITICAL, (pattern), FALSE, FALSE))
43
44 #define cockpit_expect_message(pattern) \
45 (_cockpit_expect_logged_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, G_LOG_LEVEL_MESSAGE, (pattern), FALSE, FALSE))
46
47 #define cockpit_expect_info(pattern) \
48 (_cockpit_expect_logged_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, G_LOG_LEVEL_INFO, (pattern), FALSE, FALSE))
49
50 void cockpit_assert_expected (void);
51
52 void _cockpit_assert_strmatch_msg (const char *domain,
53 const char *file,
54 int line,
55 const char *func,
56 const gchar *string,
57 const gchar *pattern);
58
59 #define cockpit_assert_strmatch(str, pattern) \
60 (_cockpit_assert_strmatch_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, (str), (pattern)))
61
62 void _cockpit_assert_json_eq_msg (const char *domain,
63 const char *file,
64 int line,
65 const char *func,
66 gpointer object_or_array,
67 const gchar *json);
68
69 #define cockpit_assert_json_eq(obj_or_arr, json) \
70 (_cockpit_assert_json_eq_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, (obj_or_arr), (json)))
71
72 void _cockpit_assert_gvariant_eq_msg (const char *domain,
73 const char *file,
74 int line,
75 const char *func,
76 GVariant *actual,
77 const gchar *expected);
78
79 #define cockpit_assert_gvariant_eq(actual, expected) \
80 (_cockpit_assert_gvariant_eq_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, (actual), (expected)))
81
82 void _cockpit_assert_data_eq_msg (const char *domain,
83 const char *file,
84 int line,
85 const char *func,
86 gconstpointer data,
87 gssize len,
88 gconstpointer expect,
89 gssize exp_len);
90
91 #define cockpit_assert_data_eq(data, len, exp, elen) \
92 (_cockpit_assert_data_eq_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, (data), (len), (exp), (elen)))
93
94 void _cockpit_assert_bytes_eq_msg (const char *domain,
95 const char *file,
96 int line,
97 const char *func,
98 GBytes *data,
99 gconstpointer expect,
100 gssize exp_len);
101
102 #define cockpit_assert_bytes_eq(data, exp, len) \
103 (_cockpit_assert_bytes_eq_msg (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, (data), (exp), (len)))
104
105
106 void cockpit_test_signal_backtrace (int sig);
107
108 GInetAddress * cockpit_test_find_non_loopback_address (void);
109
110 void cockpit_test_allow_warnings (void);
111 void cockpit_test_reset_warnings (void);
112
113 void cockpit_test_setenv (const char *variable,
114 const char *value);
115
116 gboolean cockpit_test_skip_slow (void);
117
118 void cockpit_assertion_message_error_matches (const char *domain,
119 const char *file,
120 int line,
121 const char *func,
122 const char *expr,
123 const GError *error,
124 GQuark error_domain,
125 int error_code,
126 const char *error_pattern);
127 #define cockpit_assert_error_matches(err, dom, c, message_pattern) \
128 G_STMT_START { \
129 if ((err) == NULL || \
130 (dom != 0 && (error)->domain != dom) || \
131 (c != -1 && (error)->code != c) || \
132 (message_pattern && !g_pattern_match_simple (message_pattern, (err)->message))) \
133 cockpit_assertion_message_error_matches (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
134 #err, err, dom, c, message_pattern); \
135 } G_STMT_END
136
137
138 #define cockpit_test_add_full(Fixture, fixture, TestCase, testpath, function, ...) \
139 G_STMT_START { \
140 typedef void (*CockpitTestFixtureFunc) (Fixture *, const TestCase *); \
141 struct { \
142 CockpitTestFixtureFunc _setup; \
143 CockpitTestFixtureFunc _teardown; \
144 CockpitTestFixtureFunc _test; \
145 } _cockpit_test_vtable = { \
146 ._setup = fixture ## _setup, \
147 ._teardown = fixture ## _teardown, \
148 ._test = function, \
149 }; \
150 static const TestCase _cockpit_test_case = { \
151 __VA_ARGS__ \
152 }; \
153 g_test_add_vtable (testpath, sizeof (Fixture), &_cockpit_test_case, \
154 (GTestFixtureFunc) _cockpit_test_vtable._setup, \
155 (GTestFixtureFunc) _cockpit_test_vtable._test, \
156 (GTestFixtureFunc) _cockpit_test_vtable._teardown); \
157 } G_STMT_END
158
159 #define cockpit_test_add(testpath, function, ...) \
160 cockpit_test_add_full(Fixture, fixture, TestCase, testpath, function, __VA_ARGS__)
161
162 G_END_DECLS
163
164 #endif /* __COCKPIT_TEST_H__ */