|
| 1 | +package parser |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/sunholo-data/ailang/internal/lexer" |
| 8 | +) |
| 9 | + |
| 10 | +// TestPAR020_MissingBlockSemicolon guards M-AILANG-ERROR-QUALITY: a missing ';' |
| 11 | +// between block statements (the mirror of PAR017's "extra ';' in =-body") must |
| 12 | +// produce an actionable PAR020 error naming the fix, not a bare |
| 13 | +// "expected }, got X". This is the #1 unactionable thrash-causer on small models |
| 14 | +// — config_file_parser burned 66 agent turns on a generic "expected }, got if". |
| 15 | +func TestPAR020_MissingBlockSemicolon(t *testing.T) { |
| 16 | + t.Run("func body missing semicolon (the 66-turn pattern)", func(t *testing.T) { |
| 17 | + input := "module t\n" + |
| 18 | + "pure func f(n: int) -> int {\n" + |
| 19 | + " let x = n\n" + // <-- missing ';' |
| 20 | + " if x > 0 then 1 else 0\n" + |
| 21 | + "}\n" |
| 22 | + err := firstParserErrorWithCode(t, input, "PAR020") |
| 23 | + if err == nil { |
| 24 | + t.Fatal("expected PAR020 for a missing ';' between block statements") |
| 25 | + } |
| 26 | + msg := err.Error() |
| 27 | + if !strings.Contains(msg, "missing ';'") { |
| 28 | + t.Errorf("message should name the missing ';': %s", msg) |
| 29 | + } |
| 30 | + if !strings.Contains(msg, "separated by `;`") { |
| 31 | + t.Errorf("message should explain block-statement separation: %s", msg) |
| 32 | + } |
| 33 | + if len(err.Suggestions) == 0 { |
| 34 | + t.Error("PAR020 should carry concrete fix suggestions") |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + t.Run("missing semicolon before another let", func(t *testing.T) { |
| 39 | + input := "module t\n" + |
| 40 | + "pure func f(n: int) -> int {\n" + |
| 41 | + " let x = n\n" + // <-- missing ';' |
| 42 | + " let y = x\n" + |
| 43 | + " y\n" + |
| 44 | + "}\n" |
| 45 | + if firstParserErrorWithCode(t, input, "PAR020") == nil { |
| 46 | + t.Error("expected PAR020 when a let-statement is followed by another without ';'") |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("valid block does NOT trigger PAR020", func(t *testing.T) { |
| 51 | + input := "module t\n" + |
| 52 | + "pure func f(n: int) -> int { let x = n; let y = x + 1; y }\n" |
| 53 | + if err := firstParserErrorWithCode(t, input, "PAR020"); err != nil { |
| 54 | + t.Errorf("valid semicolon-separated block must not trigger PAR020: %s", err.Error()) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("single-expression body does NOT trigger PAR020", func(t *testing.T) { |
| 59 | + input := "module t\n" + |
| 60 | + "pure func f(n: int) -> int { n + 1 }\n" |
| 61 | + if err := firstParserErrorWithCode(t, input, "PAR020"); err != nil { |
| 62 | + t.Errorf("single-expression body must not trigger PAR020: %s", err.Error()) |
| 63 | + } |
| 64 | + }) |
| 65 | +} |
| 66 | + |
| 67 | +// firstParserErrorWithCode parses input and returns the first *ParserError whose |
| 68 | +// Code matches, or nil if none. |
| 69 | +func firstParserErrorWithCode(t *testing.T, input, code string) *ParserError { |
| 70 | + t.Helper() |
| 71 | + l := lexer.New(input, "<test>") |
| 72 | + p := New(l) |
| 73 | + _ = p.Parse() |
| 74 | + for _, e := range p.errors { |
| 75 | + if pe, ok := e.(*ParserError); ok && pe.Code == code { |
| 76 | + return pe |
| 77 | + } |
| 78 | + } |
| 79 | + return nil |
| 80 | +} |
0 commit comments