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