aboutsummaryrefslogtreecommitdiff
path: root/api_test/main.c
blob: f74dee2e7a2c40e23be30a4e4c0a658870b42368 (plain)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "cmark.h"
  5. #include "node.h"
  6. #include "harness.h"
  7. static void
  8. accessors(test_batch_runner *runner)
  9. {
  10. static const unsigned char markdown[] =
  11. "## Header\n"
  12. "\n"
  13. "* Item 1\n"
  14. "* Item 2\n"
  15. "\n"
  16. "2. Item 1\n"
  17. "\n"
  18. "3. Item 2\n"
  19. "\n"
  20. "\n"
  21. " code\n"
  22. "\n"
  23. "``` lang\n"
  24. "fenced\n"
  25. "```\n"
  26. "\n"
  27. "<div>html</div>\n"
  28. "\n"
  29. "[link](url 'title')\n";
  30. cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1);
  31. // Getters
  32. cmark_node *header = cmark_node_first_child(doc);
  33. INT_EQ(runner, cmark_node_get_type(header), CMARK_NODE_ATX_HEADER,
  34. "get_type header");
  35. INT_EQ(runner, cmark_node_get_header_level(header), 2,
  36. "get_header_level");
  37. cmark_node *bullet_list = cmark_node_next(header);
  38. INT_EQ(runner, cmark_node_get_type(bullet_list), CMARK_NODE_LIST,
  39. "get_type bullet list");
  40. INT_EQ(runner, cmark_node_get_list_type(bullet_list),
  41. CMARK_BULLET_LIST, "get_list_type bullet");
  42. INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1,
  43. "get_list_tight tight");
  44. cmark_node *ordered_list = cmark_node_next(bullet_list);
  45. INT_EQ(runner, cmark_node_get_type(ordered_list), CMARK_NODE_LIST,
  46. "get_type ordered list");
  47. INT_EQ(runner, cmark_node_get_list_type(ordered_list),
  48. CMARK_ORDERED_LIST, "get_list_type ordered");
  49. INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2,
  50. "get_list_start");
  51. INT_EQ(runner, cmark_node_get_list_tight(ordered_list), 0,
  52. "get_list_tight loose");
  53. cmark_node *code = cmark_node_next(ordered_list);
  54. INT_EQ(runner, cmark_node_get_type(code), CMARK_NODE_INDENTED_CODE,
  55. "get_type indented code");
  56. STR_EQ(runner, cmark_node_get_string_content(code), "code\n",
  57. "get_string_content indented code");
  58. cmark_node *fenced = cmark_node_next(code);
  59. INT_EQ(runner, cmark_node_get_type(fenced), CMARK_NODE_FENCED_CODE,
  60. "get_type fenced code");
  61. STR_EQ(runner, cmark_node_get_string_content(fenced), "fenced\n",
  62. "get_string_content fenced code");
  63. STR_EQ(runner, cmark_node_get_fence_info(fenced), "lang",
  64. "get_fence_info");
  65. cmark_node *html = cmark_node_next(fenced);
  66. INT_EQ(runner, cmark_node_get_type(html), CMARK_NODE_HTML,
  67. "get_type html");
  68. STR_EQ(runner, cmark_node_get_string_content(html),
  69. "<div>html</div>\n", "get_string_content html");
  70. cmark_node *paragraph = cmark_node_next(html);
  71. INT_EQ(runner, cmark_node_get_type(paragraph), CMARK_NODE_PARAGRAPH,
  72. "get_type paragraph");
  73. INT_EQ(runner, cmark_node_get_start_line(paragraph), 19,
  74. "get_start_line");
  75. INT_EQ(runner, cmark_node_get_start_column(paragraph), 1,
  76. "get_start_column");
  77. INT_EQ(runner, cmark_node_get_end_line(paragraph), 19,
  78. "get_end_line");
  79. cmark_node *link = cmark_node_first_child(paragraph);
  80. INT_EQ(runner, cmark_node_get_type(link), CMARK_NODE_LINK,
  81. "get_type link");
  82. STR_EQ(runner, cmark_node_get_url(link), "url",
  83. "get_url");
  84. STR_EQ(runner, cmark_node_get_title(link), "title",
  85. "get_title");
  86. cmark_node *string = cmark_node_first_child(link);
  87. INT_EQ(runner, cmark_node_get_type(string), CMARK_NODE_STRING,
  88. "get_type string");
  89. STR_EQ(runner, cmark_node_get_string_content(string), "link",
  90. "get_string_content string");
  91. // Setters
  92. OK(runner, cmark_node_set_header_level(header, 3),
  93. "set_header_level");
  94. OK(runner, cmark_node_set_list_type(bullet_list, CMARK_ORDERED_LIST),
  95. "set_list_type ordered");
  96. OK(runner, cmark_node_set_list_start(bullet_list, 3),
  97. "set_list_start");
  98. OK(runner, cmark_node_set_list_tight(bullet_list, 0),
  99. "set_list_tight loose");
  100. OK(runner, cmark_node_set_list_type(ordered_list, CMARK_BULLET_LIST),
  101. "set_list_type bullet");
  102. OK(runner, cmark_node_set_list_tight(ordered_list, 1),
  103. "set_list_tight tight");
  104. OK(runner, cmark_node_set_string_content(code, "CODE\n"),
  105. "set_string_content indented code");
  106. OK(runner, cmark_node_set_string_content(fenced, "FENCED\n"),
  107. "set_string_content fenced code");
  108. OK(runner, cmark_node_set_fence_info(fenced, "LANG"),
  109. "set_fence_info");
  110. OK(runner, cmark_node_set_string_content(html, "<div>HTML</div>\n"),
  111. "set_string_content html");
  112. OK(runner, cmark_node_set_url(link, "URL"),
  113. "set_url");
  114. OK(runner, cmark_node_set_title(link, "TITLE"),
  115. "set_title");
  116. OK(runner, cmark_node_set_string_content(string, "LINK"),
  117. "set_string_content string");
  118. char *rendered_html = (char *)cmark_render_html(doc);
  119. static const char expected_html[] =
  120. "<h3>Header</h3>\n"
  121. "<ol start=\"3\">\n"
  122. "<li>\n"
  123. "<p>Item 1</p>\n"
  124. "</li>\n"
  125. "<li>\n"
  126. "<p>Item 2</p>\n"
  127. "</li>\n"
  128. "</ol>\n"
  129. "<ul start=\"2\">\n"
  130. "<li>Item 1</li>\n"
  131. "<li>Item 2</li>\n"
  132. "</ul>\n"
  133. "<pre><code>CODE\n"
  134. "</code></pre>\n"
  135. "<pre><code class=\"language-LANG\">FENCED\n"
  136. "</code></pre>\n"
  137. "<div>HTML</div>\n"
  138. "<p><a href=\"URL\" title=\"TITLE\">LINK</a></p>\n";
  139. STR_EQ(runner, rendered_html, expected_html, "setters work");
  140. free(rendered_html);
  141. // Getter errors
  142. INT_EQ(runner, cmark_node_get_header_level(bullet_list), 0,
  143. "get_header_level error");
  144. INT_EQ(runner, cmark_node_get_list_type(header), CMARK_NO_LIST,
  145. "get_list_type error");
  146. INT_EQ(runner, cmark_node_get_list_start(code), 0,
  147. "get_list_start error");
  148. INT_EQ(runner, cmark_node_get_list_tight(fenced), 0,
  149. "get_list_tight error");
  150. OK(runner, cmark_node_get_string_content(ordered_list) == NULL,
  151. "get_string_content error");
  152. OK(runner, cmark_node_get_fence_info(paragraph) == NULL,
  153. "get_fence_info error");
  154. OK(runner, cmark_node_get_url(html) == NULL,
  155. "get_url error");
  156. OK(runner, cmark_node_get_title(header) == NULL,
  157. "get_title error");
  158. // Setter errors
  159. OK(runner, !cmark_node_set_header_level(bullet_list, 3),
  160. "set_header_level error");
  161. OK(runner, !cmark_node_set_list_type(header, CMARK_ORDERED_LIST),
  162. "set_list_type error");
  163. OK(runner, !cmark_node_set_list_start(code, 3),
  164. "set_list_start error");
  165. OK(runner, !cmark_node_set_list_tight(fenced, 0),
  166. "set_list_tight error");
  167. OK(runner, !cmark_node_set_string_content(ordered_list, "content\n"),
  168. "set_string_content error");
  169. OK(runner, !cmark_node_set_fence_info(paragraph, "lang"),
  170. "set_fence_info error");
  171. OK(runner, !cmark_node_set_url(html, "url"),
  172. "set_url error");
  173. OK(runner, !cmark_node_set_title(header, "title"),
  174. "set_title error");
  175. OK(runner, !cmark_node_set_header_level(header, 0),
  176. "set_header_level too small");
  177. OK(runner, !cmark_node_set_header_level(header, 7),
  178. "set_header_level too large");
  179. OK(runner, !cmark_node_set_list_type(bullet_list, CMARK_NO_LIST),
  180. "set_list_type invalid");
  181. OK(runner, !cmark_node_set_list_start(bullet_list, -1),
  182. "set_list_start negative");
  183. cmark_node_destroy(doc);
  184. }
  185. static void
  186. create_tree(test_batch_runner *runner)
  187. {
  188. char *html;
  189. cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT);
  190. cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH);
  191. OK(runner, cmark_node_append_child(doc, p), "append1");
  192. INT_EQ(runner, cmark_node_check(doc), 0, "append1 consistent");
  193. cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH);
  194. OK(runner, cmark_node_prepend_child(p, emph), "prepend1");
  195. INT_EQ(runner, cmark_node_check(doc), 0, "prepend1 consistent");
  196. cmark_node *str1 = cmark_node_new(CMARK_NODE_STRING);
  197. cmark_node_set_string_content(str1, "Hello, ");
  198. OK(runner, cmark_node_prepend_child(p, str1), "prepend2");
  199. INT_EQ(runner, cmark_node_check(doc), 0, "prepend2 consistent");
  200. cmark_node *str3 = cmark_node_new(CMARK_NODE_STRING);
  201. cmark_node_set_string_content(str3, "!");
  202. OK(runner, cmark_node_append_child(p, str3), "append2");
  203. INT_EQ(runner, cmark_node_check(doc), 0, "append2 consistent");
  204. cmark_node *str2 = cmark_node_new(CMARK_NODE_STRING);
  205. cmark_node_set_string_content(str2, "world");
  206. OK(runner, cmark_node_append_child(emph, str2), "append3");
  207. INT_EQ(runner, cmark_node_check(doc), 0, "append3 consistent");
  208. html = (char *)cmark_render_html(doc);
  209. STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n",
  210. "render_html");
  211. free(html);
  212. OK(runner, cmark_node_insert_before(str1, str3), "ins before1");
  213. INT_EQ(runner, cmark_node_check(doc), 0, "ins before1 consistent");
  214. // 31e
  215. OK(runner, cmark_node_first_child(p) == str3, "ins before1 works");
  216. OK(runner, cmark_node_insert_before(str1, emph), "ins before2");
  217. INT_EQ(runner, cmark_node_check(doc), 0, "ins before2 consistent");
  218. // 3e1
  219. OK(runner, cmark_node_last_child(p) == str1, "ins before2 works");
  220. OK(runner, cmark_node_insert_after(str1, str3), "ins after1");
  221. INT_EQ(runner, cmark_node_check(doc), 0, "ins after1 consistent");
  222. // e13
  223. OK(runner, cmark_node_last_child(p) == str3, "ins after1 works");
  224. OK(runner, cmark_node_insert_after(str1, emph), "ins after2");
  225. INT_EQ(runner, cmark_node_check(doc), 0, "ins after2 consistent");
  226. // 1e3
  227. OK(runner, cmark_node_first_child(p) == str1, "ins after2 works");
  228. html = (char *)cmark_render_html(doc);
  229. STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n",
  230. "render_html after shuffling");
  231. free(html);
  232. cmark_node_destroy(doc);
  233. }
  234. int main() {
  235. int retval;
  236. test_batch_runner *runner = test_batch_runner_new();
  237. accessors(runner);
  238. create_tree(runner);
  239. test_print_summary(runner);
  240. retval = test_ok(runner) ? 0 : 1;
  241. free(runner);
  242. return retval;
  243. }