Files
tinyblocks/tests/scratchblocks_test.c
T
mguinhosandClaude Opus 5.5 545aa86a5a Editor/scratchblocks: projeto inteiro por QR e texto em campo redondo
- tb_editor_qr_show_part (para testes) fixa a parte mostrada no painel
  "Compartilhar blueprint" em vez do loop de 1 s; -1 volta ao loop.
- scratchblocks: campo redondo com texto que nao e numero sai entre
  colchetes ("= [#]"), senao a volta lia como variavel; o texto importado
  num campo redondo volta como texto. "exportar(importar(exportado))"
  fica estavel.
- Harness cenario 36: o labirinto 3D (tests/data/labirinto.sb) entra pelo
  MCP, roda (37 cristais), exporta e importa com o mesmo resultado e passa
  pelo QR: cada parte desenhada e lida de volta, recebida fora de ordem e
  com repetidas.
- Fixture tests/data/labirinto.sb (mapa em notacao do forum).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 00:25:40 -03:00

294 lines
14 KiB
C

/*
* scratchblocks_test.c - testa tinyblocks-scratchblocks: notacao de texto do
* forum do Scratch <-> aba de blocos (TbBpTab).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <blueprint.h>
#include <tb_scratchblocks.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)
/* 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, NULL, 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 s_def_n, s_def_parts;
static bool s_def_rep;
static char s_def_name[48];
static void on_define(const char* name, const TbBpParam* parts, int n, bool reporter, void* ud) {
(void)parts; (void)ud;
s_def_n++; s_def_parts = n; s_def_rep = reporter;
snprintf(s_def_name, sizeof(s_def_name), "%s", name);
}
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);
/* -- parenteses a mais: ((seno)) nao vira uma lista chamada "seno..." --- */
t = fresh();
warn[0] = 0;
tb_scratchblocks_import("change [cx v] by (((([sin v] of (yaw)))) * (0.4))\n", t, &CX, warn, sizeof(warn));
{
int mul = child(t, t->top_n ? t->top[0] : -1, 1);
CHECK(!warn[0] && !strcmp(ty(t, mul), "Math.Mul") && !strcmp(ty(t, child(t, mul, 0)), "Number.Sin"),
"((expressão)) com parênteses a mais");
}
drop(t);
/* -- Meus Blocos: define, parametros e chamadas -------------------------- */
{
TbSbContext cx = CX;
cx.define = on_define;
t = fresh();
warn[0] = 0;
int n = tb_scratchblocks_import(
"when flag clicked\n"
"dobro (21) e <(1) = (1)> :: custom\n"
"\n"
"define dobro (n) e <ok>\n"
"if <ok> then\n"
" say ((n) * (2))\n"
"fim\n", t, &cx, warn, sizeof(warn));
if (warn[0]) printf(" aviso: %s\n", warn);
CHECK(n == 2 && !warn[0], "define + chamada: 2 pilhas, sem aviso (fim em português)");
CHECK(s_def_n == 1 && !strcmp(s_def_name, "dobro") && s_def_parts == 3 && !s_def_rep,
"define avisa o chamador: nome e 3 partes (entrada, rótulo, booleana)");
int call = t->top_n ? t->blocks[t->top[0]].next : -1;
CHECK(!strcmp(ty(t, call), "User.dobro") && !strcmp(t->blocks[call].in_text[0], "21"),
"a chamada antes do define já vira User.dobro (21)");
int hat = t->top_n > 1 ? t->top[1] : -1;
int iff = hat >= 0 ? t->blocks[hat].next : -1;
int ret = iff >= 0 ? t->blocks[iff].sub[0] : -1;
int mul = child(t, ret, 0);
CHECK(!strcmp(ty(t, hat), "Port.Define") && !strcmp(t->blocks[hat].in_text[0], "dobro") &&
!strcmp(ty(t, child(t, iff, 0)), "Port.In") && !strcmp(ty(t, child(t, mul, 0)), "Port.In"),
"no corpo, (n) e <ok> viram parâmetros (Port.In), não variáveis");
drop(t);
t = fresh();
tb_scratchblocks_import("say (n)\n", t, &cx, warn, sizeof(warn));
CHECK(!strcmp(ty(t, child(t, t->top_n ? t->top[0] : -1, 0)), "Variables.Get"), "fora do define, (n) é variável");
drop(t);
}
if (fails) printf("\n%d FALHA(S)\n", fails);
else /* projeto inteiro (o labirinto 3D): importar -> exportar -> importar e
exportar de novo da o mesmo texto e a mesma arvore de blocos */
{
FILE* f = fopen("tests/data/labirinto.sb", "rb");
static char src[16384];
size_t got = f ? fread(src, 1, sizeof(src) - 1, f) : 0;
if (f) fclose(f);
src[got] = 0;
CHECK(got > 1000, "fixture tests/data/labirinto.sb");
TbBpTab* a = fresh();
int na = tb_scratchblocks_import(src, a, &CX, warn, sizeof(warn));
/* sem a biblioteca 3D no contexto os blocos dela ficam como desconhecidos
(e voltam iguais na exportacao); o harness roda o projeto de verdade */
CHECK(na == 1, "labirinto: importa numa pilha");
char* e1 = tb_scratchblocks_export(a, &CX);
TbBpTab* b = fresh();
tb_scratchblocks_import(e1 ? e1 : "", b, &CX, warn, sizeof(warn));
char* e2 = tb_scratchblocks_export(b, &CX);
CHECK(e1 && e2 && !strcmp(e1, e2), "labirinto: exportar(importar(exportado)) e estavel");
bool same = a->blocks_n == b->blocks_n;
for (int k = 0; same && k < a->blocks_n; k++)
same = !strcmp(a->blocks[k].type, b->blocks[k].type) &&
!strcmp(a->blocks[k].in_text[0], b->blocks[k].in_text[0]);
CHECK(same, "labirinto: a ida e volta preserva os tipos e os campos de cada bloco");
int gets = 0;
for (int k = 0; k < b->blocks_n; k++)
if (!strcmp(b->blocks[k].type, "Variables.Get") && !strcmp(b->blocks[k].in_text[0], "#")) gets++;
CHECK(gets == 0, "labirinto: o \"#\" do mapa nao vira variavel");
free(e1); free(e2); drop(a); drop(b);
}
/* texto num campo redondo volta como texto: "= [#]", nao "(#)" (variavel) */
{
TbBpTab* a = fresh();
tb_scratchblocks_import("when flag clicked\nif <(x) = [#]> then\nend\n", a, &CX, warn, sizeof(warn));
char* e = tb_scratchblocks_export(a, &CX);
CHECK(e && strstr(e, "= [#]") && !strstr(e, "(#)"), "texto em campo redondo exporta entre colchetes");
TbBpTab* b = fresh();
tb_scratchblocks_import(e ? e : "", b, &CX, warn, sizeof(warn));
int gets = 0;
for (int k = 0; k < b->blocks_n; k++) if (!strcmp(b->blocks[k].type, "Variables.Get")) gets++;
CHECK(gets == 1, "ida e volta: o \"#\" continua texto (so a variavel x vira repórter)");
free(e); drop(a); drop(b);
}
printf("\nTUDO OK\n");
return fails != 0;
}