-
Notifications
You must be signed in to change notification settings - Fork 105
/
receiver_test.go
176 lines (151 loc) · 3.25 KB
/
receiver_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
170
171
172
173
174
175
176
// Copyright 2017 Alexander Palaistras. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package php
import (
"bytes"
"testing"
)
func TestReceiverStart(t *testing.T) {
e, _ = New()
t.SkipNow()
}
type testReceiver struct {
Var string
hidden int64
}
func (t *testReceiver) Ignore() {
}
func (t *testReceiver) Hello(p string) string {
return "Hello " + p
}
func (t *testReceiver) Goodbye(p string) (string, string) {
return "Goodbye", p
}
func (t *testReceiver) invalid() string {
return "I'm afraid I can't let you do that, Dave"
}
func newTestReceiver(args []interface{}) interface{} {
value := "Foo"
if len(args) > 0 {
switch v := args[0].(type) {
case bool:
return nil
case string:
value = v
}
}
return &testReceiver{Var: value, hidden: 42}
}
var receiverDefineTests = []struct {
script string
expected string
}{
{
"$t = new TestReceiver; echo is_object($t);",
"1",
},
{
`try {
$t = new TestReceiver(false);
} catch (Exception $e) {
echo $e->getMessage();
}`,
"Failed to instantiate method receiver",
},
{
"$t = new TestReceiver; echo $t->Var;",
"Foo",
},
{
"$t = new TestReceiver; echo $t->hidden;",
"",
},
{
"$t = new TestReceiver('wow'); echo $t->Var;",
"wow",
},
{
"$t = new TestReceiver; $t->Var = 'Bar'; echo $t->Var;",
"Bar",
},
{
"$t = new TestReceiver; $t->hello = 'wow'; echo $t->hello;",
"",
},
{
"$t = new TestReceiver; echo $t->Ignore();",
"",
},
{
"$t = new TestReceiver; echo $t->Hello('World');",
"Hello World",
},
{
"$t = new TestReceiver; echo json_encode($t->Goodbye('Doge'));",
`["Goodbye","Doge"]`,
},
{
"$t = new TestReceiver; echo $t->invalid();",
"",
},
{
"$t = new TestReceiver; echo ($t->Var) ? 1 : 0;",
"1",
},
{
"$t = new TestReceiver; echo isset($t->Var) ? 1 : 0;",
"1",
},
{
"$t = new TestReceiver; echo empty($t->Var) ? 1 : 0;",
"0",
},
{
"$t = new TestReceiver; echo isset($t->hidden) ? 1 : 0;",
"0",
},
}
func TestReceiverDefine(t *testing.T) {
var w bytes.Buffer
c, _ := e.NewContext()
c.Output = &w
if err := e.Define("TestReceiver", newTestReceiver); err != nil {
t.Fatalf("Engine.Define(): Failed to define method receiver: %s", err)
}
// Attempting to define a receiver twice should fail.
if err := e.Define("TestReceiver", newTestReceiver); err == nil {
t.Fatalf("Engine.Define(): Defining duplicate receiver should fail")
}
for _, tt := range receiverDefineTests {
_, err := c.Eval(tt.script)
if err != nil {
t.Errorf("Context.Eval('%s'): %s", tt.script, err)
continue
}
actual := w.String()
w.Reset()
if actual != tt.expected {
t.Errorf("Context.Eval('%s'): Expected output '%s', actual '%s'", tt.script, tt.expected, actual)
}
}
c.Destroy()
}
func TestReceiverDestroy(t *testing.T) {
c, _ := e.NewContext()
defer c.Destroy()
r := e.receivers["TestReceiver"]
if r == nil {
t.Fatalf("Receiver.Destroy(): Could not find defined receiver")
}
r.Destroy()
if r.create != nil || r.objects != nil {
t.Errorf("Receiver.Destroy(): Did not set internal fields to `nil`")
}
// Attempting to destroy a receiver twice should be a no-op.
r.Destroy()
}
func TestReceiverEnd(t *testing.T) {
e.Destroy()
t.SkipNow()
}