Adiciona tinyblocks-syntax, tinyblocks-regex e tinyblocks-markdown (com testes)

This commit is contained in:
mguinhos committed 2026-10-20 22:04:09 -03:00
1 parent c75d4be1c2
commit a921f6574d
16 files changed
+1846 -4

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
/*
* 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;
}
+68
View File
@@ -0,0 +1,68 @@
/*
* regex_test.c - testa tinyblocks-regex (POSIX ERE).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_regex.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)
int main(void) {
CHECK(tb_regex_test("abc123def", "[0-9]+"), "test encontra digitos");
CHECK(!tb_regex_test("abcdef", "[0-9]+"), "test rejeita sem digitos");
CHECK(tb_regex_match("aaa", "^a+$"), "match ancorado ok");
CHECK(!tb_regex_match("aaab", "^a+$"), "match ancorado rejeita");
char* s = tb_regex_find("abc123def456", "[0-9]+");
CHECK(s && !strcmp(s, "123"), "find primeiro casamento");
free(s);
CHECK(tb_regex_count("abc123def456", "[0-9]+") == 2, "count = 2");
s = tb_regex_group("user@host.com", "([a-z]+)@([a-z.]+)", 1);
CHECK(s && !strcmp(s, "user"), "group 1");
free(s);
s = tb_regex_group("user@host.com", "([a-z]+)@([a-z.]+)", 2);
CHECK(s && !strcmp(s, "host.com"), "group 2");
free(s);
CHECK(tb_regex_group_count("", "([a-z]+)@([a-z.]+)") == 2, "group_count = 2");
s = tb_regex_replace("abc123def456", "[0-9]+", "#");
CHECK(s && !strcmp(s, "abc#def#"), "replace todas");
free(s);
s = tb_regex_replace("2024-06-15", "([0-9]{4})-([0-9]{2})-([0-9]{2})", "$3/$2/$1");
CHECK(s && !strcmp(s, "15/06/2024"), "replace com backrefs $1..$3");
free(s);
s = tb_regex_replace("a.b", "\\.", "$$");
CHECK(s && !strcmp(s, "a$b"), "replace com $$ literal");
free(s);
int n = 0;
char** parts = tb_regex_split("um,dois,tres", ",", &n);
CHECK(parts && n == 3 && !strcmp(parts[0], "um") && !strcmp(parts[1], "dois") &&
!strcmp(parts[2], "tres"), "split por virgula");
tb_regex_free_list(parts);
parts = tb_regex_split("a1b22c", "[0-9]+", &n);
CHECK(parts && n == 3 && !strcmp(parts[0], "a") && !strcmp(parts[2], "c"), "split por regex");
tb_regex_free_list(parts);
CHECK(!tb_regex_test("x", "[") && strlen(tb_regex_last_error()) > 0, "padrao invalido -> erro");
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
TinyBlocks_LibraryBlock* lib = tb_library_register(rt, tb_regex_get_library());
CHECK(tb_rt_find_type(rt, "Regex.Test") && tb_rt_find_type(rt, "Regex.Replace") &&
tb_rt_find_type(rt, "Regex.Split") && tb_rt_find_type(rt, "Regex.Group"),
"blocos Regex.* registrados");
const TinyBlocks_Category* cat = lib ? tb_library_category(lib) : NULL;
CHECK(cat && !strcmp(cat->name, "Regex"), "categoria 'Regex'");
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}
+64
View File
@@ -0,0 +1,64 @@
/*
* syntax_test.c - testa tinyblocks-syntax (realce -> XML).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_syntax.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)
int main(void) {
char* x = tb_syntax_highlight("int x = 42; // ola\nchar* s = \"hi\";", "c");
CHECK(x && strstr(x, "<code lang=\"c\">"), "XML com <code lang>");
CHECK(x && strstr(x, "type=\"type\"") && strstr(x, ">int<"), "'int' realcado como tipo");
CHECK(x && strstr(x, "type=\"type\"") && strstr(x, ">char<"), "'char' como tipo");
CHECK(x && strstr(x, ">42<"), "numero presente");
CHECK(x && strstr(x, ">// ola<") && strstr(x, "type=\"cmt\""), "comentario de linha");
CHECK(x && strstr(x, "type=\"str\"") && strstr(x, "&quot;") == NULL && strstr(x, "\"hi\""), "string literal");
CHECK(x && strstr(x, "<line>") && strstr(x, "</line>"), "linhas separadas");
free(x);
x = tb_syntax_highlight("def f(x):\n return x # ret", "py");
CHECK(x && strstr(x, "<code lang=\"python\">"), "python normalizado de 'py'");
CHECK(x && strstr(x, ">def<") && strstr(x, "type=\"kw\""), "'def' keyword em python");
CHECK(x && strstr(x, "type=\"cmt\""), "comentario '#' em python");
free(x);
x = tb_syntax_highlight("{\"a\": 1, \"b\": [true, null]}", "json");
CHECK(x && strstr(x, "type=\"kw\"") && strstr(x, ">true<"), "json true/null");
CHECK(x && strstr(x, "type=\"str\""), "json string");
free(x);
x = tb_syntax_highlight("<a href=\"x\">texto</a>", "xml");
CHECK(x && strstr(x, "type=\"type\"") && strstr(x, ">a<"), "tag xml");
CHECK(x && strstr(x, ">href<"), "atributo xml");
free(x);
x = tb_syntax_highlight_inline("static void eval_stage(TinyBlocks_Block *self, TinyBlocks *rt)", "c");
CHECK(x && !strstr(x, "<line>") && strstr(x, ">static<") && strstr(x, ">void<"),
"inline: sem <line>, com keywords");
free(x);
CHECK(!strcmp(tb_syntax_normalize_lang("py"), "python"), "normalize py");
CHECK(!strcmp(tb_syntax_normalize_lang("C"), "c"), "normalize C (case-insensitive)");
CHECK(!strcmp(tb_syntax_normalize_lang(NULL), "texto"), "normalize NULL -> texto");
CHECK(!strcmp(tb_syntax_normalize_lang("arquivo.c"), "c"), "normalize com extensao");
CHECK(!strcmp(tb_syntax_type_color("kw"), "#c678dd"), "cor do tipo kw");
CHECK(tb_syntax_language_count() >= 6, "pelo menos 6 linguagens");
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
TinyBlocks_LibraryBlock* lib = tb_library_register(rt, tb_syntax_get_library());
CHECK(tb_rt_find_type(rt, "Syntax.Highlight") && tb_rt_find_type(rt, "Syntax.Inline") &&
tb_rt_find_type(rt, "Syntax.Languages"), "blocos Syntax.* registrados");
const TinyBlocks_Category* cat = lib ? tb_library_category(lib) : NULL;
CHECK(cat && !strcmp(cat->name, "Sintaxe"), "categoria 'Sintaxe'");
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}