-
Notifications
You must be signed in to change notification settings - Fork 65
/
muon_test.go
138 lines (105 loc) · 2.33 KB
/
muon_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
package muon
import (
"net/http"
"os"
"reflect"
"testing"
)
var w *Window
func TestMain(m *testing.M) {
cfg := &Config{
Height: 1,
Width: 1,
}
w = New(cfg, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
go func() {
w.Start()
}()
os.Exit(m.Run())
}
type testObject struct {
S1 string
F1 float64
B1 bool
}
func TestComplexType(t *testing.T) {
w.Bind("complexTest", func(to *testObject) *testObject {
return &testObject{
S1: to.S1 + " World!",
F1: to.F1 + 1,
B1: !to.B1,
}
})
res, err := w.Eval(`complexTest({S1: "Hello,", F1: 9000, B1: false})`, reflect.TypeOf(&testObject{}))
if err != nil {
t.Error(err)
}
to := res.(*testObject)
if to.S1 != "Hello, World!" {
t.Errorf("to.S1 was not correct, got %s", to.S1)
}
if to.F1 != 9001 {
t.Errorf("to.F1 was under 9000, got %f", to.F1)
}
if !to.B1 {
t.Errorf("to.B1 was not True, got false")
}
}
func t2(to *testObject) *testObject {
return &testObject{
S1: to.S1 + " World!",
F1: to.F1 + 1,
B1: !to.B1,
}
}
func TestArrayType(t *testing.T) {
w.Bind("arrayTest", func(strings []string) []float64 {
if strings[0] != "Hello" {
t.Errorf("strings[0] was not Hello, got %s", strings[0])
}
if strings[1] != "World!" {
t.Errorf("strings[1] was not World!, got %s", strings[1])
}
return []float64{1, 2, 3}
})
res, err := w.Eval(`arrayTest(["Hello","World!"])`, reflect.TypeOf([]float64{}))
if err != nil {
t.Error(err)
}
nums := res.([]float64)
if nums[0] != 1 {
t.Errorf("nums[0] was not 1, got %f", nums[0])
}
if nums[1] != 2 {
t.Errorf("nums[1] was not 2, got %f", nums[1])
}
if nums[2] != 3 {
t.Errorf("nums[2] was not 3, got %f", nums[2])
}
}
func TestEmptyType(t *testing.T) {
w.Bind("emptyTypeTest", func(nullValue string, undefinedValue string) {
if nullValue != "" {
t.Errorf("nullType was not empty!")
}
if undefinedValue != "" {
t.Errorf("undefinedType was not empty!")
}
})
_, err := w.Eval(`emptyTypeTest(null, undefined)`, nil)
if err != nil {
t.Error(err)
}
}
func TestMultipleFuncs(t *testing.T) {
w.Bind("multiple1Test", func(value1 string) {})
w.Bind("multiple2Test", func(value2 bool) {})
_, err := w.Eval(`multiple1Test("Hello, World1")`, nil)
if err != nil {
t.Error(err)
}
_, err = w.Eval(`multiple2Test(true)`, nil)
if err != nil {
t.Error(err)
}
}