Skip to content

Commit 711ddd0

Browse files
committed
Low: libcrmcommon: Convert unit test assertion helpers to macros
With a function, the CMocka assert macros showed unhelpful line numbers on error. Ref T222 Signed-off-by: Reid Wahl <[email protected]>
1 parent 6727da3 commit 711ddd0

31 files changed

+657
-680
lines changed

lib/common/tests/cmdline/pcmk__quote_cmdline_test.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
* says "The quoting style used is undefined (single or double quotes may be
1919
* used)."
2020
*/
21-
static void
22-
assert_quote_cmdline(const char **argv, const gchar *expected_single,
23-
const gchar *expected_double)
24-
{
25-
gchar *processed = pcmk__quote_cmdline((const char *const *) argv);
26-
27-
assert_true(pcmk__str_any_of(processed, expected_single, expected_double,
28-
NULL));
29-
g_free(processed);
30-
}
21+
#define assert_quote_cmdline(argv, expected_single, expected_double) \
22+
do { \
23+
gchar *processed = pcmk__quote_cmdline(argv); \
24+
\
25+
assert_true(pcmk__str_any_of(processed, expected_single, \
26+
expected_double, NULL)); \
27+
g_free(processed); \
28+
} while (0)
3129

3230
static void
3331
empty_input(void **state)

lib/common/tests/iso8601/crm_time_add_days_test.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@
1616

1717
#include <crm/common/iso8601.h>
1818

19-
static void
20-
assert_add_days(const char *orig_date_time, int days,
21-
const char *expected_date_time)
22-
{
23-
crm_time_t *orig = crm_time_new(orig_date_time);
24-
crm_time_t *expected = crm_time_new(expected_date_time);
25-
26-
assert_non_null(orig);
27-
assert_non_null(expected);
28-
29-
crm_time_add_days(orig, days);
30-
assert_int_equal(crm_time_compare(orig, expected), 0);
31-
32-
crm_time_free(orig);
33-
crm_time_free(expected);
34-
}
19+
#define assert_add_days(orig_date_time, days, expected_date_time) \
20+
do { \
21+
crm_time_t *orig = crm_time_new(orig_date_time); \
22+
crm_time_t *expected = crm_time_new(expected_date_time); \
23+
\
24+
assert_non_null(orig); \
25+
assert_non_null(expected); \
26+
\
27+
crm_time_add_days(orig, days); \
28+
assert_int_equal(crm_time_compare(orig, expected), 0); \
29+
\
30+
crm_time_free(orig); \
31+
crm_time_free(expected); \
32+
} while (0)
3533

3634
static void
3735
invalid_argument(void **state)

lib/common/tests/iso8601/crm_time_add_seconds_test.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 the Pacemaker project contributors
2+
* Copyright 2024-2025 the Pacemaker project contributors
33
*
44
* The version control history for this file may have further details.
55
*
@@ -16,22 +16,20 @@
1616

1717
#include <crm/common/iso8601.h>
1818

19-
static void
20-
assert_add_seconds(const char *orig_date_time, int seconds,
21-
const char *expected_date_time)
22-
{
23-
crm_time_t *orig = crm_time_new(orig_date_time);
24-
crm_time_t *expected = crm_time_new(expected_date_time);
25-
26-
assert_non_null(orig);
27-
assert_non_null(expected);
28-
29-
crm_time_add_seconds(orig, seconds);
30-
assert_int_equal(crm_time_compare(orig, expected), 0);
31-
32-
crm_time_free(orig);
33-
crm_time_free(expected);
34-
}
19+
#define assert_add_seconds(orig_date_time, seconds, expected_date_time) \
20+
do { \
21+
crm_time_t *orig = crm_time_new(orig_date_time); \
22+
crm_time_t *expected = crm_time_new(expected_date_time); \
23+
\
24+
assert_non_null(orig); \
25+
assert_non_null(expected); \
26+
\
27+
crm_time_add_seconds(orig, seconds); \
28+
assert_int_equal(crm_time_compare(orig, expected), 0); \
29+
\
30+
crm_time_free(orig); \
31+
crm_time_free(expected); \
32+
} while (0)
3533

3634
static void
3735
invalid_argument(void **state)

lib/common/tests/iso8601/crm_time_add_years_test.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@
1616

1717
#include <crm/common/iso8601.h>
1818

19-
static void
20-
assert_add_years(const char *orig_date_time, int years,
21-
const char *expected_date_time)
22-
{
23-
crm_time_t *orig = crm_time_new(orig_date_time);
24-
crm_time_t *expected = crm_time_new(expected_date_time);
25-
26-
assert_non_null(orig);
27-
assert_non_null(expected);
28-
29-
crm_time_add_years(orig, years);
30-
assert_int_equal(crm_time_compare(orig, expected), 0);
31-
32-
crm_time_free(orig);
33-
crm_time_free(expected);
34-
}
19+
#define assert_add_years(orig_date_time, years, expected_date_time) \
20+
do { \
21+
crm_time_t *orig = crm_time_new(orig_date_time); \
22+
crm_time_t *expected = crm_time_new(expected_date_time); \
23+
\
24+
assert_non_null(orig); \
25+
assert_non_null(expected); \
26+
\
27+
crm_time_add_years(orig, years); \
28+
assert_int_equal(crm_time_compare(orig, expected), 0); \
29+
\
30+
crm_time_free(orig); \
31+
crm_time_free(expected); \
32+
} while (0)
3533

3634
static void
3735
invalid_argument(void **state)

lib/common/tests/iso8601/pcmk__time_format_hr_test.c

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -30,83 +30,78 @@
3030
* \internal
3131
* \brief Check that \c pcmk__time_format_hr() produced expected result
3232
*
33-
* \param[in] format Time format string
34-
* \param[in] expected Check succeeds if result matches this
33+
* \param[in] format Time format string (<tt>const char *</tt>)
34+
* \param[in] expected Succeed if result matches this (<tt>const char *</tt>)
3535
*/
36-
static void
37-
assert_hr_format(const char *format, const char *expected)
38-
{
39-
crm_time_t *dt = NULL;
40-
char *result = NULL;
41-
42-
dt = crm_time_new(DATE_TIME_S);
43-
assert_non_null(dt);
44-
45-
result = pcmk__time_format_hr(format, dt, 0);
46-
assert_non_null(result);
47-
48-
assert_string_equal(result, expected);
49-
50-
crm_time_free(dt);
51-
free(result);
52-
}
36+
#define assert_hr_format(format, expected) \
37+
do { \
38+
crm_time_t *dt = NULL; \
39+
char *result = NULL; \
40+
\
41+
dt = crm_time_new(DATE_TIME_S); \
42+
assert_non_null(dt); \
43+
\
44+
result = pcmk__time_format_hr(format, dt, 0); \
45+
assert_non_null(result); \
46+
assert_string_equal(result, expected); \
47+
\
48+
crm_time_free(dt); \
49+
free(result); \
50+
} while (0)
5351

5452
/*!
5553
* \internal
5654
* \brief Check that \c pcmk__time_format_hr() produced expected result (or alt)
5755
*
58-
* \param[in] format Time format string
59-
* \param[in] expected Check succeeds if result matches this
60-
* \param[in] alternate Check also succeeds if result matches this
56+
* \param[in] format Time format string (<tt>const char *</tt>)
57+
* \param[in] expected Succeed if result matches this (<tt>const char *</tt>)
58+
* \param[in] alternate Succeed if result matches this (<tt>const char *</tt>)
6159
*
6260
* \note This allows two possible results because different \c strftime()
6361
* implementations handle certain format syntax differently.
6462
*/
65-
static void
66-
assert_hr_format_alt(const char *format, const char *expected,
67-
const char *alternate)
68-
{
69-
crm_time_t *dt = NULL;
70-
char *result = NULL;
71-
72-
dt = crm_time_new(DATE_TIME_S);
73-
assert_non_null(dt);
74-
75-
result = pcmk__time_format_hr(format, dt, 0);
76-
assert_non_null(result);
77-
78-
// CMocka has no abstraction for comparing to multiple strings
79-
assert_true((strcmp(result, expected) == 0)
80-
|| (strcmp(result, alternate) == 0));
81-
82-
crm_time_free(dt);
83-
free(result);
84-
}
63+
#define assert_hr_format_alt(format, expected, alternate) \
64+
do { \
65+
crm_time_t *dt = NULL; \
66+
char *result = NULL; \
67+
\
68+
dt = crm_time_new(DATE_TIME_S); \
69+
assert_non_null(dt); \
70+
\
71+
result = pcmk__time_format_hr(format, dt, 0); \
72+
assert_non_null(result); \
73+
\
74+
/* CMocka has no abstraction for comparing to multiple strings */ \
75+
assert_true((strcmp(result, expected) == 0) \
76+
|| (strcmp(result, alternate) == 0)); \
77+
\
78+
crm_time_free(dt); \
79+
free(result); \
80+
} while (0)
8581

8682
/*!
8783
* \internal
8884
* \brief Check that \c pcmk__time_format_hr() produced expected high-res result
8985
*
90-
* \param[in] format Time format string
91-
* \param[in] expected Check succeeds if result matches this
92-
* \param[in] usec Microseconds component of the reference time
86+
* \param[in] format Time format string (<tt>const char *</tt>)
87+
* \param[in] expected Succeed if result matches this (<tt>const char *</tt>)
88+
* \param[in] usec Microseconds component of the reference time (\c int)
9389
*/
94-
static void
95-
assert_hr_format_usec(const char *format, const char *expected, int usec)
96-
{
97-
crm_time_t *dt = NULL;
98-
char *result = NULL;
99-
100-
dt = crm_time_new(DATE_TIME_S);
101-
assert_non_null(dt);
102-
103-
result = pcmk__time_format_hr(format, dt, usec);
104-
assert_non_null(result);
105-
assert_string_equal(result, expected);
106-
107-
crm_time_free(dt);
108-
free(result);
109-
}
90+
#define assert_hr_format_usec(format, expected, usec) \
91+
do { \
92+
crm_time_t *dt = NULL; \
93+
char *result = NULL; \
94+
\
95+
dt = crm_time_new(DATE_TIME_S); \
96+
assert_non_null(dt); \
97+
\
98+
result = pcmk__time_format_hr(format, dt, usec); \
99+
assert_non_null(result); \
100+
assert_string_equal(result, expected); \
101+
\
102+
crm_time_free(dt); \
103+
free(result); \
104+
} while (0)
110105

111106
static void
112107
null_format(void **state)

lib/common/tests/nvpair/pcmk__scan_nvpair_test.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,30 @@
1313

1414
#include <crm/common/unittest_internal.h>
1515

16-
static void
17-
assert_scan_nvpair_success(const gchar *input, const gchar *expected_name,
18-
const gchar *expected_value)
19-
{
20-
gchar *name = NULL;
21-
gchar *value = NULL;
22-
int rc = pcmk__scan_nvpair(input, &name, &value);
23-
24-
assert_int_equal(rc, pcmk_rc_ok);
25-
assert_string_equal(name, expected_name);
26-
assert_string_equal(value, expected_value);
27-
28-
g_free(name);
29-
g_free(value);
30-
}
31-
32-
static void
33-
assert_scan_nvpair_failure(const gchar *input)
34-
{
35-
gchar *name = NULL;
36-
gchar *value = NULL;
37-
int rc = pcmk__scan_nvpair(input, &name, &value);
38-
39-
assert_int_equal(rc, pcmk_rc_bad_nvpair);
40-
assert_null(name);
41-
assert_null(value);
42-
}
16+
#define assert_scan_nvpair_success(input, expected_name, expected_value) \
17+
do { \
18+
gchar *name = NULL; \
19+
gchar *value = NULL; \
20+
int rc = pcmk__scan_nvpair(input, &name, &value); \
21+
\
22+
assert_int_equal(rc, pcmk_rc_ok); \
23+
assert_string_equal(name, expected_name); \
24+
assert_string_equal(value, expected_value); \
25+
\
26+
g_free(name); \
27+
g_free(value); \
28+
} while (0)
29+
30+
#define assert_scan_nvpair_failure(input) \
31+
do { \
32+
gchar *name = NULL; \
33+
gchar *value = NULL; \
34+
int rc = pcmk__scan_nvpair(input, &name, &value); \
35+
\
36+
assert_int_equal(rc, pcmk_rc_bad_nvpair); \
37+
assert_null(name); \
38+
assert_null(value); \
39+
} while (0)
4340

4441
static void
4542
null_asserts(void **state)

lib/common/tests/patchset/pcmk__cib_element_in_patchset_test.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,27 +45,21 @@ create_patchset(const char *source_s, const char *target_s)
4545
return patchset;
4646
}
4747

48-
static void
49-
assert_in_patchset(const char *source_s, const char *target_s,
50-
const char *element)
51-
{
52-
xmlNode *patchset = create_patchset(source_s, target_s);
53-
54-
assert_true(pcmk__cib_element_in_patchset(patchset, element));
55-
56-
pcmk__xml_free(patchset);
57-
}
58-
59-
static void
60-
assert_not_in_patchset(const char *source_s, const char *target_s,
61-
const char *element)
62-
{
63-
xmlNode *patchset = create_patchset(source_s, target_s);
64-
65-
assert_false(pcmk__cib_element_in_patchset(patchset, element));
66-
67-
pcmk__xml_free(patchset);
68-
}
48+
#define assert_in_patchset(source_s, target_s, element) \
49+
do { \
50+
xmlNode *patchset = create_patchset(source_s, target_s); \
51+
\
52+
assert_true(pcmk__cib_element_in_patchset(patchset, element)); \
53+
pcmk__xml_free(patchset); \
54+
} while (0)
55+
56+
#define assert_not_in_patchset(source_s, target_s, element) \
57+
do { \
58+
xmlNode *patchset = create_patchset(source_s, target_s); \
59+
\
60+
assert_false(pcmk__cib_element_in_patchset(patchset, element)); \
61+
pcmk__xml_free(patchset); \
62+
} while (0)
6963

7064
static void
7165
null_patchset_asserts(void **state)

0 commit comments

Comments
 (0)