Files
gotea/third_party/bb/tests/md_test.c
T
lotherk 3eae0cc954 build: vendor bb static site generator
Vendor the C99 bb sources, share directory, tests and man page under third_party/bb (MIT, upstream f5373fb) and add a make bb target. A checkout can now build project sites without an installed bb.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash
2026-11-22 12:27:43 +00:00

104 lines
3.5 KiB
C

/* md_test.c — unit tests for the Markdown parser (markdown_to_html).
* Compile: cc -std=c99 -Wall -Wextra -pedantic -I../src md_test.c ../src/markdown.c ../src/strbuf.c
* Run: ./md_test (exit 0 = all pass, 1 = failures)
*/
#include "bb.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int failures = 0;
static int total = 0;
/* Render markdown string to HTML string (malloc'd). */
static char *render(const char *md) {
FILE *in = tmpfile();
FILE *out = tmpfile();
if (!in || !out) { fprintf(stderr, "tmpfile failed\n"); exit(2); }
fputs(md, in);
rewind(in);
markdown_to_html(out, in, "");
fflush(out);
rewind(out);
/* read all of out */
size_t cap = 256, len = 0;
char *buf = malloc(cap);
if (!buf) exit(2);
int c;
while ((c = fgetc(out)) != EOF) {
if (len + 1 >= cap) { cap *= 2; buf = realloc(buf, cap); }
buf[len++] = (char)c;
}
buf[len] = '\0';
fclose(in);
fclose(out);
return buf;
}
static void check(const char *name, const char *md, const char *want) {
total++;
char *got = render(md);
if (strcmp(got, want) != 0) {
failures++;
printf("FAIL %s\n md: %s\n want: %s\n got: %s\n", name, md, want, got);
} else {
printf("ok %s\n", name);
}
free(got);
}
int main(void) {
/* headings */
check("h1", "# Hello\n", "<h1 id=\"hello\">Hello</h1>\n");
check("h3", "### Sub\n", "<h3 id=\"sub\">Sub</h3>\n");
check("h6", "###### deep\n", "<h6 id=\"deep\">deep</h6>\n");
check("h7-not", "####### not heading\n", "<p>####### not heading</p>\n");
/* paragraphs */
check("para", "hello world\n", "<p>hello world</p>\n");
check("para-multi", "line one\nline two\n", "<p>line one\nline two</p>\n");
check("para-blank", "one\n\ntwo\n", "<p>one</p>\n<p>two</p>\n");
/* emphasis / strong */
check("em", "*hi*\n", "<p><em>hi</em></p>\n");
check("strong", "**hi**\n", "<p><strong>hi</strong></p>\n");
check("em-strong", "**bold** and *it*\n", "<p><strong>bold</strong> and <em>it</em></p>\n");
/* inline code */
check("code", "`x`\n", "<p><code>x</code></p>\n");
check("code-esc", "`a<b&c`\n", "<p><code>a&lt;b&amp;c</code></p>\n");
/* links */
check("link", "[x](https://e.com)\n", "<p><a href=\"https://e.com\">x</a></p>\n");
check("link-esc", "[x](https://e.com?a=1&b=2)\n", "<p><a href=\"https://e.com?a=1&amp;b=2\">x</a></p>\n");
/* escaping */
check("escape-amp", "a & b\n", "<p>a &amp; b</p>\n");
check("escape-lt", "a < b\n", "<p>a &lt; b</p>\n");
check("escape-quote", "say \"hi\"\n", "<p>say &quot;hi&quot;</p>\n");
/* lists */
check("ul", "- one\n- two\n", "<ul>\n<li>one</li>\n<li>two</li>\n</ul>\n");
check("ol", "1. one\n2. two\n", "<ol>\n<li>one</li>\n<li>two</li>\n</ol>\n");
/* fenced code */
check("fence", "```\ncode\n```\n", "<pre><code>code\n</code></pre>\n");
check("fence-esc", "```\n<a>&\n```\n", "<pre><code>&lt;a&gt;&amp;\n</code></pre>\n");
/* blockquote */
check("blockquote", "> quote\n", "<blockquote>\n<p>quote</p>\n</blockquote>\n");
/* hr */
check("hr", "---\n", "<hr>\n");
/* table */
check("table", "| a | b |\n|---|---|\n| 1 | 2 |\n",
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>1</td>\n<td>2</td>\n</tr>\n</tbody>\n</table>\n");
/* frontmatter stripped */
check("frontmatter", "---\ntitle: X\n---\nbody\n", "<p>body</p>\n");
printf("\n%d/%d passed\n", total - failures, total);
return failures ? 1 : 0;
}