tal/path: handle weird case of path_join("")
[ccan] / ccan / tal / path / test / run-join.c
1 #include <ccan/tal/path/path.h>
2 #include <ccan/tal/path/path.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         char *path, *ctx = tal_strdup(NULL, "ctx");
8
9         plan_tests(36);
10
11         path = path_join(ctx, "foo", "bar");
12         ok1(streq(path, "foo/bar"));
13         ok1(tal_parent(path) == ctx);
14         tal_free(path);
15
16         path = path_join(ctx, "foo/", "bar");
17         ok1(streq(path, "foo/bar"));
18         ok1(tal_parent(path) == ctx);
19         tal_free(path);
20
21         path = path_join(ctx, "foo/", "/bar");
22         ok1(streq(path, "/bar"));
23         ok1(tal_parent(path) == ctx);
24         tal_free(path);
25
26         path = path_join(ctx, "foo", "/bar");
27         ok1(streq(path, "/bar"));
28         ok1(tal_parent(path) == ctx);
29         tal_free(path);
30
31         /* Test take */
32         path = path_join(ctx, "foo", take(tal_strdup(ctx, "bar")));
33         ok1(streq(path, "foo/bar"));
34         ok1(tal_parent(path) == ctx);
35         ok1(tal_first(ctx) == path && tal_next(path) == NULL && tal_first(path) == NULL);
36         tal_free(path);
37
38         path = path_join(ctx, "foo", take(tal_strdup(ctx, "/bar")));
39         ok1(streq(path, "/bar"));
40         ok1(tal_parent(path) == ctx);
41         ok1(tal_first(ctx) == path && tal_next(path) == NULL && tal_first(path) == NULL);
42         tal_free(path);
43
44         path = path_join(ctx, take(tal_strdup(ctx, "foo")), "bar");
45         ok1(streq(path, "foo/bar"));
46         ok1(tal_parent(path) == ctx);
47         ok1(tal_first(ctx) == path && tal_next(path) == NULL && tal_first(path) == NULL);
48         tal_free(path);
49
50         path = path_join(ctx, take(tal_strdup(ctx, "foo")), "/bar");
51         ok1(streq(path, "/bar"));
52         ok1(tal_parent(path) == ctx);
53         ok1(tal_first(ctx) == path && tal_next(path) == NULL && tal_first(path) == NULL);
54         tal_free(path);
55
56         path = path_join(ctx, take(tal_strdup(ctx, "foo")),
57                          take(tal_strdup(ctx, "bar")));
58         ok1(streq(path, "foo/bar"));
59         ok1(tal_parent(path) == ctx);
60         ok1(tal_first(ctx) == path && tal_next(path) == NULL && tal_first(path) == NULL);
61         tal_free(path);
62
63         path = path_join(ctx, take(tal_strdup(ctx, "foo")),
64                          take(tal_strdup(ctx, "/bar")));
65         ok1(streq(path, "/bar"));
66         ok1(tal_parent(path) == ctx);
67         ok1(tal_first(ctx) == path && tal_next(path) == NULL && tal_first(path) == NULL);
68         tal_free(path);
69
70         path = path_join(ctx, take(NULL), "bar");
71         ok1(!path);
72         ok1(!tal_first(ctx));
73
74         /* This is allowed to succeed, as first arg unneeded. */
75         path = path_join(ctx, take(NULL), "/bar");
76         ok1(!path || streq(path, "/bar"));
77         tal_free(path);
78         ok1(!tal_first(ctx));
79
80         path = path_join(ctx, "foo", take(NULL));
81         ok1(!path);
82         ok1(!tal_first(ctx));
83
84         path = path_join(ctx, take(NULL), take(NULL));
85         ok1(!path);
86         ok1(!tal_first(ctx));
87
88         path = path_join(ctx, "", "bar");
89         ok1(streq(path, "bar"));
90         ok1(tal_parent(path) == ctx);
91         tal_free(path);
92
93         tal_free(ctx);
94
95         return exit_status();
96 }