Files
tinyblocks/tests/markdown_test.c
T

88 lines
3.4 KiB
C

/*
* markdown_test.c - testa tinyblocks-markdown (-> HTML / XML / SVG).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_markdown.h>
static int fails = 0;
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
static const char* MD =
"# Titulo\n"
"\n"
"Um paragrafo com **negrito** e *italico* e `codigo`.\n"
"\n"
"- item um\n"
"- item dois\n"
"\n"
"1. primeiro\n"
"2. segundo\n"
"\n"
"> uma citacao\n"
"\n"
"```c\n"
"int x = 1;\n"
"```\n"
"\n"
"[link](http://exemplo.com)\n"
"\n"
"---\n";
int main(void) {
char* h = tb_markdown_to_html(MD);
CHECK(h && strstr(h, "<h1>Titulo</h1>"), "html h1");
CHECK(h && strstr(h, "<strong>negrito</strong>"), "html negrito");
CHECK(h && strstr(h, "<em>italico</em>"), "html italico");
CHECK(h && strstr(h, "<code>codigo</code>"), "html code inline");
CHECK(h && strstr(h, "<ul>") && strstr(h, "<li>item um</li>"), "html lista nao ordenada");
CHECK(h && strstr(h, "<ol>") && strstr(h, "<li>primeiro</li>"), "html lista ordenada");
CHECK(h && strstr(h, "<blockquote>uma citacao</blockquote>"), "html citacao");
CHECK(h && strstr(h, "<pre><code class=\"language-c\">") && strstr(h, "int x = 1;"), "html bloco de codigo");
CHECK(h && strstr(h, "<a href=\"http://exemplo.com\">link</a>"), "html link");
CHECK(h && strstr(h, "<hr>"), "html regra");
free(h);
char* x = tb_markdown_to_xml(MD);
CHECK(x && strstr(x, "<markdown>") && strstr(x, "</markdown>"), "xml raiz");
CHECK(x && strstr(x, "<heading level=\"1\">Titulo</heading>"), "xml heading");
CHECK(x && strstr(x, "<paragraph>") && strstr(x, "<bold>negrito</bold>"), "xml paragrafo/negrito");
CHECK(x && strstr(x, "<list ordered=\"false\">") && strstr(x, "<item>item um</item>"), "xml lista");
CHECK(x && strstr(x, "<code lang=\"c\">"), "xml bloco de codigo");
CHECK(x && strstr(x, "<link href=\"http://exemplo.com\">link</link>"), "xml link");
CHECK(x && strstr(x, "<rule/>"), "xml regra");
free(x);
char* s = tb_markdown_to_svg(MD);
CHECK(s && strstr(s, "<svg xmlns=\"http://www.w3.org/2000/svg\""), "svg raiz");
CHECK(s && strstr(s, "font-weight=\"bold\""), "svg titulo em negrito");
CHECK(s && strstr(s, "font-family=\"monospace\""), "svg bloco monospace");
CHECK(s && strstr(s, "<line "), "svg regra como linha");
CHECK(s && strstr(s, ">Titulo<"), "svg contem texto do titulo");
free(s);
char* s2 = tb_markdown_to_svg_opts("# H", 320, 12);
CHECK(s2 && strstr(s2, "width=\"320\""), "svg largura configuravel");
free(s2);
/* escaping */
char* e = tb_markdown_to_html("a < b & c > d");
CHECK(e && strstr(e, "a &lt; b &amp; c &gt; d"), "html escapa &,<,>");
free(e);
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
TinyBlocks_LibraryBlock* lib = tb_library_register(rt, tb_markdown_get_library());
CHECK(tb_rt_find_type(rt, "Markdown.ToHtml") && tb_rt_find_type(rt, "Markdown.ToXml") &&
tb_rt_find_type(rt, "Markdown.ToSvg"), "blocos Markdown.* registrados");
const TinyBlocks_Category* cat = lib ? tb_library_category(lib) : NULL;
CHECK(cat && !strcmp(cat->name, "Markdown"), "categoria 'Markdown'");
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}