This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsabre_test.go
169 lines (147 loc) · 3 KB
/
sabre_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package sabre_test
import (
"reflect"
"testing"
"github.com/spy16/sabre"
)
const sampleProgram = `
(def v [1 2 3])
(def pi 3.1412)
(def echo (fn* [arg] arg))
(echo pi)
(def int-num 10)
(def float-num 10.1234)
(def list '(nil 1 []))
(def vector ["hello" nil])
(def set #{1 2 3})
(def empty-set #{})
(def complex-calc (let* [sample '(1 2 3 4 [])]
(sample.First)))
(assert (= int-num 10)
(= float-num 10.1234)
(= pi 3.1412)
(= list '(nil 1 []))
(= vector ["hello" nil])
(= empty-set #{})
(= echo (fn* [arg] arg))
(= complex-calc 1))
(echo pi)
`
func BenchmarkEval(b *testing.B) {
scope := sabre.NewScope(nil)
_ = scope.BindGo("inc", func(a int) int {
return a + 1
})
f := &sabre.List{
Values: sabre.Values{
sabre.Symbol{Value: "inc"},
sabre.Int64(10),
},
}
for i := 0; i < b.N; i++ {
_, _ = sabre.Eval(scope, f)
}
}
func BenchmarkGoCall(b *testing.B) {
inc := func(a int) int {
return a + 1
}
for i := 0; i < b.N; i++ {
_ = inc(10)
}
}
func TestEval(t *testing.T) {
t.Parallel()
table := []struct {
name string
src string
getScope func() sabre.Scope
want sabre.Value
wantErr bool
}{
{
name: "Empty",
src: "",
want: sabre.Nil{},
},
{
name: "SingleForm",
src: "123",
want: sabre.Int64(123),
},
{
name: "MultiForm",
src: `123 [] ()`,
want: &sabre.List{
Values: sabre.Values(nil),
Position: sabre.Position{File: "<string>", Line: 1, Column: 8},
},
},
{
name: "WithFunctionCalls",
getScope: func() sabre.Scope {
scope := sabre.NewScope(nil)
_ = scope.BindGo("ten?", func(i sabre.Int64) bool {
return i == 10
})
return scope
},
src: `(ten? 10)`,
want: sabre.Bool(true),
},
{
name: "ReadError",
src: `123 [] (`,
want: nil,
wantErr: true,
},
{
name: "Program",
src: sampleProgram,
want: sabre.Float64(3.1412),
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
scope := sabre.Scope(sabre.New())
if tt.getScope != nil {
scope = tt.getScope()
}
scope.Bind("=", sabre.ValueOf(sabre.Compare))
scope.Bind("assert", &sabre.Fn{Func: asserter(t)})
got, err := sabre.ReadEvalStr(scope, tt.src)
if (err != nil) != tt.wantErr {
t.Errorf("Eval() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Eval() got = %#v, want %#v", got, tt.want)
}
})
}
}
func asserter(t *testing.T) func(sabre.Scope, []sabre.Value) (sabre.Value, error) {
return func(scope sabre.Scope, exprs []sabre.Value) (sabre.Value, error) {
var res sabre.Value
var err error
for _, expr := range exprs {
res, err = expr.Eval(scope)
if err != nil {
t.Errorf("%s: %s", expr, err)
}
if !isTruthy(res) {
t.Errorf("assertion failed: %s (result=%v)", expr, res)
}
}
return res, err
}
}
func isTruthy(v sabre.Value) bool {
if v == nil || v == (sabre.Nil{}) {
return false
}
if b, ok := v.(sabre.Bool); ok {
return bool(b)
}
return true
}