Skip to content

Commit 2794105

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 9e4b194 commit 2794105

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
@@ -64,3 +64,42 @@ void test_string_list__split(void)
6464

6565
t_string_list_clear(&list, 0);
6666
}
67+
68+
static void t_string_list_split_in_place(struct string_list *list, const char *data,
69+
const char *delim, int maxsplit, ...)
70+
{
71+
struct string_list expected_strings = STRING_LIST_INIT_DUP;
72+
char *string = xstrdup(data);
73+
va_list ap;
74+
int len;
75+
76+
va_start(ap, maxsplit);
77+
t_vcreate_string_list_dup(&expected_strings, 0, ap);
78+
va_end(ap);
79+
80+
string_list_clear(list, 0);
81+
len = string_list_split_in_place(list, string, delim, maxsplit);
82+
cl_assert_equal_i(len, expected_strings.nr);
83+
t_string_list_equal(list, &expected_strings);
84+
85+
free(string);
86+
string_list_clear(&expected_strings, 0);
87+
}
88+
89+
void test_string_list__split_in_place(void)
90+
{
91+
struct string_list list = STRING_LIST_INIT_NODUP;
92+
93+
t_string_list_split_in_place(&list, "foo:;:bar:;:baz:;:", ":;", -1,
94+
"foo", "", "", "bar", "", "", "baz", "", "", "", NULL);
95+
t_string_list_split_in_place(&list, "foo:;:bar:;:baz", ":;", 0,
96+
"foo:;:bar:;:baz", NULL);
97+
t_string_list_split_in_place(&list, "foo:;:bar:;:baz", ":;", 1,
98+
"foo", ";:bar:;:baz", NULL);
99+
t_string_list_split_in_place(&list, "foo:;:bar:;:baz", ":;", 2,
100+
"foo", "", ":bar:;:baz", NULL);
101+
t_string_list_split_in_place(&list, "foo:;:bar:;:", ":;", -1,
102+
"foo", "", "", "bar", "", "", "", NULL);
103+
104+
t_string_list_clear(&list, 0);
105+
}

0 commit comments

Comments
 (0)