@ 202aec8d6f143ace005fbbe41cc33c7a6344995e | history
package eval
import (
"errors"
"testing"
"government/sexpr"
)
// testEnv binds a few names. "boom" is deliberately unbound: evaluating it
// fails, which shows whether an expression was evaluated at all.
func testEnv() *Env {
return Builtins().
Extend("x", sexpr.Int(1)).
Extend("s", sexpr.String("hi")).
Extend("no", sexpr.F).
Extend("empty", sexpr.Nil)
}
// evalString evaluates every top-level form in src, in order, and returns
// the last value or the first error.
func evalString(t *testing.T, src string) (sexpr.Value, error) {
t.Helper()
forms, err := sexpr.ReadAll(src)
if err != nil {
t.Fatalf("ReadAll(%q): %v", src, err)
}
if len(forms) == 0 {
t.Fatalf("ReadAll(%q) returned no forms", src)
}
top := NewTopLevelIn(testEnv())
var v sexpr.Value
for _, f := range forms {
if v, err = top.Eval(f); err != nil {
return nil, err
}
}
return v, nil
}
type evalCase struct{ src, want string }
type errorCase struct{ src, code, msg string }
func runEvalCases(t *testing.T, tests []evalCase) {
t.Helper()
for _, tt := range tests {
t.Run(tt.src, func(t *testing.T) {
v, err := evalString(t, tt.src)
if err != nil {
t.Fatalf("Eval(%s): %v", tt.src, err)
}
if got := sexpr.Print(v); got != tt.want {
t.Errorf("Eval(%s) = %s, want %s", tt.src, got, tt.want)
}
})
}
}
func runErrorCases(t *testing.T, tests []errorCase) {
t.Helper()
for _, tt := range tests {
t.Run(tt.src, func(t *testing.T) {
_, err := evalString(t, tt.src)
var e *Error
if !errors.As(err, &e) {
t.Fatalf("Eval(%s) error = %v, want *Error", tt.src, err)
}
if e.Code != tt.code || e.Msg != tt.msg {
t.Errorf("Eval(%s) error = %s: %s, want %s: %s", tt.src, e.Code, e.Msg, tt.code, tt.msg)
}
})
}
}
func TestEval(t *testing.T) {
runEvalCases(t, []evalCase{
// Atoms evaluate to themselves.
{"42", "42"},
{"-1.5", "-1.5"},
{`"hello"`, `"hello"`},
{"T", "T"},
{"F", "F"},
{"()", "()"},
{"x", "1"},
{"s", `"hi"`},
{"'boom", "boom"},
{"(quote boom)", "boom"},
{"'(boom 1 2)", "(boom 1 2)"},
{"'(1 . 2)", "(1 . 2)"},
{"''x", "(quote x)"},
{"'()", "()"},
{"'T", "T"},
// Only F is false.
{"(if T 1 2)", "1"},
{"(if F 1 2)", "2"},
{"(if 0 1 2)", "1"},
{"(if 0.0 1 2)", "1"},
{`(if "" 1 2)`, "1"},
{"(if () 1 2)", "1"},
{"(if 'sym 1 2)", "1"},
{"(if no 1 2)", "2"},
{"(if empty 1 2)", "1"},
{"(if x 'yes 'no)", "yes"},
{"(if T 1)", "1"},
{"(if F 1)", "()"},
// The branch not taken is never evaluated.
{"(if T 1 boom)", "1"},
{"(if F boom 2)", "2"},
{"(if F boom)", "()"},
{"(if (if F T F) 1 2)", "2"},
{"(if T (if F 1 2) 3)", "2"},
{"(if T 'x)", "x"},
})
}
func TestEvalErrors(t *testing.T) {
runErrorCases(t, []errorCase{
{"boom", ErrUnbound, "no statute for symbol boom"},
{"(if boom 1 2)", ErrUnbound, "no statute for symbol boom"},
{"(if T boom 2)", ErrUnbound, "no statute for symbol boom"},
{"(quote)", ErrSyntax, "quote takes 1 operand, got 0"},
{"(quote a b)", ErrSyntax, "quote takes 1 operand, got 2"},
{"(if)", ErrSyntax, "if takes 2 or 3 operands, got 0"},
{"(if T)", ErrSyntax, "if takes 2 or 3 operands, got 1"},
{"(if T 1 2 3)", ErrSyntax, "if takes 2 or 3 operands, got 4"},
{"(if T . 1)", ErrSyntax, "cannot evaluate improper list (if T . 1)"},
// Special forms are syntax, not values.
{"if", ErrSpecialForm, "if is a special form, not a value"},
{"quote", ErrSpecialForm, "quote is a special form, not a value"},
{"(if T if)", ErrSpecialForm, "if is a special form, not a value"},
{"(1 2)", ErrNotAFunction, "1 is not a function"},
{"(x)", ErrNotAFunction, "1 is not a function"},
{`("f" 1)`, ErrNotAFunction, `"f" is not a function`},
{"(boom 1)", ErrUnbound, "no statute for symbol boom"},
{"((if T 1 2) 3)", ErrNotAFunction, "1 is not a function"},
})
}