/* * scratchblocks_test.c - testa tinyblocks-scratchblocks: notacao de texto do * forum do Scratch <-> aba de blocos (TbBpTab). */ #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) /* rotulos "do editor" para blocos que so o TinyBlocks tem */ static const TbSbLabel s_labels[] = { {"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3"}, {"FS.IsDir", "%1 é pasta?"}, }; static int shape(const char* type, void* ud) { (void)ud; if (!strcmp(type, "FS.IsDir")) return TB_SB_BOOLEAN; return -1; /* o resto: tabela interna */ } static int slot_kind(const char* type, int slot, void* ud) { (void)ud; (void)slot; return !strcmp(type, "Stage.Circle") ? TB_SB_SLOT_NUM : TB_SB_SLOT_TEXT; } static const char* category(const char* type, void* ud) { (void)ud; return !strncmp(type, "Stage.", 6) ? "pen" : "extension"; } static const TbSbContext CX = { s_labels, 2, shape, slot_kind, category, NULL }; static TbBpTab* fresh(void) { return calloc(1, sizeof(TbBpTab)); } static const char* SCRIPT = "[scratchblocks]\n" "when flag clicked\n" "set [x v] to (0)\n" "repeat (10)\n" " change [x v] by (1) // conta\n" " if <(x) > (5)> then\n" " say [grande]\n" " else\n" " say (join [x = ] (x))\n" " end\n" "end\n" "delete all of [frutas v]\n" "add [maçã] to [frutas v]\n" "say (item (1) of [frutas v])\n" "[/scratchblocks]\n"; static const char* EXPECTED = "when flag clicked\n" "set [x v] to (0)\n" "repeat (10)\n" " change [x v] by (1)\n" " if <(x) > (5)> then\n" " say [grande]\n" " else\n" " say (join [x = ] (x))\n" " end\n" "end\n" "delete all of [frutas v]\n" "add [maçã] to [frutas v]\n" "say (item (1) of [frutas v])\n"; static int child(const TbBpTab* t, int bi, int s) { return bi >= 0 ? t->blocks[bi].args[s] : -1; } static const char* ty(const TbBpTab* t, int bi) { return bi >= 0 ? t->blocks[bi].type : "(nada)"; } int main(void) { char warn[256]; /* -- importar --- */ TbBpTab* t = fresh(); int n = tb_scratchblocks_import(SCRIPT, t, &CX, warn, sizeof(warn)); CHECK(n == 1 && !warn[0], "importa um script (uma pilha, sem avisos)"); int hat = t->top_n ? t->top[0] : -1; CHECK(!strcmp(ty(t, hat), "Events.WhenRun"), "chapéu: quando a bandeira for clicada"); int set = t->blocks[hat].next; CHECK(!strcmp(ty(t, set), "Variables.Set") && !strcmp(t->blocks[set].in_text[0], "x") && !strcmp(t->blocks[set].in_text[1], "0"), "set [x v] to (0)"); int rep = t->blocks[set].next; CHECK(!strcmp(ty(t, rep), "Control.Repeat") && !strcmp(t->blocks[rep].in_text[0], "10"), "repeat (10)"); int chg = t->blocks[rep].sub[0]; CHECK(!strcmp(ty(t, chg), "Variables.Change"), "corpo do repeat começa com change (comentário ignorado)"); int iff = t->blocks[chg].next; CHECK(!strcmp(ty(t, iff), "Control.IfElse"), "if ... else ... end vira se/senão"); int cond = child(t, iff, 0); CHECK(!strcmp(ty(t, cond), "Operators.Gt") && !strcmp(ty(t, child(t, cond, 0)), "Variables.Get") && !strcmp(t->blocks[cond].in_text[1], "5"), "condição <(x) > (5)> com variável e número"); int say2 = t->blocks[iff].sub[1]; int join = child(t, say2, 0); CHECK(!strcmp(ty(t, join), "Text.Concat") && !strcmp(t->blocks[join].in_text[0], "x = "), "senão: say (join [x = ] (x))"); int after = t->blocks[rep].next; CHECK(!strcmp(ty(t, after), "List.Clear") && !strcmp(t->blocks[after].in_text[0], "frutas"), "depois do end: delete all of [frutas v]"); int add = t->blocks[after].next; CHECK(!strcmp(ty(t, add), "List.Push") && !strcmp(t->blocks[add].in_text[0], "maçã"), "add [maçã] to [frutas v]"); int say3 = t->blocks[add].next; int item = child(t, say3, 0); CHECK(!strcmp(ty(t, item), "List.Item") && !strcmp(ty(t, child(t, item, 0)), "List.Var") && !strcmp(t->blocks[child(t, item, 0)].in_text[0], "frutas"), "item (1) of [frutas v] usa a lista com nome"); /* -- exportar --- */ char* txt = tb_scratchblocks_export(t, &CX); CHECK(txt && !strcmp(txt, EXPECTED), "exporta igual ao texto do fórum"); if (txt && strcmp(txt, EXPECTED)) printf("---- obtido:\n%s----\n", txt); /* ida e volta */ TbBpTab* t2 = fresh(); tb_scratchblocks_import(txt ? txt : "", t2, &CX, warn, sizeof(warn)); char* txt2 = tb_scratchblocks_export(t2, &CX); CHECK(txt2 && txt && !strcmp(txt, txt2), "ida e volta (exportar(importar(texto))) é estável"); free(txt); free(txt2); free(t2); free(t); /* -- blocos só do TinyBlocks: rótulo em português + ::categoria --- */ t = fresh(); n = tb_scratchblocks_import( "when flag clicked\n" "desenhe círculo em x (10) y (-20) raio (30) :: pen\n" "if <[/tmp] é pasta? :: extension> then\n" " erase all\n" "end\n" "\n" "forever\n" " set pen size to (4)\n" "end\n" "say [isto vira outra pilha]\n", t, &CX, warn, sizeof(warn)); CHECK(n == 3 && !warn[0], "rótulos em português, chapéu, \"sempre\" e linha em branco separam pilhas"); if (!(n == 3 && !warn[0])) printf(" n=%d aviso=%s\n", n, warn); txt = tb_scratchblocks_export(t, &CX); CHECK(txt && strstr(txt, "desenhe círculo em x (10) y (-20) raio (30) :: pen\n"), "exporta bloco do TinyBlocks com rótulo em português e ::pen"); CHECK(txt && strstr(txt, "if <[/tmp] é pasta? :: extension> then\n"), "booleano do TinyBlocks dentro do if"); if (txt && !strstr(txt, "if <[/tmp] é pasta? :: extension> then\n")) printf("---- obtido:\n%s----\n", txt); CHECK(txt && strstr(txt, "forever\n set pen size to (4)\nend\n"), "sempre (tampa) com corpo"); free(txt); free(t); /* -- linha desconhecida: avisa e segue --- */ t = fresh(); n = tb_scratchblocks_import("when flag clicked\nvoe para a lua\nerase all\n", t, &CX, warn, sizeof(warn)); CHECK(n == 1 && strstr(warn, "linha 2"), "linha desconhecida gera aviso com o número da linha"); CHECK(!strcmp(ty(t, t->blocks[t->top[0]].next), "Stage.Clear"), "e o resto continua sendo importado"); free(t); /* -- texto com colchetes é escapado --- */ t = fresh(); tb_scratchblocks_import("say [a \\] b]\n", t, &CX, warn, sizeof(warn)); CHECK(!strcmp(t->blocks[t->top[0]].in_text[0], "a ] b"), "\\] dentro de [ ] é texto"); txt = tb_scratchblocks_export(t, &CX); CHECK(txt && !strcmp(txt, "say [a \\] b]\n"), "exporta escapando o ]"); free(txt); free(t); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }