json_escape: new module to escape JSON strings.
[ccan] / ccan / json_escape / test / run.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         struct json_escape *e;
10
11         /* This is how many tests you plan to run */
12         plan_tests(6);
13
14         e = json_escape(ctx, "Hello");
15         ok1(!strcmp(e->s, "Hello"));
16         ok1(!strcmp(json_escape_unescape(ctx, e),
17                     "Hello"));
18
19         e = json_escape(ctx,
20                         "\\\b\f\n\r\t\""
21                         "\\\\\\b\\f\\n\\r\\t\\\"");
22         ok1(!strcmp(e->s,
23                     "\\\\\\b\\f\\n\\r\\t\\\""
24                     "\\\\\\\\\\\\b\\\\f\\\\n\\\\r\\\\t\\\\\\\""));
25         ok1(!strcmp(json_escape_unescape(ctx, e),
26                     "\\\b\f\n\r\t\""
27                     "\\\\\\b\\f\\n\\r\\t\\\""));
28
29         /* This one doesn't escape the already-escaped chars */
30         e = json_partial_escape(ctx,
31                                 "\\\b\f\n\r\t\""
32                                 "\\\\\\b\\f\\n\\r\\t\\\"");
33         ok1(!strcmp(e->s,
34                     "\\\\\\b\\f\\n\\r\\t\\\""
35                     "\\\\\\b\\f\\n\\r\\t\\\""));
36         ok1(!strcmp(json_escape_unescape(ctx, e),
37                     "\\\b\f\n\r\t\""
38                     "\\\b\f\n\r\t\""));
39
40         tal_free(ctx);
41
42         /* This exits depending on whether all tests passed */
43         return exit_status();
44 }