Skip to content

Commit 9e4b194

Browse files
shejialuogitster
authored andcommitted
u-string-list: move "test_split" into "u-string-list.c"
We rely on "test-tool string-list" command to test the functionality of the "string-list". However, as we have introduced clar test framework, we'd better move the shell script into C program to improve speed and readability. Create a new file "u-string-list.c" under "t/unit-tests", then update the Makefile and "meson.build" to build the file. And let's first move "test_split" into unit test and gradually convert the shell script into C program. In order to create `string_list` easily by simply specifying strings in the function call, create "t_vcreate_string_list_dup" function to do this. Then port the shell script tests to C program and remove unused "test-tool" code and tests. Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dabb506 commit 9e4b194

File tree

5 files changed

+68
-67
lines changed

5 files changed

+68
-67
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ CLAR_TEST_SUITES += u-prio-queue
13631363
CLAR_TEST_SUITES += u-reftable-tree
13641364
CLAR_TEST_SUITES += u-strbuf
13651365
CLAR_TEST_SUITES += u-strcmp-offset
1366+
CLAR_TEST_SUITES += u-string-list
13661367
CLAR_TEST_SUITES += u-strvec
13671368
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13681369
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))

t/helper/test-string-list.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ static int prefix_cb(struct string_list_item *item, void *cb_data)
4646

4747
int cmd__string_list(int argc, const char **argv)
4848
{
49-
if (argc == 5 && !strcmp(argv[1], "split")) {
50-
struct string_list list = STRING_LIST_INIT_DUP;
51-
int i;
52-
const char *s = argv[2];
53-
int delim = *argv[3];
54-
int maxsplit = atoi(argv[4]);
55-
56-
i = string_list_split(&list, s, delim, maxsplit);
57-
printf("%d\n", i);
58-
write_list(&list);
59-
string_list_clear(&list, 0);
60-
return 0;
61-
}
62-
6349
if (argc == 5 && !strcmp(argv[1], "split_in_place")) {
6450
struct string_list list = STRING_LIST_INIT_NODUP;
6551
int i;

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ clar_test_suites = [
1111
'unit-tests/u-reftable-tree.c',
1212
'unit-tests/u-strbuf.c',
1313
'unit-tests/u-strcmp-offset.c',
14+
'unit-tests/u-string-list.c',
1415
'unit-tests/u-strvec.c',
1516
]
1617

t/t0063-string-list.sh

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

88
. ./test-lib.sh
99

10-
test_split () {
11-
cat >expected &&
12-
test_expect_success "split $1 at $2, max $3" "
13-
test-tool string-list split '$1' '$2' '$3' >actual &&
14-
test_cmp expected actual &&
15-
test-tool string-list split_in_place '$1' '$2' '$3' >actual &&
16-
test_cmp expected actual
17-
"
18-
}
19-
2010
test_split_in_place() {
2111
cat >expected &&
2212
test_expect_success "split (in place) $1 at $2, max $3" "
@@ -25,49 +15,6 @@ test_split_in_place() {
2515
"
2616
}
2717

28-
test_split "foo:bar:baz" ":" "-1" <<EOF
29-
3
30-
[0]: "foo"
31-
[1]: "bar"
32-
[2]: "baz"
33-
EOF
34-
35-
test_split "foo:bar:baz" ":" "0" <<EOF
36-
1
37-
[0]: "foo:bar:baz"
38-
EOF
39-
40-
test_split "foo:bar:baz" ":" "1" <<EOF
41-
2
42-
[0]: "foo"
43-
[1]: "bar:baz"
44-
EOF
45-
46-
test_split "foo:bar:baz" ":" "2" <<EOF
47-
3
48-
[0]: "foo"
49-
[1]: "bar"
50-
[2]: "baz"
51-
EOF
52-
53-
test_split "foo:bar:" ":" "-1" <<EOF
54-
3
55-
[0]: "foo"
56-
[1]: "bar"
57-
[2]: ""
58-
EOF
59-
60-
test_split "" ":" "-1" <<EOF
61-
1
62-
[0]: ""
63-
EOF
64-
65-
test_split ":" ":" "-1" <<EOF
66-
2
67-
[0]: ""
68-
[1]: ""
69-
EOF
70-
7118
test_split_in_place "foo:;:bar:;:baz:;:" ":;" "-1" <<EOF
7219
10
7320
[0]: "foo"

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "unit-test.h"
2+
#include "string-list.h"
3+
4+
static void t_vcreate_string_list_dup(struct string_list *list,
5+
int free_util, va_list ap)
6+
{
7+
const char *arg;
8+
9+
cl_assert(list->strdup_strings);
10+
11+
string_list_clear(list, free_util);
12+
while ((arg = va_arg(ap, const char *)))
13+
string_list_append(list, arg);
14+
}
15+
16+
static void t_string_list_clear(struct string_list *list, int free_util)
17+
{
18+
string_list_clear(list, free_util);
19+
cl_assert_equal_p(list->items, NULL);
20+
cl_assert_equal_i(list->nr, 0);
21+
cl_assert_equal_i(list->alloc, 0);
22+
}
23+
24+
static void t_string_list_equal(struct string_list *list,
25+
struct string_list *expected_strings)
26+
{
27+
cl_assert_equal_i(list->nr, expected_strings->nr);
28+
cl_assert(list->nr <= list->alloc);
29+
for (size_t i = 0; i < expected_strings->nr; i++)
30+
cl_assert_equal_s(list->items[i].string,
31+
expected_strings->items[i].string);
32+
}
33+
34+
static void t_string_list_split(struct string_list *list, const char *data,
35+
int delim, int maxsplit, ...)
36+
{
37+
struct string_list expected_strings = STRING_LIST_INIT_DUP;
38+
va_list ap;
39+
int len;
40+
41+
va_start(ap, maxsplit);
42+
t_vcreate_string_list_dup(&expected_strings, 0, ap);
43+
va_end(ap);
44+
45+
string_list_clear(list, 0);
46+
len = string_list_split(list, data, delim, maxsplit);
47+
cl_assert_equal_i(len, expected_strings.nr);
48+
t_string_list_equal(list, &expected_strings);
49+
50+
string_list_clear(&expected_strings, 0);
51+
}
52+
53+
void test_string_list__split(void)
54+
{
55+
struct string_list list = STRING_LIST_INIT_DUP;
56+
57+
t_string_list_split(&list, "foo:bar:baz", ':', -1, "foo", "bar", "baz", NULL);
58+
t_string_list_split(&list, "foo:bar:baz", ':', 0, "foo:bar:baz", NULL);
59+
t_string_list_split(&list, "foo:bar:baz", ':', 1, "foo", "bar:baz", NULL);
60+
t_string_list_split(&list, "foo:bar:baz", ':', 2, "foo", "bar", "baz", NULL);
61+
t_string_list_split(&list, "foo:bar:", ':', -1, "foo", "bar", "", NULL);
62+
t_string_list_split(&list, "", ':', -1, "", NULL);
63+
t_string_list_split(&list, ":", ':', -1, "", "", NULL);
64+
65+
t_string_list_clear(&list, 0);
66+
}

0 commit comments

Comments
 (0)