-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_test.go
More file actions
45 lines (38 loc) · 1.04 KB
/
eval_test.go
File metadata and controls
45 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"bufio"
"strings"
"testing"
)
func TestEval(t *testing.T) {
expectations := []TestExpectation{
{"nil", "nil"},
{"()", "nil"},
// If
{"(if 10 20 30)", "20"}, // if true
{"(if nil 20 30)", "30"}, // if false
{"(if (if 10 nil 20) 400 500)", "500"}, // cond expr return nil
{"(if (if 10 20 20) 400 500)", "400"}, // cond expr return true
// Quote
{"'(if 10 20 30)", "(if 10 20 30)"},
// Lambda
{"((lambda (a b c) (+ a b c)) 1 2 3)", "6"},
// Let
{"(let (a 10 b 20) (+ a b))", "30"},
// Hiding variable
{"((if 1 (lambda (a) (+ a 2)) (lambda (a) (+ a 100))) 100)", "102"},
}
for _, exp := range expectations {
reader := bufio.NewReader(strings.NewReader(exp.arg))
lispObj, _ := Read(reader)
result, err := Eval(lispObj, GlobalEnv())
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
actual := LispObject2String(result)
if actual != exp.expected {
t.Errorf("Test Eval: Expected (eval %s) => %s, but got %s",
exp.arg, exp.expected, actual)
}
}
}