/* * syntax_test.c - testa tinyblocks-syntax (realce -> XML). */ #include #include #include #include #include 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, ""), "XML com "); 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, """) == NULL && strstr(x, "\"hi\""), "string literal"); CHECK(x && strstr(x, "") && strstr(x, ""), "linhas separadas"); free(x); x = tb_syntax_highlight("def f(x):\n return x # ret", "py"); CHECK(x && strstr(x, ""), "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("texto", "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, "") && strstr(x, ">static<") && strstr(x, ">void<"), "inline: sem , 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; }