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

+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;
}