diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-11-22 10:28:16 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-11-22 10:28:16 -0800 |
commit | 36f6ab248ab8391bb6dbaab97e23010a9fb82ce8 (patch) | |
tree | 1414de21ed1017974e7ff30b6b0d4327cbfd0dd7 /api_test | |
parent | aebf7308ef00a8c2150200dc641d3eeee80a8303 (diff) | |
parent | b2dac901288a3431fdadd9bbefb7875596a13e4d (diff) |
Merge pull request #210 from nwellnhof/nwellnhof_141122
C API fixes, additional tests, stackless rendering
Diffstat (limited to 'api_test')
-rw-r--r-- | api_test/main.c | 269 |
1 files changed, 234 insertions, 35 deletions
diff --git a/api_test/main.c b/api_test/main.c index f74dee2..b3b5b5a 100644 --- a/api_test/main.c +++ b/api_test/main.c @@ -2,15 +2,81 @@ #include <stdlib.h> #include <string.h> +#define CMARK_NO_SHORT_NAMES #include "cmark.h" #include "node.h" #include "harness.h" +static const cmark_node_type node_types[] = { + CMARK_NODE_DOCUMENT, + CMARK_NODE_BQUOTE, + CMARK_NODE_LIST, + CMARK_NODE_LIST_ITEM, + CMARK_NODE_FENCED_CODE, + CMARK_NODE_INDENTED_CODE, + CMARK_NODE_HTML, + CMARK_NODE_PARAGRAPH, + CMARK_NODE_ATX_HEADER, + CMARK_NODE_SETEXT_HEADER, + CMARK_NODE_HRULE, + CMARK_NODE_REFERENCE_DEF, + CMARK_NODE_STRING, + CMARK_NODE_SOFTBREAK, + CMARK_NODE_LINEBREAK, + CMARK_NODE_INLINE_CODE, + CMARK_NODE_INLINE_HTML, + CMARK_NODE_EMPH, + CMARK_NODE_STRONG, + CMARK_NODE_LINK, + CMARK_NODE_IMAGE +}; +static const int num_node_types = sizeof(node_types) / sizeof(*node_types); + +static void +test_content(test_batch_runner *runner, cmark_node_type type, + int allowed_content); + +static void +constructor(test_batch_runner *runner) +{ + for (int i = 0; i < num_node_types; ++i) { + cmark_node_type type = node_types[i]; + cmark_node *node = cmark_node_new(type); + OK(runner, node != NULL, "new type %d", type); + INT_EQ(runner, cmark_node_get_type(node), type, + "get_type %d", type); + + switch (node->type) { + case CMARK_NODE_ATX_HEADER: + case CMARK_NODE_SETEXT_HEADER: + INT_EQ(runner, cmark_node_get_header_level(node), 1, + "default header level is 1"); + node->as.header.level = 1; + break; + + case CMARK_NODE_LIST: + INT_EQ(runner, cmark_node_get_list_type(node), + CMARK_BULLET_LIST, + "default is list type is bullet"); + INT_EQ(runner, cmark_node_get_list_start(node), 1, + "default is list start is 1"); + INT_EQ(runner, cmark_node_get_list_tight(node), 0, + "default is list is loose"); + break; + + default: + break; + } + + cmark_node_destroy(node); + } +} + static void accessors(test_batch_runner *runner) { - static const unsigned char markdown[] = + static const char markdown[] = "## Header\n" "\n" "* Item 1\n" @@ -36,22 +102,16 @@ accessors(test_batch_runner *runner) // Getters cmark_node *header = cmark_node_first_child(doc); - INT_EQ(runner, cmark_node_get_type(header), CMARK_NODE_ATX_HEADER, - "get_type header"); INT_EQ(runner, cmark_node_get_header_level(header), 2, "get_header_level"); cmark_node *bullet_list = cmark_node_next(header); - INT_EQ(runner, cmark_node_get_type(bullet_list), CMARK_NODE_LIST, - "get_type bullet list"); INT_EQ(runner, cmark_node_get_list_type(bullet_list), CMARK_BULLET_LIST, "get_list_type bullet"); INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1, "get_list_tight tight"); cmark_node *ordered_list = cmark_node_next(bullet_list); - INT_EQ(runner, cmark_node_get_type(ordered_list), CMARK_NODE_LIST, - "get_type ordered list"); INT_EQ(runner, cmark_node_get_list_type(ordered_list), CMARK_ORDERED_LIST, "get_list_type ordered"); INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2, @@ -60,28 +120,20 @@ accessors(test_batch_runner *runner) "get_list_tight loose"); cmark_node *code = cmark_node_next(ordered_list); - INT_EQ(runner, cmark_node_get_type(code), CMARK_NODE_INDENTED_CODE, - "get_type indented code"); STR_EQ(runner, cmark_node_get_string_content(code), "code\n", "get_string_content indented code"); cmark_node *fenced = cmark_node_next(code); - INT_EQ(runner, cmark_node_get_type(fenced), CMARK_NODE_FENCED_CODE, - "get_type fenced code"); STR_EQ(runner, cmark_node_get_string_content(fenced), "fenced\n", "get_string_content fenced code"); STR_EQ(runner, cmark_node_get_fence_info(fenced), "lang", "get_fence_info"); cmark_node *html = cmark_node_next(fenced); - INT_EQ(runner, cmark_node_get_type(html), CMARK_NODE_HTML, - "get_type html"); STR_EQ(runner, cmark_node_get_string_content(html), "<div>html</div>\n", "get_string_content html"); cmark_node *paragraph = cmark_node_next(html); - INT_EQ(runner, cmark_node_get_type(paragraph), CMARK_NODE_PARAGRAPH, - "get_type paragraph"); INT_EQ(runner, cmark_node_get_start_line(paragraph), 19, "get_start_line"); INT_EQ(runner, cmark_node_get_start_column(paragraph), 1, @@ -90,16 +142,12 @@ accessors(test_batch_runner *runner) "get_end_line"); cmark_node *link = cmark_node_first_child(paragraph); - INT_EQ(runner, cmark_node_get_type(link), CMARK_NODE_LINK, - "get_type link"); STR_EQ(runner, cmark_node_get_url(link), "url", "get_url"); STR_EQ(runner, cmark_node_get_title(link), "title", "get_title"); cmark_node *string = cmark_node_first_child(link); - INT_EQ(runner, cmark_node_get_type(string), CMARK_NODE_STRING, - "get_type string"); STR_EQ(runner, cmark_node_get_string_content(string), "link", "get_string_content string"); @@ -139,7 +187,7 @@ accessors(test_batch_runner *runner) OK(runner, cmark_node_set_string_content(string, "LINK"), "set_string_content string"); - char *rendered_html = (char *)cmark_render_html(doc); + char *rendered_html = cmark_render_html(doc); static const char expected_html[] = "<h3>Header</h3>\n" "<ol start=\"3\">\n" @@ -150,7 +198,7 @@ accessors(test_batch_runner *runner) "<p>Item 2</p>\n" "</li>\n" "</ol>\n" - "<ul start=\"2\">\n" + "<ul>\n" "<li>Item 1</li>\n" "<li>Item 2</li>\n" "</ul>\n" @@ -214,6 +262,22 @@ accessors(test_batch_runner *runner) } static void +node_check(test_batch_runner *runner) { + // Construct an incomplete tree. + cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT); + cmark_node *p1 = cmark_node_new(CMARK_NODE_PARAGRAPH); + cmark_node *p2 = cmark_node_new(CMARK_NODE_PARAGRAPH); + doc->first_child = p1; + p1->next = p2; + + INT_EQ(runner, cmark_node_check(doc, NULL), 4, "node_check works"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, + "node_check fixes tree"); + + cmark_node_destroy(doc); +} + +static void create_tree(test_batch_runner *runner) { char *html; @@ -221,66 +285,201 @@ create_tree(test_batch_runner *runner) cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH); OK(runner, cmark_node_append_child(doc, p), "append1"); - INT_EQ(runner, cmark_node_check(doc), 0, "append1 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append1 consistent"); + OK(runner, cmark_node_parent(p) == doc, "node_parent"); cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH); OK(runner, cmark_node_prepend_child(p, emph), "prepend1"); - INT_EQ(runner, cmark_node_check(doc), 0, "prepend1 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend1 consistent"); cmark_node *str1 = cmark_node_new(CMARK_NODE_STRING); cmark_node_set_string_content(str1, "Hello, "); OK(runner, cmark_node_prepend_child(p, str1), "prepend2"); - INT_EQ(runner, cmark_node_check(doc), 0, "prepend2 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend2 consistent"); cmark_node *str3 = cmark_node_new(CMARK_NODE_STRING); cmark_node_set_string_content(str3, "!"); OK(runner, cmark_node_append_child(p, str3), "append2"); - INT_EQ(runner, cmark_node_check(doc), 0, "append2 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append2 consistent"); cmark_node *str2 = cmark_node_new(CMARK_NODE_STRING); cmark_node_set_string_content(str2, "world"); OK(runner, cmark_node_append_child(emph, str2), "append3"); - INT_EQ(runner, cmark_node_check(doc), 0, "append3 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append3 consistent"); - html = (char *)cmark_render_html(doc); + html = cmark_render_html(doc); STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n", "render_html"); free(html); OK(runner, cmark_node_insert_before(str1, str3), "ins before1"); - INT_EQ(runner, cmark_node_check(doc), 0, "ins before1 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, + "ins before1 consistent"); // 31e OK(runner, cmark_node_first_child(p) == str3, "ins before1 works"); OK(runner, cmark_node_insert_before(str1, emph), "ins before2"); - INT_EQ(runner, cmark_node_check(doc), 0, "ins before2 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, + "ins before2 consistent"); // 3e1 OK(runner, cmark_node_last_child(p) == str1, "ins before2 works"); OK(runner, cmark_node_insert_after(str1, str3), "ins after1"); - INT_EQ(runner, cmark_node_check(doc), 0, "ins after1 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, + "ins after1 consistent"); // e13 - OK(runner, cmark_node_last_child(p) == str3, "ins after1 works"); + OK(runner, cmark_node_next(str1) == str3, "ins after1 works"); OK(runner, cmark_node_insert_after(str1, emph), "ins after2"); - INT_EQ(runner, cmark_node_check(doc), 0, "ins after2 consistent"); + INT_EQ(runner, cmark_node_check(doc, NULL), 0, + "ins after2 consistent"); // 1e3 - OK(runner, cmark_node_first_child(p) == str1, "ins after2 works"); + OK(runner, cmark_node_previous(emph) == str1, "ins after2 works"); - html = (char *)cmark_render_html(doc); - STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n", + cmark_node_unlink(emph); + + html = cmark_render_html(doc); + STR_EQ(runner, html, "<p>Hello, !</p>\n", "render_html after shuffling"); free(html); cmark_node_destroy(doc); + + // TODO: Test that the contents of an unlinked inline are valid + // after the parent block was destroyed. This doesn't work so far. + cmark_node_destroy(emph); +} + +void +hierarchy(test_batch_runner *runner) +{ + cmark_node *bquote1 = cmark_node_new(CMARK_NODE_BQUOTE); + cmark_node *bquote2 = cmark_node_new(CMARK_NODE_BQUOTE); + cmark_node *bquote3 = cmark_node_new(CMARK_NODE_BQUOTE); + + OK(runner, cmark_node_append_child(bquote1, bquote2), + "append bquote2"); + OK(runner, cmark_node_append_child(bquote2, bquote3), + "append bquote3"); + OK(runner, !cmark_node_append_child(bquote3, bquote3), + "adding a node as child of itself fails"); + OK(runner, !cmark_node_append_child(bquote3, bquote1), + "adding a parent as child fails"); + + cmark_node_destroy(bquote1); + + int max_node_type = CMARK_NODE_LAST_BLOCK > CMARK_NODE_LAST_INLINE + ? CMARK_NODE_LAST_BLOCK : CMARK_NODE_LAST_INLINE; + OK(runner, max_node_type < 32, "all node types < 32"); + + int list_item_flag = 1 << CMARK_NODE_LIST_ITEM; + int top_level_blocks = + (1 << CMARK_NODE_BQUOTE) | + (1 << CMARK_NODE_LIST) | + (1 << CMARK_NODE_FENCED_CODE) | + (1 << CMARK_NODE_INDENTED_CODE) | + (1 << CMARK_NODE_HTML) | + (1 << CMARK_NODE_PARAGRAPH) | + (1 << CMARK_NODE_ATX_HEADER) | + (1 << CMARK_NODE_SETEXT_HEADER) | + (1 << CMARK_NODE_HRULE) | + (1 << CMARK_NODE_REFERENCE_DEF); + int all_inlines = + (1 << CMARK_NODE_STRING) | + (1 << CMARK_NODE_SOFTBREAK) | + (1 << CMARK_NODE_LINEBREAK) | + (1 << CMARK_NODE_INLINE_CODE) | + (1 << CMARK_NODE_INLINE_HTML) | + (1 << CMARK_NODE_EMPH) | + (1 << CMARK_NODE_STRONG) | + (1 << CMARK_NODE_LINK) | + (1 << CMARK_NODE_IMAGE); + + test_content(runner, CMARK_NODE_DOCUMENT, top_level_blocks); + test_content(runner, CMARK_NODE_BQUOTE, top_level_blocks); + test_content(runner, CMARK_NODE_LIST, list_item_flag); + test_content(runner, CMARK_NODE_LIST_ITEM, top_level_blocks); + test_content(runner, CMARK_NODE_FENCED_CODE, 0); + test_content(runner, CMARK_NODE_INDENTED_CODE, 0); + test_content(runner, CMARK_NODE_HTML, 0); + test_content(runner, CMARK_NODE_PARAGRAPH, all_inlines); + test_content(runner, CMARK_NODE_ATX_HEADER, all_inlines); + test_content(runner, CMARK_NODE_SETEXT_HEADER, all_inlines); + test_content(runner, CMARK_NODE_HRULE, 0); + test_content(runner, CMARK_NODE_REFERENCE_DEF, 0); + test_content(runner, CMARK_NODE_STRING, 0); + test_content(runner, CMARK_NODE_SOFTBREAK, 0); + test_content(runner, CMARK_NODE_LINEBREAK, 0); + test_content(runner, CMARK_NODE_INLINE_CODE, 0); + test_content(runner, CMARK_NODE_INLINE_HTML, 0); + test_content(runner, CMARK_NODE_EMPH, all_inlines); + test_content(runner, CMARK_NODE_STRONG, all_inlines); + test_content(runner, CMARK_NODE_LINK, all_inlines); + test_content(runner, CMARK_NODE_IMAGE, all_inlines); +} + +static void +test_content(test_batch_runner *runner, cmark_node_type type, + int allowed_content) +{ + cmark_node *node = cmark_node_new(type); + + for (int i = 0; i < num_node_types; ++i) { + cmark_node_type child_type = node_types[i]; + cmark_node *child = cmark_node_new(child_type); + + int got = cmark_node_append_child(node, child); + int expected = (allowed_content >> child_type) & 1; + + INT_EQ(runner, got, expected, + "add %d as child of %d", child_type, type); + + cmark_node_destroy(child); + } + + cmark_node_destroy(node); +} + +static void +render_html(test_batch_runner *runner) +{ + char *html; + + static const char markdown[] = + "foo *bar*\n" + "\n" + "paragraph 2\n"; + cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1); + + cmark_node *paragraph = cmark_node_first_child(doc); + html = cmark_render_html(paragraph); + STR_EQ(runner, html, "<p>foo <em>bar</em></p>\n", + "render single paragraph"); + free(html); + + cmark_node *string = cmark_node_first_child(paragraph); + html = cmark_render_html(string); + STR_EQ(runner, html, "foo ", "render single inline"); + free(html); + + cmark_node *emph = cmark_node_next(string); + html = cmark_render_html(emph); + STR_EQ(runner, html, "<em>bar</em>", "render inline with children"); + free(html); + + cmark_node_destroy(doc); } int main() { int retval; test_batch_runner *runner = test_batch_runner_new(); + constructor(runner); accessors(runner); + node_check(runner); create_tree(runner); + hierarchy(runner); + render_html(runner); test_print_summary(runner); retval = test_ok(runner) ? 0 : 1; |