Skip to content

Commit 56b0488

Browse files
listxgitster
authored andcommitted
trailer: add unit tests for trailer iterator
Test the number of trailers found by the iterator (to be more precise, the parsing mechanism which the iterator just walks over) when given some arbitrary log message. We test the iterator because it is a public interface function exposed by the trailer API (we generally don't want to test internal implementation details which are, unlike the API, subject to drastic changes). Signed-off-by: Linus Arver <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 704b590 commit 56b0488

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,7 @@ UNIT_TEST_PROGRAMS += t-ctype
13471347
UNIT_TEST_PROGRAMS += t-mem-pool
13481348
UNIT_TEST_PROGRAMS += t-prio-queue
13491349
UNIT_TEST_PROGRAMS += t-strbuf
1350+
UNIT_TEST_PROGRAMS += t-trailer
13501351
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
13511352
UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
13521353
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o

t/unit-tests/t-trailer.c

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#include "test-lib.h"
2+
#include "trailer.h"
3+
4+
static void t_trailer_iterator(const char *msg, size_t num_expected_trailers)
5+
{
6+
struct trailer_iterator iter;
7+
size_t i = 0;
8+
9+
trailer_iterator_init(&iter, msg);
10+
while (trailer_iterator_advance(&iter))
11+
i++;
12+
trailer_iterator_release(&iter);
13+
14+
check_uint(i, ==, num_expected_trailers);
15+
}
16+
17+
static void run_t_trailer_iterator(void)
18+
{
19+
static struct test_cases {
20+
const char *name;
21+
const char *msg;
22+
size_t num_expected_trailers;
23+
} tc[] = {
24+
{
25+
"empty input",
26+
"",
27+
0
28+
},
29+
{
30+
"no newline at beginning",
31+
"Fixes: x\n"
32+
"Acked-by: x\n"
33+
"Reviewed-by: x\n",
34+
0
35+
},
36+
{
37+
"newline at beginning",
38+
"\n"
39+
"Fixes: x\n"
40+
"Acked-by: x\n"
41+
"Reviewed-by: x\n",
42+
3
43+
},
44+
{
45+
"without body text",
46+
"subject: foo bar\n"
47+
"\n"
48+
"Fixes: x\n"
49+
"Acked-by: x\n"
50+
"Reviewed-by: x\n",
51+
3
52+
},
53+
{
54+
"with body text, without divider",
55+
"my subject\n"
56+
"\n"
57+
"my body which is long\n"
58+
"and contains some special\n"
59+
"chars like : = ? !\n"
60+
"hello\n"
61+
"\n"
62+
"Fixes: x\n"
63+
"Acked-by: x\n"
64+
"Reviewed-by: x\n"
65+
"Signed-off-by: x\n",
66+
4
67+
},
68+
{
69+
"with body text, without divider (second trailer block)",
70+
"my subject\n"
71+
"\n"
72+
"my body which is long\n"
73+
"and contains some special\n"
74+
"chars like : = ? !\n"
75+
"hello\n"
76+
"\n"
77+
"Fixes: x\n"
78+
"Acked-by: x\n"
79+
"Reviewed-by: x\n"
80+
"Signed-off-by: x\n"
81+
"\n"
82+
/*
83+
* Because this is the last trailer block, it takes
84+
* precedence over the first one encountered above.
85+
*/
86+
"Helped-by: x\n"
87+
"Signed-off-by: x\n",
88+
2
89+
},
90+
{
91+
"with body text, with divider",
92+
"my subject\n"
93+
"\n"
94+
"my body which is long\n"
95+
"and contains some special\n"
96+
"chars like : = ? !\n"
97+
"hello\n"
98+
"\n"
99+
"---\n"
100+
"\n"
101+
/*
102+
* This trailer still counts because the iterator
103+
* always ignores the divider.
104+
*/
105+
"Signed-off-by: x\n",
106+
1
107+
},
108+
{
109+
"with non-trailer lines in trailer block",
110+
"subject: foo bar\n"
111+
"\n"
112+
/*
113+
* Even though this trailer block has a non-trailer line
114+
* in it, it's still a valid trailer block because it's
115+
* at least 25% trailers and is Git-generated (see
116+
* git_generated_prefixes[] in trailer.c).
117+
*/
118+
"not a trailer line\n"
119+
"not a trailer line\n"
120+
"not a trailer line\n"
121+
"Signed-off-by: x\n",
122+
1
123+
},
124+
{
125+
"with non-trailer lines (one too many) in trailer block",
126+
"subject: foo bar\n"
127+
"\n"
128+
/*
129+
* This block has only 20% trailers, so it's below the
130+
* 25% threshold.
131+
*/
132+
"not a trailer line\n"
133+
"not a trailer line\n"
134+
"not a trailer line\n"
135+
"not a trailer line\n"
136+
"Signed-off-by: x\n",
137+
0
138+
},
139+
{
140+
"with non-trailer lines (only 1) in trailer block, but no Git-generated trailers",
141+
"subject: foo bar\n"
142+
"\n"
143+
/*
144+
* This block has only 1 non-trailer out of 10 (IOW, 90%
145+
* trailers) but is not considered a trailer block
146+
* because the 25% threshold only applies to cases where
147+
* there was a Git-generated trailer.
148+
*/
149+
"Reviewed-by: x\n"
150+
"Reviewed-by: x\n"
151+
"Reviewed-by: x\n"
152+
"Helped-by: x\n"
153+
"Helped-by: x\n"
154+
"Helped-by: x\n"
155+
"Acked-by: x\n"
156+
"Acked-by: x\n"
157+
"Acked-by: x\n"
158+
"not a trailer line\n",
159+
0
160+
},
161+
};
162+
163+
for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
164+
TEST(t_trailer_iterator(tc[i].msg,
165+
tc[i].num_expected_trailers),
166+
"%s", tc[i].name);
167+
}
168+
}
169+
170+
int cmd_main(int argc, const char **argv)
171+
{
172+
run_t_trailer_iterator();
173+
return test_done();
174+
}

0 commit comments

Comments
 (0)