Skip to content

Commit 9f9aafe

Browse files
committed
Add more tests
1 parent f8c5ef1 commit 9f9aafe

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

eval.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,22 @@ type evaluator interface {
1111
}
1212

1313
// Eval parses and evaluates given input.
14-
func Eval(input string, env interface{}) (node interface{}, err error) {
15-
defer func() {
16-
if r := recover(); r != nil {
17-
err = fmt.Errorf("%v", r)
18-
}
19-
}()
20-
21-
node, err = Parse(input)
22-
14+
func Eval(input string, env interface{}) (interface{}, error) {
15+
node, err := Parse(input)
2316
if err != nil {
2417
return nil, err
2518
}
2619
return Run(node, env)
2720
}
2821

2922
// Run evaluates given ast.
30-
func Run(node Node, env interface{}) (interface{}, error) {
23+
func Run(node Node, env interface{}) (out interface{}, err error) {
24+
defer func() {
25+
if r := recover(); r != nil {
26+
err = fmt.Errorf("%v", r)
27+
}
28+
}()
29+
3130
if e, ok := node.(evaluator); ok {
3231
return e.eval(env)
3332
}

eval_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,20 @@ func TestEvalComplex(t *testing.T) {
429429
t.Fatalf("TestEvalComplex:\ngot\n\t%#v\nexpected:\n\t%#v", actual, expected)
430430
}
431431
}
432+
433+
func TestEvalRunPanic(t *testing.T) {
434+
node, err := Parse("foo()")
435+
if err != nil {
436+
t.Fatal(err)
437+
}
438+
439+
_, err = Run(node, map[string]interface{}{"foo": nil})
440+
if err == nil {
441+
err = fmt.Errorf("<nil>")
442+
}
443+
444+
expected := "reflect: call of reflect.Value.Call on zero Value"
445+
if err.Error() != expected {
446+
t.Errorf("\ngot\n\t%+v\nexpected\n\t%v", err.Error(), expected)
447+
}
448+
}

parser_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@ func TestParseError(t *testing.T) {
179179
}
180180
}
181181
}
182+
183+
func TestParseErrorPosition(t *testing.T) {
184+
_, err := Parse("foo() + bar(**)")
185+
if err == nil {
186+
err = fmt.Errorf("<nil>")
187+
}
188+
189+
expected := "unexpected token operator(**)\nfoo() + bar(**)\n------------^"
190+
if err.Error() != expected {
191+
t.Errorf("\ngot\n\t%+v\nexpected\n\t%v", err.Error(), expected)
192+
}
193+
}

0 commit comments

Comments
 (0)