|
11 | 11 |
|
12 | 12 | #include <crm/common/unittest_internal.h>
|
13 | 13 |
|
| 14 | +/*! |
| 15 | + * \internal |
| 16 | + * \brief Check a call with given input against expected return value and result |
| 17 | + * |
| 18 | + * \param[in] input Input string |
| 19 | + * \param[in] expected_rc Expected return code |
| 20 | + * \param[in] expected_result Expected parsed value (ignored unless |
| 21 | + * \p expected_rc is \c pcmk_rc_ok) |
| 22 | + */ |
14 | 23 | static void
|
15 |
| -bad_input(void **state) { |
16 |
| - // Dumps core via CRM_CHECK() |
17 |
| - assert_int_equal(pcmk__parse_bool(NULL, NULL), EINVAL); |
| 24 | +assert_parse_bool(const char *input, int expected_rc, bool expected_result) |
| 25 | +{ |
| 26 | + bool result = false; |
18 | 27 |
|
19 |
| - assert_int_equal(pcmk__parse_bool("", NULL), pcmk_rc_bad_input); |
20 |
| - assert_int_equal(pcmk__parse_bool("blahblah", NULL), pcmk_rc_bad_input); |
21 |
| -} |
| 28 | + // Ensure we still validate the string with a NULL result argument |
| 29 | + assert_int_equal(pcmk__parse_bool(input, NULL), expected_rc); |
22 | 30 |
|
23 |
| -static void |
24 |
| -is_true(void **state) { |
25 |
| - bool result; |
| 31 | + if (expected_rc != pcmk_rc_ok) { |
| 32 | + // Make sure the value of result does not change on failure |
| 33 | + expected_result = result; |
| 34 | + } |
| 35 | + |
| 36 | + assert_int_equal(pcmk__parse_bool(input, &result), expected_rc); |
| 37 | + if (expected_result) { |
| 38 | + assert_true(result); |
| 39 | + } else { |
| 40 | + assert_false(result); |
| 41 | + } |
26 | 42 |
|
27 |
| - assert_int_equal(pcmk__parse_bool("true", &result), pcmk_rc_ok); |
28 |
| - assert_true(result); |
29 |
| - assert_int_equal(pcmk__parse_bool("TrUe", &result), pcmk_rc_ok); |
30 |
| - assert_true(result); |
31 |
| - assert_int_equal(pcmk__parse_bool("on", &result), pcmk_rc_ok); |
32 |
| - assert_true(result); |
33 |
| - assert_int_equal(pcmk__parse_bool("ON", &result), pcmk_rc_ok); |
34 |
| - assert_true(result); |
35 |
| - assert_int_equal(pcmk__parse_bool("yes", &result), pcmk_rc_ok); |
36 |
| - assert_true(result); |
37 |
| - assert_int_equal(pcmk__parse_bool("yES", &result), pcmk_rc_ok); |
38 |
| - assert_true(result); |
39 |
| - assert_int_equal(pcmk__parse_bool("y", &result), pcmk_rc_ok); |
40 |
| - assert_true(result); |
41 |
| - assert_int_equal(pcmk__parse_bool("Y", &result), pcmk_rc_ok); |
42 |
| - assert_true(result); |
43 |
| - assert_int_equal(pcmk__parse_bool("1", &result), pcmk_rc_ok); |
44 |
| - assert_true(result); |
| 43 | + // Repeat with result initially set to true |
| 44 | + result = true; |
| 45 | + if (expected_rc != pcmk_rc_ok) { |
| 46 | + expected_result = result; |
| 47 | + } |
45 | 48 |
|
46 |
| - // Ensure it still validates the string with a NULL result argument |
47 |
| - assert_int_equal(pcmk__parse_bool("true", NULL), pcmk_rc_ok); |
48 |
| - assert_int_equal(pcmk__parse_bool("on", NULL), pcmk_rc_ok); |
49 |
| - assert_int_equal(pcmk__parse_bool("yes", NULL), pcmk_rc_ok); |
50 |
| - assert_int_equal(pcmk__parse_bool("y", NULL), pcmk_rc_ok); |
51 |
| - assert_int_equal(pcmk__parse_bool("1", NULL), pcmk_rc_ok); |
| 49 | + assert_int_equal(pcmk__parse_bool(input, &result), expected_rc); |
| 50 | + if (expected_result) { |
| 51 | + assert_true(result); |
| 52 | + } else { |
| 53 | + assert_false(result); |
| 54 | + } |
52 | 55 | }
|
53 | 56 |
|
54 | 57 | static void
|
55 |
| -is_not_true(void **state) { |
56 |
| - assert_int_equal(pcmk__parse_bool("truedat", NULL), pcmk_rc_bad_input); |
57 |
| - assert_int_equal(pcmk__parse_bool("onnn", NULL), pcmk_rc_bad_input); |
58 |
| - assert_int_equal(pcmk__parse_bool("yep", NULL), pcmk_rc_bad_input); |
59 |
| - assert_int_equal(pcmk__parse_bool("Y!", NULL), pcmk_rc_bad_input); |
60 |
| - assert_int_equal(pcmk__parse_bool("100", NULL), pcmk_rc_bad_input); |
| 58 | +bad_input(void **state) |
| 59 | +{ |
| 60 | + // Dumps core via CRM_CHECK() |
| 61 | + assert_parse_bool(NULL, EINVAL, false); |
| 62 | + |
| 63 | + assert_parse_bool("", pcmk_rc_bad_input, false); |
| 64 | + assert_parse_bool("blahblah", pcmk_rc_bad_input, false); |
61 | 65 | }
|
62 | 66 |
|
63 | 67 | static void
|
64 |
| -is_false(void **state) { |
65 |
| - bool result; |
| 68 | +is_true(void **state) |
| 69 | +{ |
| 70 | + assert_parse_bool("true", pcmk_rc_ok, true); |
| 71 | + assert_parse_bool("TrUe", pcmk_rc_ok, true); |
| 72 | + assert_parse_bool("on", pcmk_rc_ok, true); |
| 73 | + assert_parse_bool("ON", pcmk_rc_ok, true); |
| 74 | + assert_parse_bool("yes", pcmk_rc_ok, true); |
| 75 | + assert_parse_bool("yES", pcmk_rc_ok, true); |
| 76 | + assert_parse_bool("y", pcmk_rc_ok, true); |
| 77 | + assert_parse_bool("Y", pcmk_rc_ok, true); |
| 78 | + assert_parse_bool("1", pcmk_rc_ok, true); |
| 79 | +} |
66 | 80 |
|
67 |
| - assert_int_equal(pcmk__parse_bool("false", &result), pcmk_rc_ok); |
68 |
| - assert_false(result); |
69 |
| - assert_int_equal(pcmk__parse_bool("fAlSe", &result), pcmk_rc_ok); |
70 |
| - assert_false(result); |
71 |
| - assert_int_equal(pcmk__parse_bool(PCMK_VALUE_OFF, &result), pcmk_rc_ok); |
72 |
| - assert_false(result); |
73 |
| - assert_int_equal(pcmk__parse_bool("OFF", &result), pcmk_rc_ok); |
74 |
| - assert_false(result); |
75 |
| - assert_int_equal(pcmk__parse_bool("no", &result), pcmk_rc_ok); |
76 |
| - assert_false(result); |
77 |
| - assert_int_equal(pcmk__parse_bool("No", &result), pcmk_rc_ok); |
78 |
| - assert_false(result); |
79 |
| - assert_int_equal(pcmk__parse_bool("n", &result), pcmk_rc_ok); |
80 |
| - assert_false(result); |
81 |
| - assert_int_equal(pcmk__parse_bool("N", &result), pcmk_rc_ok); |
82 |
| - assert_false(result); |
83 |
| - assert_int_equal(pcmk__parse_bool("0", &result), pcmk_rc_ok); |
84 |
| - assert_false(result); |
| 81 | +static void |
| 82 | +is_not_true(void **state) |
| 83 | +{ |
| 84 | + assert_parse_bool("truedat", pcmk_rc_bad_input, false); |
| 85 | + assert_parse_bool("onnn", pcmk_rc_bad_input, false); |
| 86 | + assert_parse_bool("yep", pcmk_rc_bad_input, false); |
| 87 | + assert_parse_bool("Y!", pcmk_rc_bad_input, false); |
| 88 | + assert_parse_bool("100", pcmk_rc_bad_input, false); |
| 89 | +} |
85 | 90 |
|
86 |
| - // Ensure it still validates the string with a NULL result argument |
87 |
| - assert_int_equal(pcmk__parse_bool("false", NULL), pcmk_rc_ok); |
88 |
| - assert_int_equal(pcmk__parse_bool(PCMK_VALUE_OFF, NULL), pcmk_rc_ok); |
89 |
| - assert_int_equal(pcmk__parse_bool("no", NULL), pcmk_rc_ok); |
90 |
| - assert_int_equal(pcmk__parse_bool("n", NULL), pcmk_rc_ok); |
91 |
| - assert_int_equal(pcmk__parse_bool("0", NULL), pcmk_rc_ok); |
| 91 | +static void |
| 92 | +is_false(void **state) |
| 93 | +{ |
| 94 | + assert_parse_bool("false", pcmk_rc_ok, false); |
| 95 | + assert_parse_bool("fAlSe", pcmk_rc_ok, false); |
| 96 | + assert_parse_bool("off", pcmk_rc_ok, false); |
| 97 | + assert_parse_bool("OFF", pcmk_rc_ok, false); |
| 98 | + assert_parse_bool("no", pcmk_rc_ok, false); |
| 99 | + assert_parse_bool("No", pcmk_rc_ok, false); |
| 100 | + assert_parse_bool("n", pcmk_rc_ok, false); |
| 101 | + assert_parse_bool("N", pcmk_rc_ok, false); |
| 102 | + assert_parse_bool("0", pcmk_rc_ok, false); |
92 | 103 | }
|
93 | 104 |
|
94 | 105 | static void
|
95 |
| -is_not_false(void **state) { |
96 |
| - assert_int_equal(pcmk__parse_bool("falseee", NULL), pcmk_rc_bad_input); |
97 |
| - assert_int_equal(pcmk__parse_bool("of", NULL), pcmk_rc_bad_input); |
98 |
| - assert_int_equal(pcmk__parse_bool("nope", NULL), pcmk_rc_bad_input); |
99 |
| - assert_int_equal(pcmk__parse_bool("N!", NULL), pcmk_rc_bad_input); |
100 |
| - assert_int_equal(pcmk__parse_bool("000", NULL), pcmk_rc_bad_input); |
| 106 | +is_not_false(void **state) |
| 107 | +{ |
| 108 | + assert_parse_bool("falseee", pcmk_rc_bad_input, false); |
| 109 | + assert_parse_bool("of", pcmk_rc_bad_input, false); |
| 110 | + assert_parse_bool("nope", pcmk_rc_bad_input, false); |
| 111 | + assert_parse_bool("N!", pcmk_rc_bad_input, false); |
| 112 | + assert_parse_bool("000", pcmk_rc_bad_input, false); |
101 | 113 | }
|
102 | 114 |
|
103 | 115 | PCMK__UNIT_TEST(NULL, NULL,
|
|
0 commit comments