t-strvec: use if_test
The macro TEST takes a single expression. If a test requires multiple statements then they need to be placed in a function that's called in the TEST expression. Remove the cognitive overhead of defining and calling single-use functions by using if_test instead. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 30, 2024 at 16:12 UTC
9ddec6b79af6786bc628e66a75f47040fea1857b
1 file changed
+156
-200
t/unit-tests/t-strvec.c
+156
-200
@@ -36,237 +36,193 @@ static void check_strvec_loc(const char *loc, struct strvec *vec, ...)
36
check_pointer_eq(vec->v[nr], NULL);
37
}
38
39
-static void t_static_init(void)
39
+int cmd_main(int argc, const char **argv)
40
{
41
- struct strvec vec = STRVEC_INIT;
42
- check_pointer_eq(vec.v, empty_strvec);
43
- check_uint(vec.nr, ==, 0);
44
- check_uint(vec.alloc, ==, 0);
45
-}
41
+ if_test ("static initialization") {
42
+ struct strvec vec = STRVEC_INIT;
43
+ check_pointer_eq(vec.v, empty_strvec);
44
+ check_uint(vec.nr, ==, 0);
45
+ check_uint(vec.alloc, ==, 0);
46
+ }
47
47
-static void t_dynamic_init(void)
48
-{
49
- struct strvec vec;
50
- strvec_init(&vec);
51
- check_pointer_eq(vec.v, empty_strvec);
52
- check_uint(vec.nr, ==, 0);
53
- check_uint(vec.alloc, ==, 0);
54
-}
48
+ if_test ("dynamic initialization") {
49
+ struct strvec vec;
50
+ strvec_init(&vec);
51
+ check_pointer_eq(vec.v, empty_strvec);
52
+ check_uint(vec.nr, ==, 0);
53
+ check_uint(vec.alloc, ==, 0);
54
+ }
55
56
-static void t_clear(void)
57
-{
58
- struct strvec vec = STRVEC_INIT;
59
- strvec_push(&vec, "foo");
60
- strvec_clear(&vec);
61
- check_pointer_eq(vec.v, empty_strvec);
62
- check_uint(vec.nr, ==, 0);
63
- check_uint(vec.alloc, ==, 0);
64
-}
56
+ if_test ("clear") {
57
+ struct strvec vec = STRVEC_INIT;
58
+ strvec_push(&vec, "foo");
59
+ strvec_clear(&vec);
60
+ check_pointer_eq(vec.v, empty_strvec);
61
+ check_uint(vec.nr, ==, 0);
62
+ check_uint(vec.alloc, ==, 0);
63
+ }
64
66
-static void t_push(void)
67
-{
68
- struct strvec vec = STRVEC_INIT;
65
+ if_test ("push") {
66
+ struct strvec vec = STRVEC_INIT;
67
70
- strvec_push(&vec, "foo");
71
- check_strvec(&vec, "foo", NULL);
68
+ strvec_push(&vec, "foo");
69
+ check_strvec(&vec, "foo", NULL);
70
73
- strvec_push(&vec, "bar");
74
- check_strvec(&vec, "foo", "bar", NULL);
71
+ strvec_push(&vec, "bar");
72
+ check_strvec(&vec, "foo", "bar", NULL);
73
76
- strvec_clear(&vec);
77
-}
74
+ strvec_clear(&vec);
75
+ }
76
79
-static void t_pushf(void)
80
-{
81
- struct strvec vec = STRVEC_INIT;
82
- strvec_pushf(&vec, "foo: %d", 1);
83
- check_strvec(&vec, "foo: 1", NULL);
84
- strvec_clear(&vec);
85
-}
77
+ if_test ("pushf") {
78
+ struct strvec vec = STRVEC_INIT;
79
+ strvec_pushf(&vec, "foo: %d", 1);
80
+ check_strvec(&vec, "foo: 1", NULL);
81
+ strvec_clear(&vec);
82
+ }
83
87
-static void t_pushl(void)
88
-{
89
- struct strvec vec = STRVEC_INIT;
90
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
91
- check_strvec(&vec, "foo", "bar", "baz", NULL);
92
- strvec_clear(&vec);
93
-}
84
+ if_test ("pushl") {
85
+ struct strvec vec = STRVEC_INIT;
86
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
87
+ check_strvec(&vec, "foo", "bar", "baz", NULL);
88
+ strvec_clear(&vec);
89
+ }
90
95
-static void t_pushv(void)
96
-{
97
- const char *strings[] = {
98
- "foo", "bar", "baz", NULL,
99
- };
100
- struct strvec vec = STRVEC_INIT;
91
+ if_test ("pushv") {
92
+ const char *strings[] = {
93
+ "foo", "bar", "baz", NULL,
94
+ };
95
+ struct strvec vec = STRVEC_INIT;
96
102
- strvec_pushv(&vec, strings);
103
- check_strvec(&vec, "foo", "bar", "baz", NULL);
97
+ strvec_pushv(&vec, strings);
98
+ check_strvec(&vec, "foo", "bar", "baz", NULL);
99
105
- strvec_clear(&vec);
106
-}
100
+ strvec_clear(&vec);
101
+ }
102
108
-static void t_replace_at_head(void)
109
-{
110
- struct strvec vec = STRVEC_INIT;
111
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
112
- strvec_replace(&vec, 0, "replaced");
113
- check_strvec(&vec, "replaced", "bar", "baz", NULL);
114
- strvec_clear(&vec);
115
-}
103
+ if_test ("replace at head") {
104
+ struct strvec vec = STRVEC_INIT;
105
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
106
+ strvec_replace(&vec, 0, "replaced");
107
+ check_strvec(&vec, "replaced", "bar", "baz", NULL);
108
+ strvec_clear(&vec);
109
+ }
110
117
-static void t_replace_at_tail(void)
118
-{
119
- struct strvec vec = STRVEC_INIT;
120
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
121
- strvec_replace(&vec, 2, "replaced");
122
- check_strvec(&vec, "foo", "bar", "replaced", NULL);
123
- strvec_clear(&vec);
124
-}
111
+ if_test ("replace at tail") {
112
+ struct strvec vec = STRVEC_INIT;
113
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
114
+ strvec_replace(&vec, 2, "replaced");
115
+ check_strvec(&vec, "foo", "bar", "replaced", NULL);
116
+ strvec_clear(&vec);
117
+ }
118
126
-static void t_replace_in_between(void)
127
-{
128
- struct strvec vec = STRVEC_INIT;
129
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
130
- strvec_replace(&vec, 1, "replaced");
131
- check_strvec(&vec, "foo", "replaced", "baz", NULL);
132
- strvec_clear(&vec);
133
-}
119
+ if_test ("replace in between") {
120
+ struct strvec vec = STRVEC_INIT;
121
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
122
+ strvec_replace(&vec, 1, "replaced");
123
+ check_strvec(&vec, "foo", "replaced", "baz", NULL);
124
+ strvec_clear(&vec);
125
+ }
126
135
-static void t_replace_with_substring(void)
136
-{
137
- struct strvec vec = STRVEC_INIT;
138
- strvec_pushl(&vec, "foo", NULL);
139
- strvec_replace(&vec, 0, vec.v[0] + 1);
140
- check_strvec(&vec, "oo", NULL);
141
- strvec_clear(&vec);
142
-}
127
+ if_test ("replace with substring") {
128
+ struct strvec vec = STRVEC_INIT;
129
+ strvec_pushl(&vec, "foo", NULL);
130
+ strvec_replace(&vec, 0, vec.v[0] + 1);
131
+ check_strvec(&vec, "oo", NULL);
132
+ strvec_clear(&vec);
133
+ }
134
144
-static void t_remove_at_head(void)
145
-{
146
- struct strvec vec = STRVEC_INIT;
147
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
148
- strvec_remove(&vec, 0);
149
- check_strvec(&vec, "bar", "baz", NULL);
150
- strvec_clear(&vec);
151
-}
135
+ if_test ("remove at head") {
136
+ struct strvec vec = STRVEC_INIT;
137
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
138
+ strvec_remove(&vec, 0);
139
+ check_strvec(&vec, "bar", "baz", NULL);
140
+ strvec_clear(&vec);
141
+ }
142
153
-static void t_remove_at_tail(void)
154
-{
155
- struct strvec vec = STRVEC_INIT;
156
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
157
- strvec_remove(&vec, 2);
158
- check_strvec(&vec, "foo", "bar", NULL);
159
- strvec_clear(&vec);
160
-}
143
+ if_test ("remove at tail") {
144
+ struct strvec vec = STRVEC_INIT;
145
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
146
+ strvec_remove(&vec, 2);
147
+ check_strvec(&vec, "foo", "bar", NULL);
148
+ strvec_clear(&vec);
149
+ }
150
162
-static void t_remove_in_between(void)
163
-{
164
- struct strvec vec = STRVEC_INIT;
165
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
166
- strvec_remove(&vec, 1);
167
- check_strvec(&vec, "foo", "baz", NULL);
168
- strvec_clear(&vec);
169
-}
151
+ if_test ("remove in between") {
152
+ struct strvec vec = STRVEC_INIT;
153
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
154
+ strvec_remove(&vec, 1);
155
+ check_strvec(&vec, "foo", "baz", NULL);
156
+ strvec_clear(&vec);
157
+ }
158
171
-static void t_pop_empty_array(void)
172
-{
173
- struct strvec vec = STRVEC_INIT;
174
- strvec_pop(&vec);
175
- check_strvec(&vec, NULL);
176
- strvec_clear(&vec);
177
-}
159
+ if_test ("pop with empty array") {
160
+ struct strvec vec = STRVEC_INIT;
161
+ strvec_pop(&vec);
162
+ check_strvec(&vec, NULL);
163
+ strvec_clear(&vec);
164
+ }
165
179
-static void t_pop_non_empty_array(void)
180
-{
181
- struct strvec vec = STRVEC_INIT;
182
- strvec_pushl(&vec, "foo", "bar", "baz", NULL);
183
- strvec_pop(&vec);
184
- check_strvec(&vec, "foo", "bar", NULL);
185
- strvec_clear(&vec);
186
-}
166
+ if_test ("pop with non-empty array") {
167
+ struct strvec vec = STRVEC_INIT;
168
+ strvec_pushl(&vec, "foo", "bar", "baz", NULL);
169
+ strvec_pop(&vec);
170
+ check_strvec(&vec, "foo", "bar", NULL);
171
+ strvec_clear(&vec);
172
+ }
173
188
-static void t_split_empty_string(void)
189
-{
190
- struct strvec vec = STRVEC_INIT;
191
- strvec_split(&vec, "");
192
- check_strvec(&vec, NULL);
193
- strvec_clear(&vec);
194
-}
174
+ if_test ("split empty string") {
175
+ struct strvec vec = STRVEC_INIT;
176
+ strvec_split(&vec, "");
177
+ check_strvec(&vec, NULL);
178
+ strvec_clear(&vec);
179
+ }
180
196
-static void t_split_single_item(void)
197
-{
198
- struct strvec vec = STRVEC_INIT;
199
- strvec_split(&vec, "foo");
200
- check_strvec(&vec, "foo", NULL);
201
- strvec_clear(&vec);
202
-}
181
+ if_test ("split single item") {
182
+ struct strvec vec = STRVEC_INIT;
183
+ strvec_split(&vec, "foo");
184
+ check_strvec(&vec, "foo", NULL);
185
+ strvec_clear(&vec);
186
+ }
187
204
-static void t_split_multiple_items(void)
205
-{
206
- struct strvec vec = STRVEC_INIT;
207
- strvec_split(&vec, "foo bar baz");
208
- check_strvec(&vec, "foo", "bar", "baz", NULL);
209
- strvec_clear(&vec);
210
-}
188
+ if_test ("split multiple items") {
189
+ struct strvec vec = STRVEC_INIT;
190
+ strvec_split(&vec, "foo bar baz");
191
+ check_strvec(&vec, "foo", "bar", "baz", NULL);
192
+ strvec_clear(&vec);
193
+ }
194
212
-static void t_split_whitespace_only(void)
213
-{
214
- struct strvec vec = STRVEC_INIT;
215
- strvec_split(&vec, " \t\n");
216
- check_strvec(&vec, NULL);
217
- strvec_clear(&vec);
218
-}
195
+ if_test ("split whitespace only") {
196
+ struct strvec vec = STRVEC_INIT;
197
+ strvec_split(&vec, " \t\n");
198
+ check_strvec(&vec, NULL);
199
+ strvec_clear(&vec);
200
+ }
201
220
-static void t_split_multiple_consecutive_whitespaces(void)
221
-{
222
- struct strvec vec = STRVEC_INIT;
223
- strvec_split(&vec, "foo\n\t bar");
224
- check_strvec(&vec, "foo", "bar", NULL);
225
- strvec_clear(&vec);
226
-}
202
+ if_test ("split multiple consecutive whitespaces") {
203
+ struct strvec vec = STRVEC_INIT;
204
+ strvec_split(&vec, "foo\n\t bar");
205
+ check_strvec(&vec, "foo", "bar", NULL);
206
+ strvec_clear(&vec);
207
+ }
208
228
-static void t_detach(void)
229
-{
230
- struct strvec vec = STRVEC_INIT;
231
- const char **detached;
209
+ if_test ("detach") {
210
+ struct strvec vec = STRVEC_INIT;
211
+ const char **detached;
212
233
- strvec_push(&vec, "foo");
213
+ strvec_push(&vec, "foo");
214
235
- detached = strvec_detach(&vec);
236
- check_str(detached[0], "foo");
237
- check_pointer_eq(detached[1], NULL);
215
+ detached = strvec_detach(&vec);
216
+ check_str(detached[0], "foo");
217
+ check_pointer_eq(detached[1], NULL);
218
239
- check_pointer_eq(vec.v, empty_strvec);
240
- check_uint(vec.nr, ==, 0);
241
- check_uint(vec.alloc, ==, 0);
219
+ check_pointer_eq(vec.v, empty_strvec);
220
+ check_uint(vec.nr, ==, 0);
221
+ check_uint(vec.alloc, ==, 0);
222
243
- free((char *) detached[0]);
244
- free(detached);
245
-}
223
+ free((char *) detached[0]);
224
+ free(detached);
225
+ }
226
247
-int cmd_main(int argc, const char **argv)
248
-{
249
- TEST(t_static_init(), "static initialization");
250
- TEST(t_dynamic_init(), "dynamic initialization");
251
- TEST(t_clear(), "clear");
252
- TEST(t_push(), "push");
253
- TEST(t_pushf(), "pushf");
254
- TEST(t_pushl(), "pushl");
255
- TEST(t_pushv(), "pushv");
256
- TEST(t_replace_at_head(), "replace at head");
257
- TEST(t_replace_in_between(), "replace in between");
258
- TEST(t_replace_at_tail(), "replace at tail");
259
- TEST(t_replace_with_substring(), "replace with substring");
260
- TEST(t_remove_at_head(), "remove at head");
261
- TEST(t_remove_in_between(), "remove in between");
262
- TEST(t_remove_at_tail(), "remove at tail");
263
- TEST(t_pop_empty_array(), "pop with empty array");
264
- TEST(t_pop_non_empty_array(), "pop with non-empty array");
265
- TEST(t_split_empty_string(), "split empty string");
266
- TEST(t_split_single_item(), "split single item");
267
- TEST(t_split_multiple_items(), "split multiple items");
268
- TEST(t_split_whitespace_only(), "split whitespace only");
269
- TEST(t_split_multiple_consecutive_whitespaces(), "split multiple consecutive whitespaces");
270
- TEST(t_detach(), "detach");
227
return test_done();
228
}