-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_function_code_tests.go
More file actions
130 lines (105 loc) · 3.49 KB
/
generate_function_code_tests.go
File metadata and controls
130 lines (105 loc) · 3.49 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
package main
import (
"fmt"
"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() error {
if err := generateFunctionCodeUnitTest("function_code_test.tpl", "generated_function_code_no_pointer_test.go", false); err != nil {
return err
}
if err := generateFunctionCodeUnitTest("function_code_test.tpl", "generated_function_code_pointer_test.go", true); err != nil {
return err
}
return nil
}
func generateFunctionCodeUnitTest(tplFile, outputFile string, pointer bool) error {
fmt.Printf("Generating function code test file: tplFile[%s] outputFile[%s] pointer[%v]\n", tplFile, outputFile, 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 {
fmt.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 {
return fmt.Errorf("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 {
return fmt.Errorf("failed to build function validator code for struct %q: %v", newTest.StructName, err)
}
newTest.ExpectedCode = expectedCode
testCases.Tests = append(testCases.Tests, newTest)
}
if err := ExecTemplate("FunctionCodeTests", tplFile, outputFile, testCases); err != nil {
return fmt.Errorf("generating function code tests file %s", err)
}
fmt.Printf("Generating %s done\n", outputFile)
return nil
}