How to use TestErrors method of ast Package

Best Syzkaller code snippet using ast.TestErrors

evaluator_add_test.go

Source:evaluator_add_test.go Github

copy

Full Screen

1package evaluator2import (3 "monkey/ast"4 "monkey/lexer"5 "monkey/object"6 "monkey/parser"7 "monkey/token"8 "testing"9)10//TODO: specify error messages11func TestArityCallExpressions(t *testing.T) {12 tests := []struct {13 input string14 expErr bool15 errmsg string16 }{17 // to be specified: error msg18 {"let truther = fn(x){false}; truther()", true, "not enough"},19 {"let id = fn(x){x}; id()", true, "not enough"},20 {"let add = fn(x,y){x+y}; add(2)", true, "not enough"},21 // regression22 {"let zero = fn(){0}; zero()", false, ""},23 {"let truther = fn(x){false}; truther(1)", false, ""},24 {"let id = fn(x){x}; id(1)", false, ""},25 {"let add = fn(x,y){x+y}; add(1,2)", false, ""},26 // to be specified: whether we want an error + error msg27 {"let zero = fn(){0}; zero(1)", false, ""},28 {"let zero = fn(){0}; zero(1,2,3)", false, ""},29 {"let truther = fn(x){false}; truther(1,2)", true, "too many"},30 {"let id = fn(x){x}; id(1,2)", true, "too many"},31 {"let add = fn(x,y){x+y}; add(1,2,3)", true, "too many"},32 }33 for _, tt := range tests {34 l := lexer.New(tt.input)35 p := parser.New(l)36 ast := p.ParseProgram()37 if len(p.Errors()) > 0 {38 t.Errorf("Unexpected parse errors for %q", tt.input) // either wrong test setup or bugs in parser39 continue40 }41 // we are testing specifically for runtime errors in evaluation42 testErrors(tt.input, ast, tt.expErr, tt.errmsg, t)43 }44}45//TODO: specify error messages46func TestDivisionByZero(t *testing.T) {47 tests := []struct {48 input string49 expErr bool50 errmsg string51 }{52 {"3/0", true, "shame on you"}, // literally zero53 {"-3/(1-1)", true, "shame on you"}, // evaluating to zero54 {"0/1", false, ""}, // regression55 }56 for _, tt := range tests {57 l := lexer.New(tt.input)58 p := parser.New(l)59 ast := p.ParseProgram()60 if len(p.Errors()) > 0 {61 t.Errorf("Unexpected parse errors for %q", tt.input) // either wrong test setup or bugs in parser62 continue63 }64 // we are testing specifically for runtime errors in evaluation65 testErrors(tt.input, ast, tt.expErr, tt.errmsg, t)66 }67}68func testErrors(input string, ast *ast.Program, expErr bool, expMsg string, t *testing.T) {69 env := object.NewEnvironment()70 defer func() {71 if err := recover(); err != nil {72 t.Errorf("Evaluation panics for %q: %q", input, err)73 }74 }()75 value := Eval(ast, env)76 errobj, hasErr := value.(*object.Error)77 if expErr && !hasErr {78 t.Errorf("Error missing for: %q", input)79 return80 }81 if !expErr && hasErr {82 t.Errorf("Unexpected error message for %q: %q", input, errobj.Message)83 }84 if expErr && hasErr && errobj.Message != expMsg {85 t.Errorf("Wrong error message for %q: %q (should be %q)", input, errobj.Message, expMsg)86 }87}88func TestEvalToBoolConsistency(t *testing.T) {89 env, tests := setupEvalToBoolean()90 for _, tt := range tests {91 env.Set("a", tt.object)92 expr1 := "if(a){true} else {false}"93 expr2 := "!!a"94 if evaluate(expr1, env, t) != evaluate(expr2, env, t) {95 t.Errorf("inconsistent evaluation to bool for: %q ", tt.description)96 }97 }98}99func TestEvalToBoolCorrectness(t *testing.T) {100 env, tests := setupEvalToBoolean()101 for _, tt := range tests {102 env.Set("a", tt.object)103 result := evaluate("!!a", env, t)104 switch tt.expected {105 case "true":106 if result != TRUE {107 t.Errorf("%s does not evaluate to true", tt.description)108 }109 case "false":110 if result != FALSE {111 t.Errorf("%s does not evaluate to false", tt.description)112 }113 case "error":114 if result.Type() != "ERROR" {115 t.Errorf("%s does not evaluate to an error", tt.description)116 }117 default:118 t.Errorf("test setup fails, since expectation %q not yet implemented", tt.expected)119 }120 }121}122// needs specification123func setupEvalToBoolean() (*object.Environment, []struct {124 object object.Object125 description string126 expected string127}) {128 // prep: create environment and build the function object (fn(){})129 env := object.NewEnvironment()130 params := []*ast.Identifier{}131 body := &ast.BlockStatement{132 Token: token.Token{Type: token.LBRACE, Literal: "{"}}133 body.Statements = []ast.Statement{}134 functionObj := &object.Function{Parameters: params, Env: env, Body: body}135 // create testdata136 tests := []struct {137 object object.Object138 description string139 expected string //TODO: better interface{} - but then we need to specify the errormessages!140 }{141 {TRUE, "Boolean with value true", "true"},142 {FALSE, "Boolean with value false", "false"},143 {&object.Integer{Value: -1},144 "Integer with negative value", "error"},145 {&object.Integer{Value: 0},146 "Integer with zero value", "error"},147 {&object.Integer{Value: 1},148 "Integer with positive value", "error"},149 {NULL, "Null", "error"},150 {&object.Error{Message: ""}, "Error", "error"},151 {functionObj, "Function", "error"},152 {nil, "nil", "true"},153 }154 return env, tests155}156func evaluate(input string, env *object.Environment, t *testing.T) object.Object {157 l := lexer.New(input)158 p := parser.New(l)159 ast := p.ParseProgram()160 return Eval(ast, env)161}...

Full Screen

Full Screen

parser_test.go

Source:parser_test.go Github

copy

Full Screen

1package parser2import (3 "fmt"4 "github.com/antlr/antlr4/runtime/Go/antlr"5 "gopkg.in/yaml.v2"6 "io/ioutil"7 "testing"8)9type ParserTestInputData struct {10 Cases []ParseTestInputCase `yaml:"cases"`11}12type ParseTestInputCase struct {13 Name string `yaml:"name"`14 Input string `yaml:"quark"`15 Expect string `yaml:"expect"`16}17type TestErrorListener struct {18 errorCount int19}20func (t *TestErrorListener) SyntaxError(recognizer antlr.Recognizer, offendingSymbol interface{}, line, column int, msg string, e antlr.RecognitionException) {21 fmt.Printf("syntax error line %d:%d\n", line, column)22 t.errorCount += 123}24func (t *TestErrorListener) ReportAmbiguity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, exact bool, ambigAlts *antlr.BitSet, configs antlr.ATNConfigSet) {25 println("ambiguity error")26 t.errorCount += 127}28func (t *TestErrorListener) ReportAttemptingFullContext(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex int, conflictingAlts *antlr.BitSet, configs antlr.ATNConfigSet) {29}30func (t *TestErrorListener) ReportContextSensitivity(recognizer antlr.Parser, dfa *antlr.DFA, startIndex, stopIndex, prediction int, configs antlr.ATNConfigSet) {31}32func testCheckError(err error, t *testing.T) {33 if err != nil {34 t.Errorf("error %e\n", err)35 }36}37func TestParsePackage(t *testing.T) {38 testData, err := ioutil.ReadFile("parser_test_data.yml")39 testCheckError(err, t)40 inputData := ParserTestInputData{}41 err = yaml.Unmarshal(testData, &inputData)42 testCheckError(err, t)43 for _, testCase := range inputData.Cases {44 testname := testCase.Name45 t.Run(testname, func(t *testing.T) {46 defer func() {47 if r := recover(); r != nil && testCase.Expect == "valid" {48 fmt.Println(r)49 t.Errorf("expected no errors but got parser panic")50 }51 }()52 parser := NewStringParser(testCase.Input)53 testErrors := TestErrorListener{}54 parser.AddErrorListener(&testErrors)55 parser.GetPackageAST()56 if testCase.Expect == "valid" && testErrors.errorCount > 0 {57 t.Errorf("expected no errors but got %d errors", testErrors.errorCount)58 } else if testCase.Expect == "invalid" && testErrors.errorCount == 0 {59 t.Errorf("expected parser error but got 0 errors")60 }61 })62 }63}...

Full Screen

Full Screen

TestErrors

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Print(fset, &ast.CompositeLit{Type: f})8}9import (10func main() {11 f, err := parser.ParseFile(fset, "3.go", nil, parser.ParseComments)12 if err != nil {13 fmt.Println(err)

Full Screen

Full Screen

TestErrors

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := parser.ParseFile(fset, "2.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(f.Name)8 if f.Comments != nil {9 for _, comment := range f.Comments {10 fmt.Println(comment.Text())11 }12 }13 if f.Decls != nil {14 for _, decl := range f.Decls {15 switch d := decl.(type) {16 for _, spec := range d.Specs {17 switch s := spec.(type) {18 fmt.Println(s.Path.Value)19 }20 }21 }22 }23 }24}

Full Screen

Full Screen

TestErrors

Using AI Code Generation

copy

Full Screen

1import (2func main() {3import "fmt"4func main() {5 fmt.Println("Hello, playground")6}7 fset := token.NewFileSet()8 f, err := parser.ParseFile(fset, "", src, 0)9 if err != nil {10 panic(err)11 }12 fmt.Println(f.Name.Name)13 for _, s := range f.Imports {14 fmt.Println(s.Path.Value)15 }16 for _, d := range f.Decls {17 f, ok := d.(*ast.FuncDecl)18 if !ok {19 }20 fmt.Println(f.Name.Name)21 }22}23import (24func main() {25import "fmt"26func main() {27 fmt.Println("Hello, playground")28}29 fset := token.NewFileSet()30 f, err := parser.ParseFile(fset, "", src, 0)31 if err != nil {32 panic(err)33 }34 fmt.Println(f.Name.Name)35 for _, s := range f.Imports {36 fmt.Println(s.Path.Value)37 }38 for _, d := range f.Decls {39 f, ok := d.(*ast.FuncDecl)40 if !ok {41 }42 fmt.Println(f.Name.Name)43 }44}45import (46func main() {47import "fmt"48func main() {49 fmt.Println("Hello, playground")50}51 fset := token.NewFileSet()52 f, err := parser.ParseFile(fset, "", src, 0)53 if err != nil {54 panic(err)55 }56 fmt.Println(f.Name.Name)57 for _, s := range f.Imports {58 fmt.Println(s.Path.Value)59 }60 for _, d := range f.Decls {61 f, ok := d.(*ast.FuncDecl)62 if !ok {

Full Screen

Full Screen

TestErrors

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 func main() {4 fmt.Println("Hello, World!")5 }`6 f, err := parser.ParseFile(fset, "hello.go", src, 0)7 if err != nil {8 fmt.Println(err)9 }10 ast.Print(fset, f)11 err = ast.Print(fset, f)12 if err != nil {13 fmt.Println(err)14 }15 err = ast.Print(nil, f)16 if err != nil {17 fmt.Println(err)18 }19}20func main() {21 fmt.Println("Hello, World!")22}23func main() {24 fmt.Println("Hello, World!")25}26func main() {27 fmt.Println("Hello, World!")28}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful