Skip to content

Commit 5daa9bc

Browse files
shejialuogitster
authored andcommitted
u-string-list: move "test_split_in_place" to "u-string-list.c"
We use "test-tool string-list split_in_place" to test the "string_list_split_in_place" 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 11a3324 commit 5daa9bc

File tree

3 files changed

+39
-73
lines changed

3 files changed

+39
-73
lines changed

t/helper/test-string-list.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ static void parse_string_list(struct string_list *list, const char *arg)
1818
(void)string_list_split(list, arg, ':', -1);
1919
}
2020

21-
static void write_list(const struct string_list *list)
22-
{
23-
int i;
24-
for (i = 0; i < list->nr; i++)
25-
printf("[%d]: \"%s\"\n", i, list->items[i].string);
26-
}
27-
2821
static void write_list_compact(const struct string_list *list)
2922
{
3023
int i;
@@ -46,21 +39,6 @@ static int prefix_cb(struct string_list_item *item, void *cb_data)
4639

4740
int cmd__string_list(int argc, const char **argv)
4841
{
49-
if (argc == 5 && !strcmp(argv[1], "split_in_place")) {
50-
struct string_list list = STRING_LIST_INIT_NODUP;
51-
int i;
52-
char *s = xstrdup(argv[2]);
53-
const char *delim = argv[3];
54-
int maxsplit = atoi(argv[4]);
55-
56-
i = string_list_split_in_place(&list, s, delim, maxsplit);
57-
printf("%d\n", i);
58-
write_list(&list);
59-
string_list_clear(&list, 0);
60-
free(s);
61-
return 0;
62-
}
63-
6442
if (argc == 4 && !strcmp(argv[1], "filter")) {
6543
/*
6644
* Retain only the items that have the specified prefix.

t/t0063-string-list.sh

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

88
. ./test-lib.sh
99

10-
test_split_in_place() {
11-
cat >expected &&
12-
test_expect_success "split (in place) $1 at $2, max $3" "
13-
test-tool string-list split_in_place '$1' '$2' '$3' >actual &&
14-
test_cmp expected actual
15-
"
16-
}
17-
18-
test_split_in_place "foo:;:bar:;:baz:;:" ":;" "-1" <<EOF
19-
10
20-
[0]: "foo"
21-
[1]: ""
22-
[2]: ""
23-
[3]: "bar"
24-
[4]: ""
25-
[5]: ""
26-
[6]: "baz"
27-
[7]: ""
28-
[8]: ""
29-
[9]: ""
30-
EOF
31-
32-
test_split_in_place "foo:;:bar:;:baz" ":;" "0" <<EOF
33-
1
34-
[0]: "foo:;:bar:;:baz"
35-
EOF
36-
37-
test_split_in_place "foo:;:bar:;:baz" ":;" "1" <<EOF
38-
2
39-
[0]: "foo"
40-
[1]: ";:bar:;:baz"
41-
EOF
42-
43-
test_split_in_place "foo:;:bar:;:baz" ":;" "2" <<EOF
44-
3
45-
[0]: "foo"
46-
[1]: ""
47-
[2]: ":bar:;:baz"
48-
EOF
49-
50-
test_split_in_place "foo:;:bar:;:" ":;" "-1" <<EOF
51-
7
52-
[0]: "foo"
53-
[1]: ""
54-
[2]: ""
55-
[3]: "bar"
56-
[4]: ""
57-
[5]: ""
58-
[6]: ""
59-
EOF
60-
6110
test_expect_success "test filter_string_list" '
6211
test "x-" = "x$(test-tool string-list filter - y)" &&
6312
test "x-" = "x$(test-tool string-list filter no y)" &&

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,42 @@ void test_string_list__split(void)
8484

8585
t_string_list_clear(&expected_strings, 0);
8686
}
87+
88+
static void t_string_list_split_in_place(const char *data, const char *delim, int maxsplit,
89+
struct string_list *expected_strings)
90+
{
91+
struct string_list list = STRING_LIST_INIT_NODUP;
92+
93+
char *string = xstrdup(data);
94+
95+
int len = string_list_split_in_place(&list, string, delim, maxsplit);
96+
cl_assert_equal_i(len, expected_strings->nr);
97+
t_check_string_list(&list, expected_strings);
98+
99+
free(string);
100+
t_string_list_clear(&list, 0);
101+
}
102+
103+
void test_string_list__split_in_place(void)
104+
{
105+
struct string_list expected_strings = STRING_LIST_INIT_DUP;
106+
107+
t_create_string_list_dup(&expected_strings, 0, "foo", "", "", "bar",
108+
"", "", "baz", "", "", "", NULL);
109+
t_string_list_split_in_place("foo:;:bar:;:baz:;:", ":;", -1, &expected_strings);
110+
111+
t_create_string_list_dup(&expected_strings, 0, "foo:;:bar:;:baz", NULL);
112+
t_string_list_split_in_place("foo:;:bar:;:baz", ":;", 0, &expected_strings);
113+
114+
t_create_string_list_dup(&expected_strings, 0, "foo", ";:bar:;:baz", NULL);
115+
t_string_list_split_in_place("foo:;:bar:;:baz", ":;", 1, &expected_strings);
116+
117+
t_create_string_list_dup(&expected_strings, 0, "foo", "", ":bar:;:baz", NULL);
118+
t_string_list_split_in_place("foo:;:bar:;:baz", ":;", 2, &expected_strings);
119+
120+
t_create_string_list_dup(&expected_strings, 0, "foo", "", "", "bar",
121+
"", "", "", NULL);
122+
t_string_list_split_in_place("foo:;:bar:;:", ":;", -1, &expected_strings);
123+
124+
t_string_list_clear(&expected_strings, 0);
125+
}

0 commit comments

Comments
 (0)