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