| 1 | package sr |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/goccmack/gocc/example/sr/ast" |
| 7 | "github.com/goccmack/gocc/example/sr/lexer" |
| 8 | "github.com/goccmack/gocc/example/sr/parser" |
| 9 | ) |
| 10 | |
| 11 | func parse(src string) (stmt ast.Stmt, err error) { |
| 12 | lex := lexer.NewLexer([]byte(src)) |
| 13 | p := parser.NewParser() |
| 14 | if res, err := p.Parse(lex); err == nil { |
| 15 | stmt = res.(ast.Stmt) |
| 16 | } |
| 17 | return |
| 18 | } |
| 19 | |
| 20 | func Test1(t *testing.T) { |
| 21 | stmt, err := parse("if c1 then s1") |
| 22 | if err != nil { |
| 23 | t.Fatal(err.Error()) |
| 24 | } |
| 25 | ifs, ok := stmt.(*ast.If) |
| 26 | if !ok { |
| 27 | t.Fatalf("sr_test.Test1: stmt is not *ast.If") |
| 28 | } |
| 29 | if !ifs.MatchIf("c1", ast.IdStmt("s1")) { |
| 30 | t.Fail() |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func Test2(t *testing.T) { |
| 35 | stmt, err := parse("if c1 then s1 else s2") |
| 36 | if err != nil { |
| 37 | t.Fatal(err.Error()) |
| 38 | } |
| 39 | ifes, ok := stmt.(*ast.IfElse) |
| 40 | if !ok { |
| 41 | t.Fatalf("stmt is not *ast.IfElse") |
| 42 | } |
| 43 | if !ifes.MatchIfElse("c1", ast.IdStmt("s1"), ast.IdStmt("s2")) { |
| 44 | t.Fail() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func Test3(t *testing.T) { |
| 49 | stmt, err := parse("if c1 then if c2 then s2 else s3") |
| 50 | if err != nil { |
| 51 | t.Fatal(err.Error()) |
| 52 | } |
| 53 | ifs, ok := stmt.(*ast.If) |
| 54 | if !ok { |
| 55 | t.Fatalf("stmt is not *ast.IfElse") |
| 56 | } |
| 57 | ife2 := &ast.IfElse{C: "c2", S1: ast.IdStmt("s2"), S2: ast.IdStmt("s3")} |
| 58 | if !ifs.MatchIf("c1", ife2) { |
| 59 | t.Fail() |
| 60 | } |
| 61 | } |