Skip to content

Commit ab3f06d

Browse files
committed
add tests
Signed-off-by: Robert Landers <landers.robert@gmail.com>
1 parent 4236218 commit ab3f06d

1 file changed

Lines changed: 265 additions & 0 deletions

File tree

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
package extgen
2+
3+
import (
4+
"github.com/stretchr/testify/require"
5+
"os"
6+
"path/filepath"
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestModuleParser(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
input string
16+
expected *phpModule
17+
}{
18+
{
19+
name: "both init and shutdown",
20+
input: `package main
21+
22+
//export_php:module init=initializeModule, shutdown=cleanupModule
23+
func initializeModule() {
24+
// Initialization code
25+
}
26+
27+
func cleanupModule() {
28+
// Cleanup code
29+
}`,
30+
expected: &phpModule{
31+
InitFunc: "initializeModule",
32+
ShutdownFunc: "cleanupModule",
33+
},
34+
},
35+
{
36+
name: "only init function",
37+
input: `package main
38+
39+
//export_php:module init=initializeModule
40+
func initializeModule() {
41+
// Initialization code
42+
}`,
43+
expected: &phpModule{
44+
InitFunc: "initializeModule",
45+
ShutdownFunc: "",
46+
},
47+
},
48+
{
49+
name: "only shutdown function",
50+
input: `package main
51+
52+
//export_php:module shutdown=cleanupModule
53+
func cleanupModule() {
54+
// Cleanup code
55+
}`,
56+
expected: &phpModule{
57+
InitFunc: "",
58+
ShutdownFunc: "cleanupModule",
59+
},
60+
},
61+
{
62+
name: "with extra whitespace",
63+
input: `package main
64+
65+
//export_php:module init = initModule , shutdown = shutdownModule
66+
func initModule() {
67+
// Initialization code
68+
}
69+
70+
func shutdownModule() {
71+
// Cleanup code
72+
}`,
73+
expected: &phpModule{
74+
InitFunc: "initModule",
75+
ShutdownFunc: "shutdownModule",
76+
},
77+
},
78+
{
79+
name: "no module directive",
80+
input: `package main
81+
82+
func regularFunction() {
83+
// Just a regular Go function
84+
}`,
85+
expected: nil,
86+
},
87+
{
88+
name: "empty module directive",
89+
input: `package main
90+
91+
//export_php:module
92+
func someFunction() {
93+
// Some code
94+
}`,
95+
expected: &phpModule{
96+
InitFunc: "",
97+
ShutdownFunc: "",
98+
},
99+
},
100+
{
101+
name: "invalid module directive format",
102+
input: `package main
103+
104+
//export_php:module init:initFunc, shutdown:shutdownFunc
105+
func initFunc() {
106+
// Initialization code
107+
}
108+
109+
func shutdownFunc() {
110+
// Cleanup code
111+
}`,
112+
expected: &phpModule{
113+
InitFunc: "",
114+
ShutdownFunc: "",
115+
},
116+
},
117+
{
118+
name: "unrecognized keys",
119+
input: `package main
120+
121+
//export_php:module start=startFunc, stop=stopFunc
122+
func startFunc() {
123+
// Start code
124+
}
125+
126+
func stopFunc() {
127+
// Stop code
128+
}`,
129+
expected: &phpModule{
130+
InitFunc: "",
131+
ShutdownFunc: "",
132+
},
133+
},
134+
}
135+
136+
for _, tt := range tests {
137+
t.Run(tt.name, func(t *testing.T) {
138+
tmpDir := t.TempDir()
139+
fileName := filepath.Join(tmpDir, tt.name+".go")
140+
require.NoError(t, os.WriteFile(fileName, []byte(tt.input), 0644))
141+
142+
parser := &ModuleParser{}
143+
module, err := parser.parse(fileName)
144+
require.NoError(t, err)
145+
146+
if tt.expected == nil {
147+
assert.Nil(t, module, "parse() should return nil for no module directive")
148+
} else {
149+
assert.NotNil(t, module, "parse() should not return nil")
150+
assert.Equal(t, tt.expected.InitFunc, module.InitFunc, "InitFunc mismatch")
151+
assert.Equal(t, tt.expected.ShutdownFunc, module.ShutdownFunc, "ShutdownFunc mismatch")
152+
}
153+
})
154+
}
155+
}
156+
157+
func TestParseModuleInfo(t *testing.T) {
158+
tests := []struct {
159+
name string
160+
info string
161+
expected *phpModule
162+
}{
163+
{
164+
name: "both init and shutdown",
165+
info: "init=initializeModule, shutdown=cleanupModule",
166+
expected: &phpModule{
167+
InitFunc: "initializeModule",
168+
ShutdownFunc: "cleanupModule",
169+
},
170+
},
171+
{
172+
name: "only init",
173+
info: "init=initializeModule",
174+
expected: &phpModule{
175+
InitFunc: "initializeModule",
176+
ShutdownFunc: "",
177+
},
178+
},
179+
{
180+
name: "only shutdown",
181+
info: "shutdown=cleanupModule",
182+
expected: &phpModule{
183+
InitFunc: "",
184+
ShutdownFunc: "cleanupModule",
185+
},
186+
},
187+
{
188+
name: "with extra whitespace",
189+
info: " init = initModule , shutdown = shutdownModule ",
190+
expected: &phpModule{
191+
InitFunc: "initModule",
192+
ShutdownFunc: "shutdownModule",
193+
},
194+
},
195+
{
196+
name: "empty string",
197+
info: "",
198+
expected: &phpModule{
199+
InitFunc: "",
200+
ShutdownFunc: "",
201+
},
202+
},
203+
{
204+
name: "invalid format - no equals sign",
205+
info: "init initFunc, shutdown shutdownFunc",
206+
expected: &phpModule{
207+
InitFunc: "",
208+
ShutdownFunc: "",
209+
},
210+
},
211+
{
212+
name: "invalid format - wrong separator",
213+
info: "init=initFunc; shutdown=shutdownFunc",
214+
expected: &phpModule{
215+
InitFunc: "initFunc",
216+
ShutdownFunc: "",
217+
},
218+
},
219+
{
220+
name: "unrecognized keys",
221+
info: "start=startFunc, stop=stopFunc",
222+
expected: &phpModule{
223+
InitFunc: "",
224+
ShutdownFunc: "",
225+
},
226+
},
227+
{
228+
name: "mixed valid and invalid",
229+
info: "init=initFunc, invalid, shutdown=shutdownFunc",
230+
expected: &phpModule{
231+
InitFunc: "initFunc",
232+
ShutdownFunc: "shutdownFunc",
233+
},
234+
},
235+
{
236+
name: "reversed order",
237+
info: "shutdown=shutdownFunc, init=initFunc",
238+
expected: &phpModule{
239+
InitFunc: "initFunc",
240+
ShutdownFunc: "shutdownFunc",
241+
},
242+
},
243+
}
244+
245+
parser := &ModuleParser{}
246+
for _, tt := range tests {
247+
t.Run(tt.name, func(t *testing.T) {
248+
module, err := parser.parseModuleInfo(tt.info)
249+
250+
assert.NoError(t, err, "parseModuleInfo() unexpected error")
251+
assert.NotNil(t, module, "parseModuleInfo() should not return nil")
252+
assert.Equal(t, tt.expected.InitFunc, module.InitFunc, "InitFunc mismatch")
253+
assert.Equal(t, tt.expected.ShutdownFunc, module.ShutdownFunc, "ShutdownFunc mismatch")
254+
})
255+
}
256+
}
257+
258+
func TestModuleParserFileErrors(t *testing.T) {
259+
parser := &ModuleParser{}
260+
261+
// Test with non-existent file
262+
module, err := parser.parse("non_existent_file.go")
263+
assert.Error(t, err, "parse() should return error for non-existent file")
264+
assert.Nil(t, module, "parse() should return nil for non-existent file")
265+
}

0 commit comments

Comments
 (0)