Skip to content

Commit 7e7ce78

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 62c514a commit 7e7ce78

File tree

3 files changed

+73
-32
lines changed

3 files changed

+73
-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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ 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+
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+
1636
static void t_string_list_equal(struct string_list *list,
1737
struct string_list *expected_strings)
1838
{
@@ -90,3 +110,56 @@ void test_string_list__split_in_place(void)
90110
t_string_list_split_in_place("foo:;:bar:;:", ":;", -1,
91111
"foo", "", "", "bar", "", "", "", NULL);
92112
}
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+
}

0 commit comments

Comments
 (0)