aboutsummaryrefslogtreecommitdiff
path: root/api_test/main.c
blob: 1f42807e062bed3e2cf9185f3fe5cc190ea180c2 (plain)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define CMARK_NO_SHORT_NAMES
  5. #include "cmark.h"
  6. #include "node.h"
  7. #include "harness.h"
  8. #define UTF8_REPL "\xEF\xBF\xBD"
  9. void
  10. test_cplusplus(test_batch_runner *runner);
  11. static const cmark_node_type node_types[] = {
  12. CMARK_NODE_DOCUMENT,
  13. CMARK_NODE_BLOCK_QUOTE,
  14. CMARK_NODE_LIST,
  15. CMARK_NODE_LIST_ITEM,
  16. CMARK_NODE_CODE_BLOCK,
  17. CMARK_NODE_HTML,
  18. CMARK_NODE_PARAGRAPH,
  19. CMARK_NODE_HEADER,
  20. CMARK_NODE_HRULE,
  21. CMARK_NODE_REFERENCE_DEF,
  22. CMARK_NODE_TEXT,
  23. CMARK_NODE_SOFTBREAK,
  24. CMARK_NODE_LINEBREAK,
  25. CMARK_NODE_INLINE_CODE,
  26. CMARK_NODE_INLINE_HTML,
  27. CMARK_NODE_EMPH,
  28. CMARK_NODE_STRONG,
  29. CMARK_NODE_LINK,
  30. CMARK_NODE_IMAGE
  31. };
  32. static const int num_node_types = sizeof(node_types) / sizeof(*node_types);
  33. static void
  34. test_md_to_html(test_batch_runner *runner, const char *markdown,
  35. const char *expected_html, const char *msg);
  36. static void
  37. test_content(test_batch_runner *runner, cmark_node_type type,
  38. int allowed_content);
  39. static void
  40. test_char(test_batch_runner *runner, int valid, const char *utf8,
  41. const char *msg);
  42. static void
  43. test_incomplete_char(test_batch_runner *runner, const char *utf8,
  44. const char *msg);
  45. static void
  46. test_continuation_byte(test_batch_runner *runner, const char *utf8);
  47. static void
  48. constructor(test_batch_runner *runner)
  49. {
  50. for (int i = 0; i < num_node_types; ++i) {
  51. cmark_node_type type = node_types[i];
  52. cmark_node *node = cmark_node_new(type);
  53. OK(runner, node != NULL, "new type %d", type);
  54. INT_EQ(runner, cmark_node_get_type(node), type,
  55. "get_type %d", type);
  56. switch (node->type) {
  57. case CMARK_NODE_HEADER:
  58. INT_EQ(runner, cmark_node_get_header_level(node), 1,
  59. "default header level is 1");
  60. node->as.header.level = 1;
  61. break;
  62. case CMARK_NODE_LIST:
  63. INT_EQ(runner, cmark_node_get_list_type(node),
  64. CMARK_BULLET_LIST,
  65. "default is list type is bullet");
  66. INT_EQ(runner, cmark_node_get_list_start(node), 1,
  67. "default is list start is 1");
  68. INT_EQ(runner, cmark_node_get_list_tight(node), 0,
  69. "default is list is loose");
  70. break;
  71. default:
  72. break;
  73. }
  74. cmark_node_free(node);
  75. }
  76. }
  77. static void
  78. accessors(test_batch_runner *runner)
  79. {
  80. static const char markdown[] =
  81. "## Header\n"
  82. "\n"
  83. "* Item 1\n"
  84. "* Item 2\n"
  85. "\n"
  86. "2. Item 1\n"
  87. "\n"
  88. "3. Item 2\n"
  89. "\n"
  90. "\n"
  91. " code\n"
  92. "\n"
  93. "``` lang\n"
  94. "fenced\n"
  95. "```\n"
  96. "\n"
  97. "<div>html</div>\n"
  98. "\n"
  99. "[link](url 'title')\n";
  100. cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1);
  101. // Getters
  102. cmark_node *header = cmark_node_first_child(doc);
  103. INT_EQ(runner, cmark_node_get_header_level(header), 2,
  104. "get_header_level");
  105. cmark_node *bullet_list = cmark_node_next(header);
  106. INT_EQ(runner, cmark_node_get_list_type(bullet_list),
  107. CMARK_BULLET_LIST, "get_list_type bullet");
  108. INT_EQ(runner, cmark_node_get_list_tight(bullet_list), 1,
  109. "get_list_tight tight");
  110. cmark_node *ordered_list = cmark_node_next(bullet_list);
  111. INT_EQ(runner, cmark_node_get_list_type(ordered_list),
  112. CMARK_ORDERED_LIST, "get_list_type ordered");
  113. INT_EQ(runner, cmark_node_get_list_start(ordered_list), 2,
  114. "get_list_start");
  115. INT_EQ(runner, cmark_node_get_list_tight(ordered_list), 0,
  116. "get_list_tight loose");
  117. cmark_node *code = cmark_node_next(ordered_list);
  118. STR_EQ(runner, cmark_node_get_string_content(code), "code\n",
  119. "get_string_content indented code");
  120. cmark_node *fenced = cmark_node_next(code);
  121. STR_EQ(runner, cmark_node_get_string_content(fenced), "fenced\n",
  122. "get_string_content fenced code");
  123. STR_EQ(runner, cmark_node_get_fence_info(fenced), "lang",
  124. "get_fence_info");
  125. cmark_node *html = cmark_node_next(fenced);
  126. STR_EQ(runner, cmark_node_get_string_content(html),
  127. "<div>html</div>\n", "get_string_content html");
  128. cmark_node *paragraph = cmark_node_next(html);
  129. INT_EQ(runner, cmark_node_get_start_line(paragraph), 19,
  130. "get_start_line");
  131. INT_EQ(runner, cmark_node_get_start_column(paragraph), 1,
  132. "get_start_column");
  133. INT_EQ(runner, cmark_node_get_end_line(paragraph), 19,
  134. "get_end_line");
  135. cmark_node *link = cmark_node_first_child(paragraph);
  136. STR_EQ(runner, cmark_node_get_url(link), "url",
  137. "get_url");
  138. STR_EQ(runner, cmark_node_get_title(link), "title",
  139. "get_title");
  140. cmark_node *string = cmark_node_first_child(link);
  141. STR_EQ(runner, cmark_node_get_string_content(string), "link",
  142. "get_string_content string");
  143. // Setters
  144. OK(runner, cmark_node_set_header_level(header, 3),
  145. "set_header_level");
  146. OK(runner, cmark_node_set_list_type(bullet_list, CMARK_ORDERED_LIST),
  147. "set_list_type ordered");
  148. OK(runner, cmark_node_set_list_start(bullet_list, 3),
  149. "set_list_start");
  150. OK(runner, cmark_node_set_list_tight(bullet_list, 0),
  151. "set_list_tight loose");
  152. OK(runner, cmark_node_set_list_type(ordered_list, CMARK_BULLET_LIST),
  153. "set_list_type bullet");
  154. OK(runner, cmark_node_set_list_tight(ordered_list, 1),
  155. "set_list_tight tight");
  156. OK(runner, cmark_node_set_string_content(code, "CODE\n"),
  157. "set_string_content indented code");
  158. OK(runner, cmark_node_set_string_content(fenced, "FENCED\n"),
  159. "set_string_content fenced code");
  160. OK(runner, cmark_node_set_fence_info(fenced, "LANG"),
  161. "set_fence_info");
  162. OK(runner, cmark_node_set_string_content(html, "<div>HTML</div>\n"),
  163. "set_string_content html");
  164. OK(runner, cmark_node_set_url(link, "URL"),
  165. "set_url");
  166. OK(runner, cmark_node_set_title(link, "TITLE"),
  167. "set_title");
  168. OK(runner, cmark_node_set_string_content(string, "LINK"),
  169. "set_string_content string");
  170. char *rendered_html = cmark_render_html(doc);
  171. static const char expected_html[] =
  172. "<h3>Header</h3>\n"
  173. "<ol start=\"3\">\n"
  174. "<li>\n"
  175. "<p>Item 1</p>\n"
  176. "</li>\n"
  177. "<li>\n"
  178. "<p>Item 2</p>\n"
  179. "</li>\n"
  180. "</ol>\n"
  181. "<ul>\n"
  182. "<li>Item 1</li>\n"
  183. "<li>Item 2</li>\n"
  184. "</ul>\n"
  185. "<pre><code>CODE\n"
  186. "</code></pre>\n"
  187. "<pre><code class=\"language-LANG\">FENCED\n"
  188. "</code></pre>\n"
  189. "<div>HTML</div>\n"
  190. "<p><a href=\"URL\" title=\"TITLE\">LINK</a></p>\n";
  191. STR_EQ(runner, rendered_html, expected_html, "setters work");
  192. free(rendered_html);
  193. // Getter errors
  194. INT_EQ(runner, cmark_node_get_header_level(bullet_list), 0,
  195. "get_header_level error");
  196. INT_EQ(runner, cmark_node_get_list_type(header), CMARK_NO_LIST,
  197. "get_list_type error");
  198. INT_EQ(runner, cmark_node_get_list_start(code), 0,
  199. "get_list_start error");
  200. INT_EQ(runner, cmark_node_get_list_tight(fenced), 0,
  201. "get_list_tight error");
  202. OK(runner, cmark_node_get_string_content(ordered_list) == NULL,
  203. "get_string_content error");
  204. OK(runner, cmark_node_get_fence_info(paragraph) == NULL,
  205. "get_fence_info error");
  206. OK(runner, cmark_node_get_url(html) == NULL,
  207. "get_url error");
  208. OK(runner, cmark_node_get_title(header) == NULL,
  209. "get_title error");
  210. // Setter errors
  211. OK(runner, !cmark_node_set_header_level(bullet_list, 3),
  212. "set_header_level error");
  213. OK(runner, !cmark_node_set_list_type(header, CMARK_ORDERED_LIST),
  214. "set_list_type error");
  215. OK(runner, !cmark_node_set_list_start(code, 3),
  216. "set_list_start error");
  217. OK(runner, !cmark_node_set_list_tight(fenced, 0),
  218. "set_list_tight error");
  219. OK(runner, !cmark_node_set_string_content(ordered_list, "content\n"),
  220. "set_string_content error");
  221. OK(runner, !cmark_node_set_fence_info(paragraph, "lang"),
  222. "set_fence_info error");
  223. OK(runner, !cmark_node_set_url(html, "url"),
  224. "set_url error");
  225. OK(runner, !cmark_node_set_title(header, "title"),
  226. "set_title error");
  227. OK(runner, !cmark_node_set_header_level(header, 0),
  228. "set_header_level too small");
  229. OK(runner, !cmark_node_set_header_level(header, 7),
  230. "set_header_level too large");
  231. OK(runner, !cmark_node_set_list_type(bullet_list, CMARK_NO_LIST),
  232. "set_list_type invalid");
  233. OK(runner, !cmark_node_set_list_start(bullet_list, -1),
  234. "set_list_start negative");
  235. cmark_node_free(doc);
  236. }
  237. static void
  238. node_check(test_batch_runner *runner) {
  239. // Construct an incomplete tree.
  240. cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT);
  241. cmark_node *p1 = cmark_node_new(CMARK_NODE_PARAGRAPH);
  242. cmark_node *p2 = cmark_node_new(CMARK_NODE_PARAGRAPH);
  243. doc->first_child = p1;
  244. p1->next = p2;
  245. INT_EQ(runner, cmark_node_check(doc, NULL), 4, "node_check works");
  246. INT_EQ(runner, cmark_node_check(doc, NULL), 0,
  247. "node_check fixes tree");
  248. cmark_node_free(doc);
  249. }
  250. static void
  251. create_tree(test_batch_runner *runner)
  252. {
  253. char *html;
  254. cmark_node *doc = cmark_node_new(CMARK_NODE_DOCUMENT);
  255. cmark_node *p = cmark_node_new(CMARK_NODE_PARAGRAPH);
  256. OK(runner, !cmark_node_insert_before(doc, p),
  257. "insert before root fails");
  258. OK(runner, !cmark_node_insert_after(doc, p),
  259. "insert after root fails");
  260. OK(runner, cmark_node_append_child(doc, p), "append1");
  261. INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append1 consistent");
  262. OK(runner, cmark_node_parent(p) == doc, "node_parent");
  263. cmark_node *emph = cmark_node_new(CMARK_NODE_EMPH);
  264. OK(runner, cmark_node_prepend_child(p, emph), "prepend1");
  265. INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend1 consistent");
  266. cmark_node *str1 = cmark_node_new(CMARK_NODE_TEXT);
  267. cmark_node_set_string_content(str1, "Hello, ");
  268. OK(runner, cmark_node_prepend_child(p, str1), "prepend2");
  269. INT_EQ(runner, cmark_node_check(doc, NULL), 0, "prepend2 consistent");
  270. cmark_node *str3 = cmark_node_new(CMARK_NODE_TEXT);
  271. cmark_node_set_string_content(str3, "!");
  272. OK(runner, cmark_node_append_child(p, str3), "append2");
  273. INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append2 consistent");
  274. cmark_node *str2 = cmark_node_new(CMARK_NODE_TEXT);
  275. cmark_node_set_string_content(str2, "world");
  276. OK(runner, cmark_node_append_child(emph, str2), "append3");
  277. INT_EQ(runner, cmark_node_check(doc, NULL), 0, "append3 consistent");
  278. html = cmark_render_html(doc);
  279. STR_EQ(runner, html, "<p>Hello, <em>world</em>!</p>\n",
  280. "render_html");
  281. free(html);
  282. OK(runner, cmark_node_insert_before(str1, str3), "ins before1");
  283. INT_EQ(runner, cmark_node_check(doc, NULL), 0,
  284. "ins before1 consistent");
  285. // 31e
  286. OK(runner, cmark_node_first_child(p) == str3, "ins before1 works");
  287. OK(runner, cmark_node_insert_before(str1, emph), "ins before2");
  288. INT_EQ(runner, cmark_node_check(doc, NULL), 0,
  289. "ins before2 consistent");
  290. // 3e1
  291. OK(runner, cmark_node_last_child(p) == str1, "ins before2 works");
  292. OK(runner, cmark_node_insert_after(str1, str3), "ins after1");
  293. INT_EQ(runner, cmark_node_check(doc, NULL), 0,
  294. "ins after1 consistent");
  295. // e13
  296. OK(runner, cmark_node_next(str1) == str3, "ins after1 works");
  297. OK(runner, cmark_node_insert_after(str1, emph), "ins after2");
  298. INT_EQ(runner, cmark_node_check(doc, NULL), 0,
  299. "ins after2 consistent");
  300. // 1e3
  301. OK(runner, cmark_node_previous(emph) == str1, "ins after2 works");
  302. cmark_node_unlink(emph);
  303. html = cmark_render_html(doc);
  304. STR_EQ(runner, html, "<p>Hello, !</p>\n",
  305. "render_html after shuffling");
  306. free(html);
  307. cmark_node_free(doc);
  308. // TODO: Test that the contents of an unlinked inline are valid
  309. // after the parent block was destroyed. This doesn't work so far.
  310. cmark_node_free(emph);
  311. }
  312. void
  313. hierarchy(test_batch_runner *runner)
  314. {
  315. cmark_node *bquote1 = cmark_node_new(CMARK_NODE_BLOCK_QUOTE);
  316. cmark_node *bquote2 = cmark_node_new(CMARK_NODE_BLOCK_QUOTE);
  317. cmark_node *bquote3 = cmark_node_new(CMARK_NODE_BLOCK_QUOTE);
  318. OK(runner, cmark_node_append_child(bquote1, bquote2),
  319. "append bquote2");
  320. OK(runner, cmark_node_append_child(bquote2, bquote3),
  321. "append bquote3");
  322. OK(runner, !cmark_node_append_child(bquote3, bquote3),
  323. "adding a node as child of itself fails");
  324. OK(runner, !cmark_node_append_child(bquote3, bquote1),
  325. "adding a parent as child fails");
  326. cmark_node_free(bquote1);
  327. int max_node_type = CMARK_NODE_LAST_BLOCK > CMARK_NODE_LAST_INLINE
  328. ? CMARK_NODE_LAST_BLOCK : CMARK_NODE_LAST_INLINE;
  329. OK(runner, max_node_type < 32, "all node types < 32");
  330. int list_item_flag = 1 << CMARK_NODE_LIST_ITEM;
  331. int top_level_blocks =
  332. (1 << CMARK_NODE_BLOCK_QUOTE) |
  333. (1 << CMARK_NODE_LIST) |
  334. (1 << CMARK_NODE_CODE_BLOCK) |
  335. (1 << CMARK_NODE_HTML) |
  336. (1 << CMARK_NODE_PARAGRAPH) |
  337. (1 << CMARK_NODE_HEADER) |
  338. (1 << CMARK_NODE_HRULE) |
  339. (1 << CMARK_NODE_REFERENCE_DEF);
  340. int all_inlines =
  341. (1 << CMARK_NODE_TEXT) |
  342. (1 << CMARK_NODE_SOFTBREAK) |
  343. (1 << CMARK_NODE_LINEBREAK) |
  344. (1 << CMARK_NODE_INLINE_CODE) |
  345. (1 << CMARK_NODE_INLINE_HTML) |
  346. (1 << CMARK_NODE_EMPH) |
  347. (1 << CMARK_NODE_STRONG) |
  348. (1 << CMARK_NODE_LINK) |
  349. (1 << CMARK_NODE_IMAGE);
  350. test_content(runner, CMARK_NODE_DOCUMENT, top_level_blocks);
  351. test_content(runner, CMARK_NODE_BLOCK_QUOTE, top_level_blocks);
  352. test_content(runner, CMARK_NODE_LIST, list_item_flag);
  353. test_content(runner, CMARK_NODE_LIST_ITEM, top_level_blocks);
  354. test_content(runner, CMARK_NODE_CODE_BLOCK , 0);
  355. test_content(runner, CMARK_NODE_HTML, 0);
  356. test_content(runner, CMARK_NODE_PARAGRAPH, all_inlines);
  357. test_content(runner, CMARK_NODE_HEADER, all_inlines);
  358. test_content(runner, CMARK_NODE_HRULE, 0);
  359. test_content(runner, CMARK_NODE_REFERENCE_DEF, 0);
  360. test_content(runner, CMARK_NODE_TEXT, 0);
  361. test_content(runner, CMARK_NODE_SOFTBREAK, 0);
  362. test_content(runner, CMARK_NODE_LINEBREAK, 0);
  363. test_content(runner, CMARK_NODE_INLINE_CODE, 0);
  364. test_content(runner, CMARK_NODE_INLINE_HTML, 0);
  365. test_content(runner, CMARK_NODE_EMPH, all_inlines);
  366. test_content(runner, CMARK_NODE_STRONG, all_inlines);
  367. test_content(runner, CMARK_NODE_LINK, all_inlines);
  368. test_content(runner, CMARK_NODE_IMAGE, all_inlines);
  369. }
  370. static void
  371. test_content(test_batch_runner *runner, cmark_node_type type,
  372. int allowed_content)
  373. {
  374. cmark_node *node = cmark_node_new(type);
  375. for (int i = 0; i < num_node_types; ++i) {
  376. cmark_node_type child_type = node_types[i];
  377. cmark_node *child = cmark_node_new(child_type);
  378. int got = cmark_node_append_child(node, child);
  379. int expected = (allowed_content >> child_type) & 1;
  380. INT_EQ(runner, got, expected,
  381. "add %d as child of %d", child_type, type);
  382. cmark_node_free(child);
  383. }
  384. cmark_node_free(node);
  385. }
  386. static void
  387. parser(test_batch_runner *runner)
  388. {
  389. test_md_to_html(runner, "No newline", "<p>No newline</p>\n",
  390. "document without trailing newline");
  391. }
  392. static void
  393. render_html(test_batch_runner *runner)
  394. {
  395. char *html;
  396. static const char markdown[] =
  397. "foo *bar*\n"
  398. "\n"
  399. "paragraph 2\n";
  400. cmark_node *doc = cmark_parse_document(markdown, sizeof(markdown) - 1);
  401. cmark_node *paragraph = cmark_node_first_child(doc);
  402. html = cmark_render_html(paragraph);
  403. STR_EQ(runner, html, "<p>foo <em>bar</em></p>\n",
  404. "render single paragraph");
  405. free(html);
  406. cmark_node *string = cmark_node_first_child(paragraph);
  407. html = cmark_render_html(string);
  408. STR_EQ(runner, html, "foo ", "render single inline");
  409. free(html);
  410. cmark_node *emph = cmark_node_next(string);
  411. html = cmark_render_html(emph);
  412. STR_EQ(runner, html, "<em>bar</em>", "render inline with children");
  413. free(html);
  414. cmark_node_free(doc);
  415. }
  416. static void
  417. utf8(test_batch_runner *runner)
  418. {
  419. // Ranges
  420. test_char(runner, 1, "\x01", "valid utf8 01");
  421. test_char(runner, 1, "\x7F", "valid utf8 7F");
  422. test_char(runner, 0, "\x80", "invalid utf8 80");
  423. test_char(runner, 0, "\xBF", "invalid utf8 BF");
  424. test_char(runner, 0, "\xC0\x80", "invalid utf8 C080");
  425. test_char(runner, 0, "\xC1\xBF", "invalid utf8 C1BF");
  426. test_char(runner, 1, "\xC2\x80", "valid utf8 C280");
  427. test_char(runner, 1, "\xDF\xBF", "valid utf8 DFBF");
  428. test_char(runner, 0, "\xE0\x80\x80", "invalid utf8 E08080");
  429. test_char(runner, 0, "\xE0\x9F\xBF", "invalid utf8 E09FBF");
  430. test_char(runner, 1, "\xE0\xA0\x80", "valid utf8 E0A080");
  431. test_char(runner, 1, "\xED\x9F\xBF", "valid utf8 ED9FBF");
  432. test_char(runner, 0, "\xED\xA0\x80", "invalid utf8 EDA080");
  433. test_char(runner, 0, "\xED\xBF\xBF", "invalid utf8 EDBFBF");
  434. test_char(runner, 0, "\xF0\x80\x80\x80", "invalid utf8 F0808080");
  435. test_char(runner, 0, "\xF0\x8F\xBF\xBF", "invalid utf8 F08FBFBF");
  436. test_char(runner, 1, "\xF0\x90\x80\x80", "valid utf8 F0908080");
  437. test_char(runner, 1, "\xF4\x8F\xBF\xBF", "valid utf8 F48FBFBF");
  438. test_char(runner, 0, "\xF4\x90\x80\x80", "invalid utf8 F4908080");
  439. test_char(runner, 0, "\xF7\xBF\xBF\xBF", "invalid utf8 F7BFBFBF");
  440. test_char(runner, 0, "\xF8", "invalid utf8 F8");
  441. test_char(runner, 0, "\xFF", "invalid utf8 FF");
  442. // Incomplete byte sequences at end of input
  443. test_incomplete_char(runner, "\xE0\xA0", "invalid utf8 E0A0");
  444. test_incomplete_char(runner, "\xF0\x90\x80", "invalid utf8 F09080");
  445. // Invalid continuation bytes
  446. test_continuation_byte(runner, "\xC2\x80");
  447. test_continuation_byte(runner, "\xE0\xA0\x80");
  448. test_continuation_byte(runner, "\xF0\x90\x80\x80");
  449. // Test string containing null character
  450. static const char string_with_null[] = "((((\0))))";
  451. char *html = cmark_markdown_to_html(string_with_null,
  452. sizeof(string_with_null) - 1);
  453. STR_EQ(runner, html, "<p>((((" UTF8_REPL "))))</p>\n",
  454. "utf8 with U+0000");
  455. free(html);
  456. }
  457. static void
  458. test_char(test_batch_runner *runner, int valid, const char *utf8,
  459. const char *msg)
  460. {
  461. char buf[20];
  462. sprintf(buf, "((((%s))))", utf8);
  463. if (valid) {
  464. char expected[30];
  465. sprintf(expected, "<p>((((%s))))</p>\n", utf8);
  466. test_md_to_html(runner, buf, expected, msg);
  467. }
  468. else {
  469. test_md_to_html(runner, buf, "<p>((((" UTF8_REPL "))))</p>\n",
  470. msg);
  471. }
  472. }
  473. static void
  474. test_incomplete_char(test_batch_runner *runner, const char *utf8,
  475. const char *msg)
  476. {
  477. char buf[20];
  478. sprintf(buf, "----%s", utf8);
  479. test_md_to_html(runner, buf, "<p>----" UTF8_REPL "</p>\n", msg);
  480. }
  481. static void
  482. test_continuation_byte(test_batch_runner *runner, const char *utf8)
  483. {
  484. int len = strlen(utf8);
  485. for (int pos = 1; pos < len; ++pos) {
  486. char buf[20];
  487. sprintf(buf, "((((%s))))", utf8);
  488. buf[4+pos] = '\x20';
  489. char expected[50];
  490. strcpy(expected, "<p>((((" UTF8_REPL "\x20");
  491. for (int i = pos + 1; i < len; ++i) {
  492. strcat(expected, UTF8_REPL);
  493. }
  494. strcat(expected, "))))</p>\n");
  495. char *html = cmark_markdown_to_html(buf, strlen(buf));
  496. STR_EQ(runner, html, expected,
  497. "invalid utf8 continuation byte %d/%d", pos, len);
  498. free(html);
  499. }
  500. }
  501. static void
  502. test_md_to_html(test_batch_runner *runner, const char *markdown,
  503. const char *expected_html, const char *msg)
  504. {
  505. char *html = cmark_markdown_to_html(markdown, strlen(markdown));
  506. STR_EQ(runner, html, expected_html, msg);
  507. free(html);
  508. }
  509. int main() {
  510. int retval;
  511. test_batch_runner *runner = test_batch_runner_new();
  512. constructor(runner);
  513. accessors(runner);
  514. node_check(runner);
  515. create_tree(runner);
  516. hierarchy(runner);
  517. parser(runner);
  518. render_html(runner);
  519. utf8(runner);
  520. test_cplusplus(runner);
  521. test_print_summary(runner);
  522. retval = test_ok(runner) ? 0 : 1;
  523. free(runner);
  524. return retval;
  525. }