Skip to content

Commit 85aabec

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 5daa9bc commit 85aabec

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,54 @@ void test_string_list__split_in_place(void)
123123

124124
t_string_list_clear(&expected_strings, 0);
125125
}
126+
127+
static int prefix_cb(struct string_list_item *item, void *cb_data)
128+
{
129+
const char *prefix = (const char *)cb_data;
130+
return starts_with(item->string, prefix);
131+
}
132+
133+
static void t_string_list_filter(struct string_list *list,
134+
string_list_each_func_t want, void *cb_data,
135+
struct string_list *expected_strings)
136+
{
137+
filter_string_list(list, 0, want, cb_data);
138+
t_check_string_list(list, expected_strings);
139+
}
140+
141+
void test_string_list__filter(void)
142+
{
143+
struct string_list expected_strings = STRING_LIST_INIT_DUP;
144+
struct string_list list = STRING_LIST_INIT_DUP;
145+
const char *prefix = "y";
146+
147+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
148+
149+
t_create_string_list_dup(&list, 0, "no", NULL);
150+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
151+
152+
t_create_string_list_dup(&list, 0, "yes", NULL);
153+
t_create_string_list_dup(&expected_strings, 0, "yes", NULL);
154+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
155+
156+
t_create_string_list_dup(&list, 0, "no", "yes", NULL);
157+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
158+
159+
t_create_string_list_dup(&list, 0, "yes", "no", NULL);
160+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
161+
162+
t_create_string_list_dup(&list, 0, "y1", "y2", NULL);
163+
t_create_string_list_dup(&expected_strings, 0, "y1", "y2", NULL);
164+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
165+
166+
t_create_string_list_dup(&list, 0, "y2", "y1", NULL);
167+
t_create_string_list_dup(&expected_strings, 0, "y2", "y1", NULL);
168+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
169+
170+
t_create_string_list_dup(&list, 0, "x1", "x2", NULL);
171+
t_create_string_list_dup(&expected_strings, 0, NULL);
172+
t_string_list_filter(&list, prefix_cb, (void*)prefix, &expected_strings);
173+
174+
t_string_list_clear(&list, 0);
175+
t_string_list_clear(&expected_strings, 0);
176+
}

0 commit comments

Comments
 (0)