trailer unit tests: inspect iterator contents

Previously we only checked whether we would iterate a certain (expected) number of times. Also check the parsed "raw", "key" and "val" fields during each iteration. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Linus Arver <linus@ucla.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Arver committed May 2, 2024 at 04:54 UTC dc88e5279a0bc68dc7b0337b0da34d50984bb38b
1 file changed +148 -13
t/unit-tests/t-trailer.c
+148 -13
@@ -1,14 +1,27 @@
1 #include "test-lib.h"
2 #include "trailer.h"
3
4 -static void t_trailer_iterator(const char *msg, size_t num_expected)
4 +struct contents {
5 + const char *raw;
6 + const char *key;
7 + const char *val;
8 +};
9 +
10 +static void t_trailer_iterator(const char *msg, size_t num_expected,
11 + struct contents *contents)
12 {
13 struct trailer_iterator iter;
14 size_t i = 0;
15
16 trailer_iterator_init(&iter, msg);
10 - while (trailer_iterator_advance(&iter))
17 + while (trailer_iterator_advance(&iter)) {
18 + if (num_expected) {
19 + check_str(iter.raw, contents[i].raw);
20 + check_str(iter.key.buf, contents[i].key);
21 + check_str(iter.val.buf, contents[i].val);
22 + }
23 i++;
24 + }
25 trailer_iterator_release(&iter);
26
27 check_uint(i, ==, num_expected);
@@ -16,22 +29,26 @@ static void t_trailer_iterator(const char *msg, size_t num_expected)
29
30 static void run_t_trailer_iterator(void)
31 {
32 +
33 static struct test_cases {
34 const char *name;
35 const char *msg;
36 size_t num_expected;
37 + struct contents contents[10];
38 } tc[] = {
39 {
40 "empty input",
41 "",
27 - 0
42 + 0,
43 + {{0}},
44 },
45 {
46 "no newline at beginning",
47 "Fixes: x\n"
48 "Acked-by: x\n"
49 "Reviewed-by: x\n",
34 - 0
50 + 0,
51 + {{0}},
52 },
53 {
54 "newline at beginning",
@@ -39,7 +56,27 @@ static void run_t_trailer_iterator(void)
56 "Fixes: x\n"
57 "Acked-by: x\n"
58 "Reviewed-by: x\n",
42 - 3
59 + 3,
60 + {
61 + {
62 + .raw = "Fixes: x\n",
63 + .key = "Fixes",
64 + .val = "x",
65 + },
66 + {
67 + .raw = "Acked-by: x\n",
68 + .key = "Acked-by",
69 + .val = "x",
70 + },
71 + {
72 + .raw = "Reviewed-by: x\n",
73 + .key = "Reviewed-by",
74 + .val = "x",
75 + },
76 + {
77 + 0
78 + },
79 + },
80 },
81 {
82 "without body text",
@@ -48,7 +85,27 @@ static void run_t_trailer_iterator(void)
85 "Fixes: x\n"
86 "Acked-by: x\n"
87 "Reviewed-by: x\n",
51 - 3
88 + 3,
89 + {
90 + {
91 + .raw = "Fixes: x\n",
92 + .key = "Fixes",
93 + .val = "x",
94 + },
95 + {
96 + .raw = "Acked-by: x\n",
97 + .key = "Acked-by",
98 + .val = "x",
99 + },
100 + {
101 + .raw = "Reviewed-by: x\n",
102 + .key = "Reviewed-by",
103 + .val = "x",
104 + },
105 + {
106 + 0
107 + },
108 + },
109 },
110 {
111 "with body text, without divider",
@@ -63,7 +120,32 @@ static void run_t_trailer_iterator(void)
120 "Acked-by: x\n"
121 "Reviewed-by: x\n"
122 "Signed-off-by: x\n",
66 - 4
123 + 4,
124 + {
125 + {
126 + .raw = "Fixes: x\n",
127 + .key = "Fixes",
128 + .val = "x",
129 + },
130 + {
131 + .raw = "Acked-by: x\n",
132 + .key = "Acked-by",
133 + .val = "x",
134 + },
135 + {
136 + .raw = "Reviewed-by: x\n",
137 + .key = "Reviewed-by",
138 + .val = "x",
139 + },
140 + {
141 + .raw = "Signed-off-by: x\n",
142 + .key = "Signed-off-by",
143 + .val = "x",
144 + },
145 + {
146 + 0
147 + },
148 + },
149 },
150 {
151 "with body text, without divider (second trailer block)",
@@ -85,7 +167,22 @@ static void run_t_trailer_iterator(void)
167 */
168 "Helped-by: x\n"
169 "Signed-off-by: x\n",
88 - 2
170 + 2,
171 + {
172 + {
173 + .raw = "Helped-by: x\n",
174 + .key = "Helped-by",
175 + .val = "x",
176 + },
177 + {
178 + .raw = "Signed-off-by: x\n",
179 + .key = "Signed-off-by",
180 + .val = "x",
181 + },
182 + {
183 + 0
184 + },
185 + },
186 },
187 {
188 "with body text, with divider",
@@ -103,7 +200,17 @@ static void run_t_trailer_iterator(void)
200 * always ignores the divider.
201 */
202 "Signed-off-by: x\n",
106 - 1
203 + 1,
204 + {
205 + {
206 + .raw = "Signed-off-by: x\n",
207 + .key = "Signed-off-by",
208 + .val = "x",
209 + },
210 + {
211 + 0
212 + },
213 + },
214 },
215 {
216 "with non-trailer lines in trailer block",
@@ -125,7 +232,32 @@ static void run_t_trailer_iterator(void)
232 * because we still want to iterate through the entire
233 * block.
234 */
128 - 4
235 + 4,
236 + {
237 + {
238 + .raw = "not a trailer line\n",
239 + .key = "not a trailer line",
240 + .val = "",
241 + },
242 + {
243 + .raw = "not a trailer line\n",
244 + .key = "not a trailer line",
245 + .val = "",
246 + },
247 + {
248 + .raw = "not a trailer line\n",
249 + .key = "not a trailer line",
250 + .val = "",
251 + },
252 + {
253 + .raw = "Signed-off-by: x\n",
254 + .key = "Signed-off-by",
255 + .val = "x",
256 + },
257 + {
258 + 0
259 + },
260 + },
261 },
262 {
263 "with non-trailer lines (one too many) in trailer block",
@@ -140,7 +272,8 @@ static void run_t_trailer_iterator(void)
272 "not a trailer line\n"
273 "not a trailer line\n"
274 "Signed-off-by: x\n",
143 - 0
275 + 0,
276 + {{0}},
277 },
278 {
279 "with non-trailer lines (only 1) in trailer block, but no Git-generated trailers",
@@ -162,13 +295,15 @@ static void run_t_trailer_iterator(void)
295 "Acked-by: x\n"
296 "Acked-by: x\n"
297 "not a trailer line\n",
165 - 0
298 + 0,
299 + {{0}},
300 },
301 };
302
303 for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
304 TEST(t_trailer_iterator(tc[i].msg,
171 - tc[i].num_expected),
305 + tc[i].num_expected,
306 + tc[i].contents),
307 "%s", tc[i].name);
308 }
309 }