1// Most tests are in reg-test/run 2 3package dsl 4 5import ( 6 "fmt" 7 "testing" 8 9 "miller/src/dsl" 10 "miller/src/parsing/lexer" 11 "miller/src/parsing/parser" 12) 13 14func testSingle(sourceString []byte) (*dsl.AST, error) { 15 fmt.Printf("Input: %s\n", sourceString) 16 theLexer := lexer.NewLexer(sourceString) 17 theParser := parser.NewParser() 18 interfaceAST, err := theParser.Parse(theLexer) 19 if err == nil { 20 return interfaceAST.(*dsl.AST), nil 21 } else { 22 return nil, err 23 } 24} 25 26func TestFail(t *testing.T) { 27 _, err := testSingle([]byte("a b ; d e f")) 28 if err == nil { 29 t.Fatal("Expected parse error") 30 } else { 31 fmt.Printf("Parsing failed as expected: %v\n", err) 32 } 33} 34 35func TestPassOne(t *testing.T) { 36 ast, err := testSingle([]byte("$x = 3")) 37 if err != nil { 38 t.Fatal(err.Error()) 39 } 40 fmt.Println("AST:") 41 ast.Print() 42} 43 44func TestPassTwo(t *testing.T) { 45 ast, err := testSingle([]byte("$x = 3; $y = 0xef")) 46 if err != nil { 47 t.Fatal(err.Error()) 48 } 49 fmt.Println("AST:") 50 ast.Print() 51} 52 53func TestPassThree(t *testing.T) { 54 ast, err := testSingle([]byte("$x = 3; $y = 0xef; $z = true")) 55 if err != nil { 56 t.Fatal(err.Error()) 57 } 58 fmt.Println("AST:") 59 ast.Print() 60} 61