Skip to content

Commit 4421ed8

Browse files
shejialuogitster
authored andcommitted
u-string-list: move "filter string" test to "u-string-list.c"
We use "test-tool string-list filter" to test the "filter_string_list" function. As we have introduced the unit test, we'd better remove the logic from shell script to C program to improve test speed and readability. Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2794105 commit 4421ed8

File tree

3 files changed

+66
-32
lines changed

3 files changed

+66
-32
lines changed

t/helper/test-string-list.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,8 @@ static void write_list_compact(const struct string_list *list)
3131
}
3232
}
3333

34-
static int prefix_cb(struct string_list_item *item, void *cb_data)
35-
{
36-
const char *prefix = (const char *)cb_data;
37-
return starts_with(item->string, prefix);
38-
}
39-
4034
int cmd__string_list(int argc, const char **argv)
4135
{
42-
if (argc == 4 && !strcmp(argv[1], "filter")) {
43-
/*
44-
* Retain only the items that have the specified prefix.
45-
* Arguments: list|- prefix
46-
*/
47-
struct string_list list = STRING_LIST_INIT_DUP;
48-
const char *prefix = argv[3];
49-
50-
parse_string_list(&list, argv[2]);
51-
filter_string_list(&list, 0, prefix_cb, (void *)prefix);
52-
write_list_compact(&list);
53-
string_list_clear(&list, 0);
54-
return 0;
55-
}
56-
5736
if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) {
5837
struct string_list list = STRING_LIST_INIT_DUP;
5938

t/t0063-string-list.sh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ test_description='Test string list functionality'
77

88
. ./test-lib.sh
99

10-
test_expect_success "test filter_string_list" '
11-
test "x-" = "x$(test-tool string-list filter - y)" &&
12-
test "x-" = "x$(test-tool string-list filter no y)" &&
13-
test yes = "$(test-tool string-list filter yes y)" &&
14-
test yes = "$(test-tool string-list filter no:yes y)" &&
15-
test yes = "$(test-tool string-list filter yes:no y)" &&
16-
test y1:y2 = "$(test-tool string-list filter y1:y2 y)" &&
17-
test y2:y1 = "$(test-tool string-list filter y2:y1 y)" &&
18-
test "x-" = "x$(test-tool string-list filter x1:x2 y)"
19-
'
20-
2110
test_expect_success "test remove_duplicates" '
2211
test "x-" = "x$(test-tool string-list remove_duplicates -)" &&
2312
test "x" = "x$(test-tool string-list remove_duplicates "")" &&

t/unit-tests/u-string-list.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ static void t_vcreate_string_list_dup(struct string_list *list,
1313
string_list_append(list, arg);
1414
}
1515

16+
static void t_create_string_list_dup(struct string_list *list, int free_util, ...)
17+
{
18+
va_list ap;
19+
20+
cl_assert(list->strdup_strings);
21+
22+
string_list_clear(list, free_util);
23+
va_start(ap, free_util);
24+
t_vcreate_string_list_dup(list, free_util, ap);
25+
va_end(ap);
26+
}
27+
1628
static void t_string_list_clear(struct string_list *list, int free_util)
1729
{
1830
string_list_clear(list, free_util);
@@ -103,3 +115,57 @@ void test_string_list__split_in_place(void)
103115

104116
t_string_list_clear(&list, 0);
105117
}
118+
119+
static int prefix_cb(struct string_list_item *item, void *cb_data)
120+
{
121+
const char *prefix = (const char *)cb_data;
122+
return starts_with(item->string, prefix);
123+
}
124+
125+
static void t_string_list_filter(struct string_list *list,
126+
string_list_each_func_t want, void *cb_data, ...)
127+
{
128+
struct string_list expected_strings = STRING_LIST_INIT_DUP;
129+
va_list ap;
130+
131+
va_start(ap, cb_data);
132+
t_vcreate_string_list_dup(&expected_strings, 0, ap);
133+
va_end(ap);
134+
135+
filter_string_list(list, 0, want, cb_data);
136+
t_string_list_equal(list, &expected_strings);
137+
138+
string_list_clear(&expected_strings, 0);
139+
}
140+
141+
void test_string_list__filter(void)
142+
{
143+
struct string_list list = STRING_LIST_INIT_DUP;
144+
const char *prefix = "y";
145+
146+
t_create_string_list_dup(&list, 0, NULL);
147+
t_string_list_filter(&list, prefix_cb, (void*)prefix, NULL);
148+
149+
t_create_string_list_dup(&list, 0, "no", NULL);
150+
t_string_list_filter(&list, prefix_cb, (void*)prefix, NULL);
151+
152+
t_create_string_list_dup(&list, 0, "yes", NULL);
153+
t_string_list_filter(&list, prefix_cb, (void*)prefix, "yes", NULL);
154+
155+
t_create_string_list_dup(&list, 0, "no", "yes", NULL);
156+
t_string_list_filter(&list, prefix_cb, (void*)prefix, "yes", NULL);
157+
158+
t_create_string_list_dup(&list, 0, "yes", "no", NULL);
159+
t_string_list_filter(&list, prefix_cb, (void*)prefix, "yes", NULL);
160+
161+
t_create_string_list_dup(&list, 0, "y1", "y2", NULL);
162+
t_string_list_filter(&list, prefix_cb, (void*)prefix, "y1", "y2", NULL);
163+
164+
t_create_string_list_dup(&list, 0, "y2", "y1", NULL);
165+
t_string_list_filter(&list, prefix_cb, (void*)prefix, "y2", "y1", NULL);
166+
167+
t_create_string_list_dup(&list, 0, "x1", "x2", NULL);
168+
t_string_list_filter(&list, prefix_cb, (void*)prefix, NULL);
169+
170+
t_string_list_clear(&list, 0);
171+
}

0 commit comments

Comments
 (0)