/* * Teste do parser TOML (tinyblocks-toml): valida contra os library.toml do * repo e alguns casos sintaticos. */ #include #include #include #include static int fails = 0; #define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; } } while (0) static void test_library_files(void) { glob_t g; if (glob("libraries/*/library.toml", 0, NULL, &g) != 0) { printf("FAIL nenhum library.toml encontrado\n"); fails++; return; } int n = 0; for (size_t i = 0; i < g.gl_pathc; i++) { TomlLexError err; TomlDoc* doc = toml_parse_file(g.gl_pathv[i], &err); if (!doc) { printf("FAIL parse %s: %s (%d:%d)\n", g.gl_pathv[i], err.msg, err.line, err.col); fails++; continue; } const char* name = toml_as_string(toml_path_get(doc->root, "library.name"), NULL); const char* cat = toml_as_string(toml_path_get(doc->root, "library.category"), NULL); const char* so = toml_as_string(toml_path_get(doc->root, "build.shared"), NULL); if (!name || !cat || !so) { printf("FAIL metadados incompletos em %s\n", g.gl_pathv[i]); fails++; } else { n++; } toml_free(doc); } globfree(&g); CHECK(n >= 13, "todos os library.toml tem name/category/build.shared"); printf(" %d arquivos .toml validados\n", n); } static void test_syntax(void) { const char* src = "# comentario\n" "title = \"TOML \\u00e9 \\\"legal\\\"\"\n" "lit = 'C:\\Users\\x'\n" "int = 1_000\n" "hex = 0xDEAD_BEEF\n" "flt = 3.14e2\n" "b = true\n" "arr = [1, 2, 3, \"quatro\"]\n" "date = 1979-05-27T07:32:00Z\n" "dotted.key = \"v\"\n" "inline = { x = 1, y = 2 }\n" "\n" "[servidor]\n" "host = \"localhost\"\n" "porta = 8080\n" "\n" "[[itens]]\n" "nome = \"a\"\n" "[[itens]]\n" "nome = \"b\"\n"; TomlLexError err; TomlDoc* doc = toml_parse(src, &err); CHECK(doc != NULL, "parser aceita TOML variado"); if (!doc) { printf(" erro: %s (%d:%d)\n", err.msg, err.line, err.col); return; } CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "title"), ""), "TOML \xc3\xa9 \"legal\"") == 0, "string basica com escapes/unicode"); CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "lit"), ""), "C:\\Users\\x") == 0, "string literal"); CHECK(toml_as_int(toml_path_get(doc->root, "int"), 0) == 1000, "inteiro com underline"); CHECK(toml_as_int(toml_path_get(doc->root, "hex"), 0) == 0xDEADBEEF, "inteiro hexadecimal"); CHECK(toml_as_float(toml_path_get(doc->root, "flt"), 0) == 314.0, "float com expoente"); CHECK(toml_as_bool(toml_path_get(doc->root, "b"), false) == true, "booleano"); CHECK(toml_array_count(toml_path_get(doc->root, "arr")) == 4, "array com 4 itens"); CHECK(strcmp(toml_as_string(toml_array_get(toml_path_get(doc->root, "arr"), 3), ""), "quatro") == 0, "item de array"); CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "date"), ""), "1979-05-27T07:32:00Z") == 0, "data/hora"); CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "dotted.key"), ""), "v") == 0, "chave pontuada"); CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "servidor.host"), ""), "localhost") == 0, "tabela [servidor]"); CHECK(toml_as_int(toml_path_get(doc->root, "servidor.porta"), 0) == 8080, "valor de tabela"); CHECK(toml_array_count(toml_path_get(doc->root, "itens")) == 2, "array de tabelas"); CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "itens.1.nome"), ""), "b") == 0, "segundo item do array de tabelas"); CHECK(toml_as_int(toml_path_get(doc->root, "inline.x"), 0) == 1 && toml_as_int(toml_path_get(doc->root, "inline.y"), 0) == 2, "tabela inline"); toml_free(doc); } int main(void) { test_syntax(); test_library_files(); printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails); return fails != 0; }