Skip to content

Commit dc88e52

Browse files
listxgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f80060 commit dc88e52

File tree

1 file changed

+148
-13
lines changed

1 file changed

+148
-13
lines changed

t/unit-tests/t-trailer.c

Lines changed: 148 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,82 @@
11
#include "test-lib.h"
22
#include "trailer.h"
33

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)
512
{
613
struct trailer_iterator iter;
714
size_t i = 0;
815

916
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+
}
1123
i++;
24+
}
1225
trailer_iterator_release(&iter);
1326

1427
check_uint(i, ==, num_expected);
1528
}
1629

1730
static void run_t_trailer_iterator(void)
1831
{
32+
1933
static struct test_cases {
2034
const char *name;
2135
const char *msg;
2236
size_t num_expected;
37+
struct contents contents[10];
2338
} tc[] = {
2439
{
2540
"empty input",
2641
"",
27-
0
42+
0,
43+
{{0}},
2844
},
2945
{
3046
"no newline at beginning",
3147
"Fixes: x\n"
3248
"Acked-by: x\n"
3349
"Reviewed-by: x\n",
34-
0
50+
0,
51+
{{0}},
3552
},
3653
{
3754
"newline at beginning",
3855
"\n"
3956
"Fixes: x\n"
4057
"Acked-by: x\n"
4158
"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+
},
4380
},
4481
{
4582
"without body text",
@@ -48,7 +85,27 @@ static void run_t_trailer_iterator(void)
4885
"Fixes: x\n"
4986
"Acked-by: x\n"
5087
"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+
},
52109
},
53110
{
54111
"with body text, without divider",
@@ -63,7 +120,32 @@ static void run_t_trailer_iterator(void)
63120
"Acked-by: x\n"
64121
"Reviewed-by: x\n"
65122
"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+
},
67149
},
68150
{
69151
"with body text, without divider (second trailer block)",
@@ -85,7 +167,22 @@ static void run_t_trailer_iterator(void)
85167
*/
86168
"Helped-by: x\n"
87169
"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+
},
89186
},
90187
{
91188
"with body text, with divider",
@@ -103,7 +200,17 @@ static void run_t_trailer_iterator(void)
103200
* always ignores the divider.
104201
*/
105202
"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+
},
107214
},
108215
{
109216
"with non-trailer lines in trailer block",
@@ -125,7 +232,32 @@ static void run_t_trailer_iterator(void)
125232
* because we still want to iterate through the entire
126233
* block.
127234
*/
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+
},
129261
},
130262
{
131263
"with non-trailer lines (one too many) in trailer block",
@@ -140,7 +272,8 @@ static void run_t_trailer_iterator(void)
140272
"not a trailer line\n"
141273
"not a trailer line\n"
142274
"Signed-off-by: x\n",
143-
0
275+
0,
276+
{{0}},
144277
},
145278
{
146279
"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)
162295
"Acked-by: x\n"
163296
"Acked-by: x\n"
164297
"not a trailer line\n",
165-
0
298+
0,
299+
{{0}},
166300
},
167301
};
168302

169303
for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
170304
TEST(t_trailer_iterator(tc[i].msg,
171-
tc[i].num_expected),
305+
tc[i].num_expected,
306+
tc[i].contents),
172307
"%s", tc[i].name);
173308
}
174309
}

0 commit comments

Comments
 (0)