Skip to content

Commit a0fdb37

Browse files
committed
simplify tests and code
1 parent 65b0a65 commit a0fdb37

25 files changed

Lines changed: 286 additions & 594 deletions

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.24.0
55
retract v1.0.0-rc.1 // Human error
66

77
require (
8+
github.com/Masterminds/sprig/v3 v3.3.0
89
github.com/maypok86/otter v1.2.4
910
github.com/prometheus/client_golang v1.22.0
1011
github.com/stretchr/testify v1.10.0
@@ -14,19 +15,29 @@ require (
1415
)
1516

1617
require (
18+
dario.cat/mergo v1.0.1 // indirect
19+
github.com/Masterminds/goutils v1.1.1 // indirect
20+
github.com/Masterminds/semver/v3 v3.3.0 // indirect
1721
github.com/beorn7/perks v1.0.1 // indirect
1822
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1923
github.com/davecgh/go-spew v1.1.1 // indirect
2024
github.com/dolthub/maphash v0.1.0 // indirect
2125
github.com/gammazero/deque v1.0.0 // indirect
26+
github.com/google/uuid v1.6.0 // indirect
27+
github.com/huandu/xstrings v1.5.0 // indirect
2228
github.com/kylelemons/godebug v1.1.0 // indirect
29+
github.com/mitchellh/copystructure v1.2.0 // indirect
30+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2331
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2432
github.com/pmezard/go-difflib v1.0.0 // indirect
2533
github.com/prometheus/client_model v0.6.2 // indirect
2634
github.com/prometheus/common v0.64.0 // indirect
2735
github.com/prometheus/procfs v0.16.1 // indirect
2836
github.com/rogpeppe/go-internal v1.12.0 // indirect
37+
github.com/shopspring/decimal v1.4.0 // indirect
38+
github.com/spf13/cast v1.7.0 // indirect
2939
go.uber.org/multierr v1.11.0 // indirect
40+
golang.org/x/crypto v0.39.0 // indirect
3041
golang.org/x/sys v0.33.0 // indirect
3142
golang.org/x/text v0.26.0 // indirect
3243
google.golang.org/protobuf v1.36.6 // indirect

go.sum

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
2+
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
3+
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
4+
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
5+
github.com/Masterminds/semver/v3 v3.3.0 h1:B8LGeaivUe71a5qox1ICM/JLl0NqZSW5CHyL+hmvYS0=
6+
github.com/Masterminds/semver/v3 v3.3.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
7+
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
8+
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
19
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
210
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
311
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
@@ -6,10 +14,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
614
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
715
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
816
github.com/dolthub/maphash v0.1.0/go.mod h1:gkg4Ch4CdCDu5h6PMriVLawB7koZ+5ijb9puGMV50a4=
17+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
18+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
919
github.com/gammazero/deque v1.0.0 h1:LTmimT8H7bXkkCy6gZX7zNLtkbz4NdS2z8LZuor3j34=
1020
github.com/gammazero/deque v1.0.0/go.mod h1:iflpYvtGfM3U8S8j+sZEKIak3SAKYpA5/SQewgfXDKo=
1121
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1222
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
23+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
24+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
25+
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
26+
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
1327
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1428
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
1529
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -18,6 +32,10 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
1832
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
1933
github.com/maypok86/otter v1.2.4 h1:HhW1Pq6VdJkmWwcZZq19BlEQkHtI8xgsQzBVXJU0nfc=
2034
github.com/maypok86/otter v1.2.4/go.mod h1:mKLfoI7v1HOmQMwFgX4QkRk23mX6ge3RDvjdHOWG4R4=
35+
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
36+
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
37+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
38+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
2139
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
2240
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
2341
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -32,6 +50,10 @@ github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzM
3250
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
3351
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
3452
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
53+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
54+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
55+
github.com/spf13/cast v1.7.0 h1:ntdiHjuueXFgm5nzDRdOS4yfT43P5Fnud6DH50rz/7w=
56+
github.com/spf13/cast v1.7.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
3557
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
3658
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3759
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
@@ -42,6 +64,8 @@ go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
4264
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
4365
go.uber.org/zap/exp v0.3.0 h1:6JYzdifzYkGmTdRR59oYH+Ng7k49H9qVpWwNSsGJj3U=
4466
go.uber.org/zap/exp v0.3.0/go.mod h1:5I384qq7XGxYyByIhHm6jg5CHkGY0nsTfbDLgDDlgJQ=
67+
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
68+
golang.org/x/crypto v0.39.0/go.mod h1:L+Xg3Wf6HoL4Bn4238Z6ft6KfEpN0tJGo53AAPC632U=
4569
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
4670
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
4771
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=

internal/extgen/cfile.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package extgen
22

33
import (
4+
"github.com/Masterminds/sprig/v3"
5+
46
"bytes"
57
_ "embed"
68
"path/filepath"
@@ -20,7 +22,6 @@ type cTemplateData struct {
2022
Functions []phpFunction
2123
Classes []phpClass
2224
Constants []phpConstant
23-
Version string
2425
}
2526

2627
func (cg *cFileGenerator) generate() error {
@@ -29,6 +30,7 @@ func (cg *cFileGenerator) generate() error {
2930
if err != nil {
3031
return err
3132
}
33+
3234
return WriteFile(filename, content)
3335
}
3436

@@ -50,27 +52,15 @@ func (cg *cFileGenerator) buildContent() (string, error) {
5052
}
5153

5254
func (cg *cFileGenerator) getTemplateContent() (string, error) {
53-
tmpl, err := template.New("cfile").Funcs(template.FuncMap{
54-
"inc": func(i int) int {
55-
return i + 1
56-
},
57-
}).Parse(cFileContent)
58-
59-
if err != nil {
60-
return "", err
61-
}
55+
tmpl := template.Must(template.New("cfile").Funcs(sprig.FuncMap()).Parse(cFileContent))
6256

63-
data := cTemplateData{
57+
var buf bytes.Buffer
58+
if err := tmpl.Execute(&buf, cTemplateData{
6459
BaseName: cg.generator.BaseName,
6560
Functions: cg.generator.Functions,
6661
Classes: cg.generator.Classes,
6762
Constants: cg.generator.Constants,
68-
Version: "1.0.0",
69-
}
70-
71-
var buf bytes.Buffer
72-
err = tmpl.Execute(&buf, data)
73-
if err != nil {
63+
}); err != nil {
7464
return "", err
7565
}
7666

internal/extgen/cfile_test.go

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package extgen
22

33
import (
44
"github.com/stretchr/testify/require"
5-
"os"
65
"path/filepath"
76
"strings"
87
"testing"
@@ -11,11 +10,7 @@ import (
1110
)
1211

1312
func TestCFileGenerator_Generate(t *testing.T) {
14-
tmpDir, err := os.MkdirTemp("", "c_file_generator_test")
15-
require.NoError(t, err)
16-
t.Cleanup(func() {
17-
os.RemoveAll(tmpDir)
18-
})
13+
tmpDir := t.TempDir()
1914

2015
generator := &Generator{
2116
BaseName: "test_extension",
@@ -54,8 +49,7 @@ func TestCFileGenerator_Generate(t *testing.T) {
5449
require.NoError(t, cGen.generate())
5550

5651
expectedFile := filepath.Join(tmpDir, "test_extension.c")
57-
_, err = os.Stat(expectedFile)
58-
assert.False(t, os.IsNotExist(err), "Expected C file was not created: %s", expectedFile)
52+
require.FileExists(t, expectedFile, "Expected C file was not created: %s", expectedFile)
5953

6054
content, err := ReadFile(expectedFile)
6155
require.NoError(t, err)
@@ -141,9 +135,7 @@ func TestCFileGenerator_BuildContent(t *testing.T) {
141135

142136
cGen := cFileGenerator{generator}
143137
content, err := cGen.buildContent()
144-
if err != nil {
145-
t.Fatalf("buildContent() failed: %v", err)
146-
}
138+
require.NoError(t, err)
147139

148140
for _, expected := range tt.contains {
149141
assert.Contains(t, content, expected, "Generated C content should contain '%s'", expected)
@@ -212,12 +204,7 @@ func TestCFileGenerator_GetTemplateContent(t *testing.T) {
212204
}
213205

214206
func TestCFileIntegrationWithGenerators(t *testing.T) {
215-
tmpDir, err := os.MkdirTemp("", "c_integration_test")
216-
require.NoError(t, err)
217-
218-
t.Cleanup(func() {
219-
os.RemoveAll(tmpDir)
220-
})
207+
tmpDir := t.TempDir()
221208

222209
functions := []phpFunction{
223210
{
@@ -324,9 +311,7 @@ func TestCFileSpecialCharacters(t *testing.T) {
324311

325312
cGen := cFileGenerator{generator}
326313
content, err := cGen.buildContent()
327-
if err != nil {
328-
t.Fatalf("buildContent() failed: %v", err)
329-
}
314+
require.NoError(t, err)
330315

331316
expectedInclude := "#include \"" + tt.expected + ".h\""
332317
assert.Contains(t, content, expectedInclude, "Content should contain include: %s", expectedInclude)
@@ -434,7 +419,7 @@ func TestCFileConstants(t *testing.T) {
434419
},
435420
{
436421
Name: "GLOBAL_STRING",
437-
Value: "\"test\"",
422+
Value: `"test"`,
438423
PhpType: "string",
439424
},
440425
},
@@ -455,9 +440,7 @@ func TestCFileConstants(t *testing.T) {
455440

456441
cGen := cFileGenerator{generator}
457442
content, err := cGen.buildContent()
458-
if err != nil {
459-
t.Fatalf("buildContent() failed: %v", err)
460-
}
443+
require.NoError(t, err)
461444

462445
for _, expected := range tt.contains {
463446
assert.Contains(t, content, expected, "Generated C content should contain '%s'", expected)

internal/extgen/classparser.go

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ func (cp *classParser) Parse(filename string) ([]phpClass, error) {
2727
return cp.parse(filename)
2828
}
2929

30-
func (cp *classParser) parse(filename string) ([]phpClass, error) {
30+
func (cp *classParser) parse(filename string) (classes []phpClass, err error) {
3131
fset := token.NewFileSet()
3232
node, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
3333
if err != nil {
3434
return nil, fmt.Errorf("parsing file: %w", err)
3535
}
3636

37-
var classes []phpClass
3837
validator := Validator{}
3938

4039
exportDirectives := cp.collectExportDirectives(node, fset)
@@ -137,20 +136,6 @@ func (cp *classParser) extractPHPClassCommentWithLine(commentGroup *ast.CommentG
137136
return "", 0
138137
}
139138

140-
func (cp *classParser) extractPHPClassComment(commentGroup *ast.CommentGroup) string {
141-
if commentGroup == nil {
142-
return ""
143-
}
144-
145-
for _, comment := range commentGroup.List {
146-
if matches := phpClassRegex.FindStringSubmatch(comment.Text); matches != nil {
147-
return matches[1]
148-
}
149-
}
150-
151-
return ""
152-
}
153-
154139
func (cp *classParser) parseStructFields(fields []*ast.Field) []phpClassProperty {
155140
var properties []phpClassProperty
156141

@@ -177,6 +162,7 @@ func (cp *classParser) parseStructField(fieldName string, field *ast.Field) phpC
177162
}
178163

179164
prop.PhpType = cp.goTypeToPHPType(prop.goType)
165+
180166
return prop
181167
}
182168

@@ -217,14 +203,19 @@ func (cp *classParser) goTypeToPHPType(goType string) string {
217203
return "mixed"
218204
}
219205

220-
func (cp *classParser) parseMethods(filename string) ([]phpClassMethod, error) {
206+
func (cp *classParser) parseMethods(filename string) (methods []phpClassMethod, err error) {
221207
file, err := os.Open(filename)
222208
if err != nil {
223209
return nil, err
224210
}
225-
defer file.Close()
226211

227-
var methods []phpClassMethod
212+
defer func() {
213+
e := file.Close()
214+
if err != nil {
215+
err = e
216+
}
217+
}()
218+
228219
scanner := bufio.NewScanner(file)
229220
var currentMethod *phpClassMethod
230221

@@ -239,7 +230,8 @@ func (cp *classParser) parseMethods(filename string) ([]phpClassMethod, error) {
239230

240231
method, err := cp.parseMethodSignature(className, signature)
241232
if err != nil {
242-
fmt.Printf("Warning: Error parsing method signature '%s': %v\n", signature, err)
233+
fmt.Printf("Warning: Error parsing method signature %q: %v\n", signature, err)
234+
243235
continue
244236
}
245237

@@ -253,7 +245,8 @@ func (cp *classParser) parseMethods(filename string) ([]phpClassMethod, error) {
253245
}
254246

255247
if err := validator.validateScalarTypes(phpFunc); err != nil {
256-
fmt.Printf("Warning: Method '%s::%s' uses unsupported types: %v\n", className, method.Name, err)
248+
fmt.Printf("Warning: Method \"%s::%s\" uses unsupported types: %v\n", className, method.Name, err)
249+
257250
continue
258251
}
259252

@@ -266,6 +259,7 @@ func (cp *classParser) parseMethods(filename string) ([]phpClassMethod, error) {
266259
if err != nil {
267260
return nil, fmt.Errorf("extracting Go method function: %w", err)
268261
}
262+
269263
currentMethod.goFunction = goFunc
270264

271265
validator := Validator{}
@@ -318,6 +312,7 @@ func (cp *classParser) parseMethodSignature(className, signature string) (*phpCl
318312
if err != nil {
319313
return nil, fmt.Errorf("parsing parameter '%s': %w", part, err)
320314
}
315+
321316
params = append(params, param)
322317
}
323318
}
@@ -366,7 +361,7 @@ func (cp *classParser) sanitizeDefaultValue(value string) string {
366361
return "null"
367362
}
368363

369-
return strings.Trim(value, "'\"")
364+
return strings.Trim(value, `'"`)
370365
}
371366

372367
func (cp *classParser) extractGoMethodFunction(scanner *bufio.Scanner, firstLine string) (string, error) {

0 commit comments

Comments
 (0)