-
Notifications
You must be signed in to change notification settings - Fork 7
/
env_test.go
61 lines (54 loc) · 1.34 KB
/
env_test.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package goscheme
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestEnv_Find(t *testing.T) {
env := &Env{
outer: nil,
frame: map[Symbol]Expression{"x": 1, "y": 1},
}
ret, err := env.Find("x")
assert.Equal(t, 1, ret)
assert.Nil(t, err)
// nested env
env2 := &Env{
outer: env,
frame: map[Symbol]Expression{"x": 2},
}
ret, err = env2.Find("x")
assert.Equal(t, 2, ret)
assert.Nil(t, err)
ret, err = env2.Find("y")
assert.Equal(t, 1, ret)
assert.Nil(t, err)
_, err = env2.Find("unknown")
assert.NotNil(t, err)
}
func Test_listImpl(t *testing.T) {
testCases := []struct {
input []Expression
expected *Pair
}{
{[]Expression{1, 2, 3}, &Pair{1, &Pair{2, &Pair{3, NilObj}}}},
{[]Expression{1, &Pair{Car: 2}, 3}, &Pair{1, &Pair{&Pair{Car: 2}, &Pair{3, NilObj}}}},
}
for _, c := range testCases {
p, _ := listImpl(c.input...)
assert.Equal(t, c.expected, p)
}
}
func Test_appendImpl(t *testing.T) {
testCases := []struct {
input []Expression
expected *Pair
}{
{[]Expression{&Pair{1, NilObj}, 2}, &Pair{1, &Pair{2, NilObj}}},
{[]Expression{&Pair{1, NilObj}, &Pair{2, NilObj}}, &Pair{1, &Pair{2, NilObj}}},
{[]Expression{&Pair{1, NilObj}, &Pair{2, NilObj}, 3}, &Pair{1, &Pair{2, &Pair{3, NilObj}}}},
}
for _, c := range testCases {
l, _ := appendImpl(c.input...)
assert.Equal(t, c.expected, l)
}
}