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
/
specials_test.go
132 lines (117 loc) · 2.33 KB
/
specials_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
package sabre_test
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/spy16/sabre"
)
const src = `
(def temp (let* [pi 3.1412]
pi))
(def hello (fn* hello
([arg] arg)
([arg & rest] rest)))
`
func TestSpecials(t *testing.T) {
scope := sabre.New()
expected := sabre.MultiFn{
Name: "hello",
IsMacro: false,
Methods: []sabre.Fn{
{
Args: []string{"arg", "rest"},
Variadic: true,
Body: sabre.Module{
sabre.Symbol{Value: "rest"},
},
},
},
}
res, err := sabre.ReadEvalStr(scope, src)
if err != nil {
t.Errorf("Eval() unexpected error: %v", err)
}
if reflect.DeepEqual(res, expected) {
t.Errorf("Eval() expected=%v, got=%v", expected, res)
}
}
func TestDot(t *testing.T) {
t.Parallel()
table := []struct {
name string
src string
want sabre.Value
wantErr bool
}{
{
name: "StringFieldAccess",
src: "foo.Name",
want: sabre.String("Bob"),
},
{
name: "BoolFieldAccess",
src: "foo.Enabled",
want: sabre.Bool(false),
},
{
name: "MethodAccess",
src: `(foo.Bar "Baz")`,
want: sabre.String("Bar(\"Baz\")"),
},
{
name: "MethodAccessPtr",
src: `(foo.BarPtr "Bob")`,
want: sabre.String("BarPtr(\"Bob\")"),
},
{
name: "EvalFailed",
src: `blah.BarPtr`,
want: nil,
wantErr: true,
},
{
name: "NonExistentMember",
src: `foo.Baz`,
want: nil,
wantErr: true,
},
{
name: "PrivateMember",
src: `foo.privateMember`,
want: nil,
wantErr: true,
},
}
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) {
scope := sabre.New()
scope.BindGo("foo", &Foo{
Name: "Bob",
})
form, err := sabre.NewReader(strings.NewReader(tt.src)).All()
if err != nil {
t.Fatalf("failed to read source='%s': %+v", tt.src, err)
}
got, err := sabre.Eval(scope, form)
if (err != nil) != tt.wantErr {
t.Errorf("Eval() unexpected error: %+v", err)
}
if !reflect.DeepEqual(tt.want, got) {
t.Errorf("Eval() want=%#v, got=%#v", tt.want, got)
}
})
}
}
// Foo is a dummy type for member access tests.
type Foo struct {
Name string
Enabled bool
privateMember bool
}
func (foo *Foo) BarPtr(arg string) string {
return fmt.Sprintf("BarPtr(\"%s\")", arg)
}
func (foo Foo) Bar(arg string) string {
return fmt.Sprintf("Bar(\"%s\")", arg)
}