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 <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed Jun 29, 2025 at 12:28 UTC 7e7ce7826534142c069758c13b2d308e901138c0
3 files changed +73 -32
t/helper/test-string-list.c
-21
@@ -31,29 +31,8 @@ static void write_list_compact(const struct string_list *list)
31 }
32 }
33
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 -
34 int cmd__string_list(int argc, const char **argv)
35 {
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 -
36 if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) {
37 struct string_list list = STRING_LIST_INIT_DUP;
38
t/t0063-string-list.sh
-11
@@ -7,17 +7,6 @@ test_description='Test string list functionality'
7
8 . ./test-lib.sh
9
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 -
10 test_expect_success "test remove_duplicates" '
11 test "x-" = "x$(test-tool string-list remove_duplicates -)" &&
12 test "x" = "x$(test-tool string-list remove_duplicates "")" &&
t/unit-tests/u-string-list.c
+73
@@ -13,6 +13,26 @@ static void t_vcreate_string_list_dup(struct string_list *list,
13 string_list_append(list, arg);
14 }
15
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 +
28 +static void t_string_list_clear(struct string_list *list, int free_util)
29 +{
30 + string_list_clear(list, free_util);
31 + cl_assert_equal_p(list->items, NULL);
32 + cl_assert_equal_i(list->nr, 0);
33 + cl_assert_equal_i(list->alloc, 0);
34 +}
35 +
36 static void t_string_list_equal(struct string_list *list,
37 struct string_list *expected_strings)
38 {
@@ -90,3 +110,56 @@ void test_string_list__split_in_place(void)
110 t_string_list_split_in_place("foo:;:bar:;:", ":;", -1,
111 "foo", "", "", "bar", "", "", "", NULL);
112 }
113 +
114 +static int prefix_cb(struct string_list_item *item, void *cb_data)
115 +{
116 + const char *prefix = (const char *)cb_data;
117 + return starts_with(item->string, prefix);
118 +}
119 +
120 +static void t_string_list_filter(struct string_list *list, ...)
121 +{
122 + struct string_list expected_strings = STRING_LIST_INIT_DUP;
123 + const char *prefix = "y";
124 + va_list ap;
125 +
126 + va_start(ap, list);
127 + t_vcreate_string_list_dup(&expected_strings, 0, ap);
128 + va_end(ap);
129 +
130 + filter_string_list(list, 0, prefix_cb, (void *)prefix);
131 + t_string_list_equal(list, &expected_strings);
132 +
133 + string_list_clear(&expected_strings, 0);
134 +}
135 +
136 +void test_string_list__filter(void)
137 +{
138 + struct string_list list = STRING_LIST_INIT_DUP;
139 +
140 + t_create_string_list_dup(&list, 0, NULL);
141 + t_string_list_filter(&list, NULL);
142 +
143 + t_create_string_list_dup(&list, 0, "no", NULL);
144 + t_string_list_filter(&list, NULL);
145 +
146 + t_create_string_list_dup(&list, 0, "yes", NULL);
147 + t_string_list_filter(&list, "yes", NULL);
148 +
149 + t_create_string_list_dup(&list, 0, "no", "yes", NULL);
150 + t_string_list_filter(&list, "yes", NULL);
151 +
152 + t_create_string_list_dup(&list, 0, "yes", "no", NULL);
153 + t_string_list_filter(&list, "yes", NULL);
154 +
155 + t_create_string_list_dup(&list, 0, "y1", "y2", NULL);
156 + t_string_list_filter(&list, "y1", "y2", NULL);
157 +
158 + t_create_string_list_dup(&list, 0, "y2", "y1", NULL);
159 + t_string_list_filter(&list, "y2", "y1", NULL);
160 +
161 + t_create_string_list_dup(&list, 0, "x1", "x2", NULL);
162 + t_string_list_filter(&list, NULL);
163 +
164 + t_string_list_clear(&list, 0);
165 +}