@@ -211,11 +211,30 @@ impl ToDoc for Statement {
211211 // Metabuiltin statement: just emit its name as a standalone command.
212212 Statement :: MetaStmt ( name) => concat ( text ( "meta " ) , text ( name. clone ( ) ) ) ,
213213
214- // Test definitions (similar a function, mas preservando palavra-chave test/modtest)
214+ // Test definitions: seguem a sintaxe do parser `test nome(): ... end`.
215+ // O parser já restringe TestDef a funções sem parâmetros e retorno Void;
216+ // aqui apenas espelhamos essa forma textual.
215217 Statement :: TestDef ( func) => {
216- // Reusa ToDoc de Function mas prefixa com 'test '.
217- concat ( text ( "test " ) , func. to_doc ( ) )
218+ let header = concat (
219+ text ( "test " ) ,
220+ concat ( text ( func. name . clone ( ) ) , text ( "()" ) ) ,
221+ ) ;
222+
223+ let body_doc = func
224+ . body
225+ . as_ref ( )
226+ // Para testes unitários sem corpo explícito, usamos um bloco vazio
227+ // alinhado com a gramática: `test nome(): end`.
228+ . map_or_else ( || concat ( text ( ":" ) , text ( " end" ) ) , |b| b. to_doc ( ) ) ;
229+
230+ // Importante: não inserir espaço entre `()` e `:`; o parser espera
231+ // que o bloco comece imediatamente após `)`.
232+ concat ( header, body_doc)
218233 }
234+
235+ // ModTestDef: mantemos o prefixo `modtest` seguido do nome do módulo e
236+ // do statement interno. Quando o corpo é um bloco, o pretty-printer
237+ // produz naturalmente `: ... end`, alinhado com o estilo de `test`.
219238 Statement :: ModTestDef ( module, stmt) => concat (
220239 text ( "modtest " ) ,
221240 concat ( text ( module. clone ( ) ) , concat ( text ( " " ) , stmt. to_doc ( ) ) ) ,
@@ -362,7 +381,14 @@ mod tests {
362381 Box :: new ( Statement :: Return ( Box :: new ( Expression :: CInt ( 1 ) ) ) ) ,
363382 ) ;
364383 let t1 = pretty ( 80 , & test_def. to_doc ( ) ) ;
365- assert ! ( t1. starts_with( "test def f" ) ) ;
384+ // Formato deve bater exatamente com a gramática do parser: `test f(): end`.
385+ assert_eq ! ( t1, "test f(): end" ) ;
386+
387+ // Verifica também que o texto gerado é parseável como TestDef.
388+ use crate :: parser:: parse_statement;
389+ let ( rest, parsed) = parse_statement ( & t1) . expect ( "pretty output for TestDef must parse" ) ;
390+ assert ! ( rest. trim( ) . is_empty( ) ) ;
391+ matches ! ( parsed, Statement :: TestDef ( _) ) ;
366392 let t2 = pretty ( 80 , & mod_test. to_doc ( ) ) ;
367393 assert ! ( t2. starts_with( "modtest m " ) ) ;
368394 }
0 commit comments