size: 2 KiB

1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <assert.h>
5#include "djot.h"
6
7int failed = 0;
8int num = 0;
9int result;
10
11static void asserteq(char *actual, char *expected) {
12 num = num + 1;
13 if (strcmp(actual, expected) == 0) {
14 printf("Test %4d PASSED\n", num);
15 } else {
16 printf("Test %4d FAILED\nExpected:\n%s\nGot:\n%s\n", num, expected, actual);
17 failed = failed + 1;
18 }
19}
20
21static int error(lua_State *L) {
22 djot_report_error(L);
23 exit(1);
24}
25
26int main (void) {
27 char *out;
28 int ok;
29
30 /* Do this once, before any use of the djot library */
31 lua_State *L = djot_open();
32 if (!L) {
33 fprintf(stderr, "djot_open returned NULL.\n");
34 exit(1);
35 }
36
37 out = djot_parse_and_render_events(L, "hi *there*\n");
38 if (!out) error(L);
39 asserteq(out,
40"[[\"+para\",1,1]\n\
41,[\"str\",1,3]\n\
42,[\"+strong\",4,4]\n\
43,[\"str\",5,9]\n\
44,[\"-strong\",10,10]\n\
45,[\"-para\",11,11]\n\
46]\n");
47
48 ok = djot_parse(L, "hi *there*\n", true);
49 if (!ok) error(L);
50 out = djot_render_html(L);
51 if (!out) error(L);
52 asserteq(out,
53"<p data-startpos=\"1:1:1\" data-endpos=\"1:11:11\">hi <strong data-startpos=\"1:4:4\" data-endpos=\"1:10:10\">there</strong></p>\n");
54
55 out = djot_render_ast_json(L);
56 if (!out) error(L);
57 asserteq(out,
58"{\"tag\":\"doc\",\"children\":[{\"tag\":\"para\",\"pos\":[\"1:1:1\",\"1:11:11\"],\"children\":[{\"tag\":\"str\",\"text\":\"hi \",\"pos\":[\"1:1:1\",\"1:3:3\"]},{\"tag\":\"strong\",\"pos\":[\"1:4:4\",\"1:10:10\"],\"children\":[{\"tag\":\"str\",\"text\":\"there\",\"pos\":[\"1:5:5\",\"1:9:9\"]}]}]}],\"references\":[],\"footnotes\":[]}\n");
59
60 char *capsfilter = "return {\n\
61str = function(e)\n\
62 e.text = e.text:upper()\n\
63end\n\
64}\n";
65
66 result = djot_apply_filter(L, capsfilter);
67 if (!result) {
68 error(L);
69 } else {
70 out = djot_render_html(L);
71 if (!out) error(L);
72 asserteq(out,
73"<p data-startpos=\"1:1:1\" data-endpos=\"1:11:11\">HI <strong data-startpos=\"1:4:4\" data-endpos=\"1:10:10\">THERE</strong></p>\n");
74 }
75
76 /* When you're finished, close the djot library */
77 djot_close(L);
78
79 if (failed) {
80 printf("%d tests failed.\n", failed);
81 } else {
82 printf("All good.\n");
83 }
84 return failed;
85}