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

shejialuo committed Jun 29, 2025 at 12:28 UTC 62c514a9efd2206e081509ca3abc9cd5645eff0b
3 files changed +37 -73
t/helper/test-string-list.c
-22
@@ -18,13 +18,6 @@ static void parse_string_list(struct string_list *list, const char *arg)
18 (void)string_list_split(list, arg, ':', -1);
19 }
20
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 -
21 static void write_list_compact(const struct string_list *list)
22 {
23 int i;
@@ -46,21 +39,6 @@ static int prefix_cb(struct string_list_item *item, void *cb_data)
39
40 int cmd__string_list(int argc, const char **argv)
41 {
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 -
42 if (argc == 4 && !strcmp(argv[1], "filter")) {
43 /*
44 * Retain only the items that have the specified prefix.
t/t0063-string-list.sh
-51
@@ -7,57 +7,6 @@ test_description='Test string list functionality'
7
8 . ./test-lib.sh
9
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 -
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)" &&
t/unit-tests/u-string-list.c
+37
@@ -53,3 +53,40 @@ void test_string_list__split(void)
53 t_string_list_split("", ':', -1, "", NULL);
54 t_string_list_split(":", ':', -1, "", "", NULL);
55 }
56 +
57 +static void t_string_list_split_in_place(const char *data, const char *delim,
58 + int maxsplit, ...)
59 +{
60 + struct string_list expected_strings = STRING_LIST_INIT_DUP;
61 + struct string_list list = STRING_LIST_INIT_NODUP;
62 + char *string = xstrdup(data);
63 + va_list ap;
64 + int len;
65 +
66 + va_start(ap, maxsplit);
67 + t_vcreate_string_list_dup(&expected_strings, 0, ap);
68 + va_end(ap);
69 +
70 + string_list_clear(&list, 0);
71 + len = string_list_split_in_place(&list, string, delim, maxsplit);
72 + cl_assert_equal_i(len, expected_strings.nr);
73 + t_string_list_equal(&list, &expected_strings);
74 +
75 + free(string);
76 + string_list_clear(&expected_strings, 0);
77 + string_list_clear(&list, 0);
78 +}
79 +
80 +void test_string_list__split_in_place(void)
81 +{
82 + t_string_list_split_in_place("foo:;:bar:;:baz:;:", ":;", -1,
83 + "foo", "", "", "bar", "", "", "baz", "", "", "", NULL);
84 + t_string_list_split_in_place("foo:;:bar:;:baz", ":;", 0,
85 + "foo:;:bar:;:baz", NULL);
86 + t_string_list_split_in_place("foo:;:bar:;:baz", ":;", 1,
87 + "foo", ";:bar:;:baz", NULL);
88 + t_string_list_split_in_place("foo:;:bar:;:baz", ":;", 2,
89 + "foo", "", ":bar:;:baz", NULL);
90 + t_string_list_split_in_place("foo:;:bar:;:", ":;", -1,
91 + "foo", "", "", "bar", "", "", "", NULL);
92 +}