-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_test.go
48 lines (40 loc) · 1.22 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
package gocalc
import "testing"
type mockExprVisitor struct {
visited []expr
}
func (m *mockExprVisitor) add(e expr) {
m.visited = append(m.visited, e)
}
func (m *mockExprVisitor) visitBinaryExpr(b *binaryExpr) { m.add(b) }
func (m *mockExprVisitor) visitFuncExpr(f *funcExpr) { m.add(f) }
func (m *mockExprVisitor) visitParamExpr(p *paramExpr) { m.add(p) }
func (m *mockExprVisitor) visitUnaryExpr(u *unaryExpr) { m.add(u) }
func (m *mockExprVisitor) visitBoolExpr(b *boolExpr) { m.add(b) }
func (m *mockExprVisitor) visitFloatExpr(f *floatExpr) { m.add(f) }
func (m *mockExprVisitor) visitIntExpr(i *intExpr) { m.add(i) }
func newMockExprVisitor() *mockExprVisitor {
return &mockExprVisitor{}
}
var acceptExprs = []expr{
&binaryExpr{},
&funcExpr{},
¶mExpr{},
&unaryExpr{},
&boolExpr{},
&floatExpr{},
&intExpr{},
}
func TestAccepts(t *testing.T) {
for _, accepter := range acceptExprs {
m := newMockExprVisitor()
accepter.accept(m)
if len(m.visited) > 1 {
t.Errorf("More than one expr visited: %#v", m.visited)
} else if len(m.visited) < 1 {
t.Error("No expr visited")
} else if visited := m.visited[0]; visited != accepter {
t.Errorf("Wrong expr was visited: %T", visited)
}
}
}