-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerate_validation_code_tests.go
More file actions
98 lines (81 loc) · 2.98 KB
/
generate_validation_code_tests.go
File metadata and controls
98 lines (81 loc) · 2.98 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
package main
import (
"fmt"
"github.com/opencodeco/validgen/internal/analyzer"
"github.com/opencodeco/validgen/internal/codegenerator"
"github.com/opencodeco/validgen/internal/common"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type ValidationCodeTestCases struct {
FuncName string
Tests []ValidationCodeTestCase
}
type ValidationCodeTestCase struct {
TestName string
FieldName string
FieldType common.FieldType
Validation string
ExpectedCode string
}
func generateValidationCodeUnitTests() error {
if err := generateValidationCodeUnitTest("build_validation_code_test.tpl", "generated_validation_code_no_pointer_test.go", false); err != nil {
return err
}
if err := generateValidationCodeUnitTest("build_validation_code_test.tpl", "generated_validation_code_pointer_test.go", true); err != nil {
return err
}
return nil
}
func generateValidationCodeUnitTest(tplFile, outputFile string, pointer bool) error {
fmt.Printf("Generating validation code test file: tplFile[%s] outputFile[%s] pointer[%v]\n", tplFile, outputFile, pointer)
funcName := "TestBuildValidationCode"
if pointer {
funcName += "Pointer"
}
testCases := ValidationCodeTestCases{
FuncName: funcName,
}
for _, typeValidation := range typesValidation {
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
}
testName := fmt.Sprintf("%s_%s_%s", typeValidation.tag, cases.Lower(language.Und).String(fieldType.ToStringName()), validation)
fieldName := "Field" + cases.Title(language.Und).String(typeValidation.tag) + fieldType.ToStringName()
gv := codegenerator.GenValidations{}
parsedValidation, err := analyzer.ParserValidation(validation)
if err != nil {
return fmt.Errorf("failed to parse validation %q: %v", validation, err)
}
expectedValidationCode, err := gv.BuildValidationCode(fieldName, fieldType, []*analyzer.Validation{parsedValidation})
if err != nil {
return fmt.Errorf("failed to build validation code for %q: %v", fieldName, err)
}
testCases.Tests = append(testCases.Tests, ValidationCodeTestCase{
TestName: testName,
FieldName: fieldName,
FieldType: fieldType,
Validation: validation,
ExpectedCode: expectedValidationCode,
})
}
}
}
if err := ExecTemplate("ValidationCodeTests", tplFile, outputFile, testCases); err != nil {
return fmt.Errorf("error generating validation code tests file %s", err)
}
fmt.Printf("Generating %s done\n", outputFile)
return nil
}