/* * 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?"}, {"Image.New", "nova imagem(%1 x %2) cor r %3 g %4 b %5"}, /* ( ) e , no rotulo */ {"Image.SetPixel", "pinte a imagem %1 em (%2,%3) com r %4 g %5 b %6"}, }; static int shape(const char* type, void* ud) { (void)ud; if (!strcmp(type, "FS.IsDir")) return TB_SB_BOOLEAN; if (!strcmp(type, "Image.New")) return TB_SB_REPORTER; 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, (int)(sizeof(s_labels) / sizeof(s_labels[0])), shape, slot_kind, category, NULL }; static TbBpTab* fresh(void) { return calloc(1, sizeof(TbBpTab)); } /* a aba tem pool dinamico desde o limite de 65536 blocos */ static void drop(TbBpTab* t) { if (!t) return; tb_blueprint_tab_free(t); free(t); } 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); drop(t2); drop(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); drop(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"); drop(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); drop(t); /* -- rotulos com ( ) e , : com os parenteses do exportador e sem eles --- */ t = fresh(); warn[0] = 0; n = tb_scratchblocks_import("set [tela v] to (nova imagem((96) x (72)) cor r (135) g (200) b (255))\n" "pinte a imagem (tela) em ((px),(py)) com r (1) g (2) b (3)\n" "pinte a imagem (tela) em (4) , (5) com r (1) g (2) b (3)\n", t, &CX, warn, sizeof(warn)); { int set = t->top_n ? t->top[0] : -1, img = child(t, set, 1); int p1 = set >= 0 ? t->blocks[set].next : -1, p2 = p1 >= 0 ? t->blocks[p1].next : -1; CHECK(!warn[0] && !strcmp(ty(t, img), "Image.New") && !strcmp(t->blocks[img].in_text[0], "96") && !strcmp(t->blocks[img].in_text[4], "255"), "rótulo com ( ) no meio: nova imagem((96) x (72))"); CHECK(!strcmp(ty(t, p1), "Image.SetPixel") && !strcmp(ty(t, child(t, p1, 1)), "Variables.Get"), "rótulo com (x,y): pinte ... em ((px),(py))"); CHECK(!strcmp(ty(t, p2), "Image.SetPixel") && !strcmp(t->blocks[p2].in_text[2], "5"), "e sem os parênteses: em (4) , (5)"); txt = tb_scratchblocks_export(t, &CX); TbBpTab* t3 = fresh(); int n3 = txt ? tb_scratchblocks_import(txt, t3, &CX, warn, sizeof(warn)) : 0; char* txt3 = n3 > 0 ? tb_scratchblocks_export(t3, &CX) : NULL; CHECK(txt && txt3 && !strcmp(txt, txt3), "rótulos com pontuação: exporta e importa de novo igual"); if (txt && txt3 && strcmp(txt, txt3)) printf("---- 1:\n%s---- 2:\n%s", txt, txt3); free(txt); free(txt3); drop(t3); } drop(t); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }