forked from erh/scheme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_test.go
99 lines (85 loc) · 1.77 KB
/
ast_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package scheme
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAST0(t *testing.T) {
x, err := Parse("1")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1.0, *x.Val.Float)
x, err = Parse("1.1")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1.1, *x.Val.Float)
x, err = Parse("-1.1")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, -1.1, *x.Val.Float)
x, err = Parse("-1")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, -1.0, *x.Val.Float)
x, err = Parse("\"a\"")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "a", *x.Val.AString)
x, err = Parse("a")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "a", *x.Var)
x, err = Parse("+")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "+", *x.Symbol)
x, err = Parse("*")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "*", *x.Symbol)
}
func TestAST1(t *testing.T) {
x, err := Parse("(foo bar)")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 2, len(x.Call))
assert.Equal(t, "foo", *x.Call[0].Var)
}
func TestAST2(t *testing.T) {
x, err := Parse("(foo 5 3.3)")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 3, len(x.Call))
assert.Equal(t, "foo", *x.Call[0].Var)
assert.Equal(t, 5.0, *x.Call[1].Val.Float)
assert.Equal(t, 3.3, *x.Call[2].Val.Float)
}
func TestAST3(t *testing.T) {
x, err := Parse("(+ 5 3.3)")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 3, len(x.Call))
assert.Equal(t, "+", *x.Call[0].Symbol)
assert.Equal(t, 5.0, *x.Call[1].Val.Float)
assert.Equal(t, 3.3, *x.Call[2].Val.Float)
}
func TestAST4(t *testing.T) {
x, err := Parse("(+ 5 (+ 1 4))")
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 3, len(x.Call))
assert.Equal(t, "+", *x.Call[0].Symbol)
assert.Equal(t, 5.0, *x.Call[1].Val.Float)
assert.Equal(t, "+", *x.Call[2].Call[0].Symbol)
}