| 1 | package astx |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/goccmack/gocc/example/astx/ast" |
| 8 | "github.com/goccmack/gocc/example/astx/lexer" |
| 9 | "github.com/goccmack/gocc/example/astx/parser" |
| 10 | ) |
| 11 | |
| 12 | func TestPass(t *testing.T) { |
| 13 | sml, err := test([]byte("a b c d e f")) |
| 14 | if err != nil { |
| 15 | t.Fatal(err.Error()) |
| 16 | } |
| 17 | fmt.Printf("output: %s\n", sml) |
| 18 | } |
| 19 | |
| 20 | func TestFail(t *testing.T) { |
| 21 | _, err := test([]byte("a b ; d e f")) |
| 22 | if err == nil { |
| 23 | t.Fatal("expected parse error") |
| 24 | } else { |
| 25 | fmt.Printf("Parsing failed as expected: %v\n", err) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func test(src []byte) (astree ast.StmtList, err error) { |
| 30 | fmt.Printf("input: %s\n", src) |
| 31 | s := lexer.NewLexer(src) |
| 32 | p := parser.NewParser() |
| 33 | a, err := p.Parse(s) |
| 34 | if err == nil { |
| 35 | astree = a.(ast.StmtList) |
| 36 | } |
| 37 | return |
| 38 | } |