-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_function_code_tests.go
More file actions
154 lines (126 loc) · 3.9 KB
/
Copy pathgenerate_function_code_tests.go
File metadata and controls
154 lines (126 loc) · 3.9 KB
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
package main
import (
"bytes"
"fmt"
"go/format"
"log"
"os"
"text/template"
"github.com/opencodeco/validgen/internal/analyzer"
"github.com/opencodeco/validgen/internal/codegenerator"
"github.com/opencodeco/validgen/internal/common"
"github.com/opencodeco/validgen/internal/parser"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type FunctionCodeTestCases struct {
FuncName string
Tests []FunctionCodeTestCase
}
type FunctionCodeTestCase struct {
TestName string
StructName string
Fields []FunctionCodeTestField
ExpectedCode string
}
type FunctionCodeTestField struct {
Name string
Type common.FieldType
Tag string
}
func generateFunctionCodeUnitTests() {
generateFunctionCodeTestsFile("function_code_test.tpl", "generated_function_code_no_pointer_test.go", false)
generateFunctionCodeTestsFile("function_code_test.tpl", "generated_function_code_pointer_test.go", true)
}
func generateFunctionCodeTestsFile(tpl, dest string, pointer bool) {
log.Printf("Generating function code test file: tpl[%s] dest[%s] pointer[%v]\n", tpl, dest, pointer)
funcName := "TestBuildFunctionCode"
if pointer {
funcName += "Pointer"
}
testCases := FunctionCodeTestCases{
FuncName: funcName,
}
for _, typeValidation := range typesValidation {
newTest := FunctionCodeTestCase{
TestName: typeValidation.tag + "Struct",
StructName: typeValidation.tag + "Struct",
}
structInfo := &analyzer.Struct{
Struct: parser.Struct{
PackageName: "main",
StructName: newTest.StructName,
},
}
for _, toGenerate := range typeValidation.testCases {
if toGenerate.excludeIf&noPointer != 0 && !pointer {
log.Printf("Skipping no pointer: tag %s type %s\n", typeValidation.tag, toGenerate.typeClass)
continue
}
normalizedType := toGenerate.typeClass
if pointer {
normalizedType = "*" + normalizedType
}
fieldTypes, _ := common.HelperFromNormalizedToFieldTypes(normalizedType)
for _, fieldType := range fieldTypes {
validation := typeValidation.tag
if typeValidation.argsCount != common.ZeroValue {
validation += "=" + toGenerate.validation
}
fieldName := "Field" + cases.Title(language.Und).String(typeValidation.tag) + fieldType.ToStringName()
parsedValidation, err := analyzer.ParserValidation(validation)
if err != nil {
log.Fatalf("failed to parse validation %q: %v", validation, err)
}
newTest.Fields = append(newTest.Fields, FunctionCodeTestField{
Name: fieldName,
Type: fieldType,
Tag: validation,
})
structInfo.Fields = append(structInfo.Fields, parser.Field{
FieldName: fieldName,
Type: fieldType,
Tag: `validate:"` + validation + `"`,
})
structInfo.FieldsValidations = append(structInfo.FieldsValidations, analyzer.FieldValidations{
Validations: []*analyzer.Validation{parsedValidation},
})
}
}
gv := codegenerator.GenValidations{
Struct: structInfo,
}
expectedCode, err := gv.BuildFuncValidatorCode()
if err != nil {
log.Fatalf("failed to build function validator code for struct %q: %v", newTest.StructName, err)
}
newTest.ExpectedCode = expectedCode
testCases.Tests = append(testCases.Tests, newTest)
}
if err := testCases.GenerateFile(tpl, dest); err != nil {
log.Fatalf("error generating function code tests file %s", err)
}
log.Printf("Generating %s done\n", dest)
}
func (tc *FunctionCodeTestCases) GenerateFile(tplFile, output string) error {
tpl, err := os.ReadFile(tplFile)
if err != nil {
return fmt.Errorf("error reading %s: %s", tplFile, err)
}
tmpl, err := template.New("ValidationCodeTests").Parse(string(tpl))
if err != nil {
return err
}
code := new(bytes.Buffer)
if err := tmpl.Execute(code, tc); err != nil {
return err
}
formattedCode, err := format.Source(code.Bytes())
if err != nil {
return err
}
if err := os.WriteFile(output, formattedCode, 0644); err != nil {
return err
}
return nil
}