json_escape: new module to escape JSON strings.
[ccan] / ccan / json_escape / test / run-partial.c
1 #include <ccan/json_escape/json_escape.h>
2 /* Include the C files directly. */
3 #include <ccan/json_escape/json_escape.c>
4 #include <ccan/tap/tap.h>
5
6 int main(void)
7 {
8         const tal_t *ctx = tal(NULL, char);
9
10         /* This is how many tests you plan to run */
11         plan_tests(21);
12
13         ok1(!strcmp(json_partial_escape(ctx, "\\")->s, "\\\\"));
14         ok1(!strcmp(json_partial_escape(ctx, "\\\\")->s, "\\\\"));
15         ok1(!strcmp(json_partial_escape(ctx, "\\\\\\")->s, "\\\\\\\\"));
16         ok1(!strcmp(json_partial_escape(ctx, "\\\\\\\\")->s, "\\\\\\\\"));
17         ok1(!strcmp(json_partial_escape(ctx, "\\n")->s, "\\n"));
18         ok1(!strcmp(json_partial_escape(ctx, "\n")->s, "\\n"));
19         ok1(!strcmp(json_partial_escape(ctx, "\\\"")->s, "\\\""));
20         ok1(!strcmp(json_partial_escape(ctx, "\"")->s, "\\\""));
21         ok1(!strcmp(json_partial_escape(ctx, "\\t")->s, "\\t"));
22         ok1(!strcmp(json_partial_escape(ctx, "\t")->s, "\\t"));
23         ok1(!strcmp(json_partial_escape(ctx, "\\b")->s, "\\b"));
24         ok1(!strcmp(json_partial_escape(ctx, "\b")->s, "\\b"));
25         ok1(!strcmp(json_partial_escape(ctx, "\\r")->s, "\\r"));
26         ok1(!strcmp(json_partial_escape(ctx, "\r")->s, "\\r"));
27         ok1(!strcmp(json_partial_escape(ctx, "\\f")->s, "\\f"));
28         ok1(!strcmp(json_partial_escape(ctx, "\f")->s, "\\f"));
29         /* You're allowed to escape / according to json.org. */
30         ok1(!strcmp(json_partial_escape(ctx, "\\/")->s, "\\/"));
31         ok1(!strcmp(json_partial_escape(ctx, "/")->s, "/"));
32
33         ok1(!strcmp(json_partial_escape(ctx, "\\u0FFF")->s, "\\u0FFF"));
34         ok1(!strcmp(json_partial_escape(ctx, "\\u0FFFx")->s, "\\u0FFFx"));
35
36         /* Unknown escapes should be escaped. */
37         ok1(!strcmp(json_partial_escape(ctx, "\\x")->s, "\\\\x"));
38         tal_free(ctx);
39
40         return 0;
41 }