|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "go/format" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + "text/template" |
| 11 | + |
| 12 | + "github.com/opencodeco/validgen/internal/common" |
| 13 | + "golang.org/x/text/cases" |
| 14 | + "golang.org/x/text/language" |
| 15 | +) |
| 16 | + |
| 17 | +type CmpBenchTests struct { |
| 18 | + Tests []CmpBenchTest |
| 19 | +} |
| 20 | + |
| 21 | +type CmpBenchTest struct { |
| 22 | + TestName string |
| 23 | + FieldType string |
| 24 | + BasicType string |
| 25 | + ValidGenTag string |
| 26 | + ValidatorTag string |
| 27 | + ValidInput string |
| 28 | +} |
| 29 | + |
| 30 | +func generateComparativePerformanceTests() { |
| 31 | + generateComparativePerformanceTest("cmp_perf_no_pointer_tests.tpl", "generated_cmp_perf_no_pointer_test.go", false) |
| 32 | + generateComparativePerformanceTest("cmp_perf_pointer_tests.tpl", "generated_cmp_perf_pointer_test.go", true) |
| 33 | +} |
| 34 | + |
| 35 | +func generateComparativePerformanceTest(tpl, dest string, pointer bool) { |
| 36 | + log.Printf("Generating comparative performance tests file: tpl[%s] dest[%s] pointer[%v]\n", tpl, dest, pointer) |
| 37 | + |
| 38 | + benchTests := CmpBenchTests{} |
| 39 | + |
| 40 | + for _, typeVal := range typesValidation { |
| 41 | + if typeVal.validatorTag == "" { |
| 42 | + log.Printf("Skipping tag %s: go-validator tag not defined\n", typeVal.tag) |
| 43 | + continue |
| 44 | + } |
| 45 | + |
| 46 | + for _, testCase := range typeVal.testCases { |
| 47 | + if testCase.excludeIf&cmpBenchTests != 0 { |
| 48 | + log.Printf("Skipping test: tag %s type %s\n", typeVal.tag, testCase.typeClass) |
| 49 | + continue |
| 50 | + } |
| 51 | + if testCase.excludeIf&noPointer != 0 && !pointer { |
| 52 | + log.Printf("Skipping no pointer: tag %s type %s\n", typeVal.tag, testCase.typeClass) |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + normalizedType := testCase.typeClass |
| 57 | + if pointer { |
| 58 | + normalizedType = "*" + normalizedType |
| 59 | + } |
| 60 | + |
| 61 | + fTypes := common.HelperFromNormalizedToBasicTypes(normalizedType) |
| 62 | + sNames := common.HelperFromNormalizedToStringNames(normalizedType) |
| 63 | + |
| 64 | + for i := range fTypes { |
| 65 | + validGenTag := typeVal.tag |
| 66 | + if typeVal.argsCount != common.ZeroValue { |
| 67 | + validGenTag += "=" + testCase.validation |
| 68 | + } |
| 69 | + goValidatorTag := typeVal.validatorTag |
| 70 | + if typeVal.argsCount != common.ZeroValue { |
| 71 | + goValidatorTag += "=" + testCase.validation |
| 72 | + } |
| 73 | + testName := cases.Title(language.Und).String(typeVal.tag) + sNames[i] |
| 74 | + |
| 75 | + basicType, _ := strings.CutPrefix(fTypes[i], "*") |
| 76 | + |
| 77 | + benchTests.Tests = append(benchTests.Tests, CmpBenchTest{ |
| 78 | + TestName: testName, |
| 79 | + FieldType: fTypes[i], |
| 80 | + BasicType: basicType, |
| 81 | + ValidGenTag: validGenTag, |
| 82 | + ValidatorTag: goValidatorTag, |
| 83 | + ValidInput: strings.ReplaceAll(testCase.validCase, "{{.BasicType}}", basicType), |
| 84 | + }) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + log.Printf("%d test cases were generated\n", len(benchTests.Tests)) |
| 90 | + |
| 91 | + if err := benchTests.GenerateFile(tpl, dest); err != nil { |
| 92 | + log.Fatalf("error generating comparative performance tests file %s", err) |
| 93 | + } |
| 94 | + |
| 95 | + log.Println("Generating done") |
| 96 | +} |
| 97 | + |
| 98 | +func (cbt *CmpBenchTests) GenerateFile(tplFile, output string) error { |
| 99 | + tpl, err := os.ReadFile(tplFile) |
| 100 | + if err != nil { |
| 101 | + return fmt.Errorf("error reading %s: %s", tplFile, err) |
| 102 | + } |
| 103 | + |
| 104 | + tmpl, err := template.New("BenchTest").Parse(string(tpl)) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("error parsing template %s: %s", tplFile, err) |
| 107 | + } |
| 108 | + |
| 109 | + code := new(bytes.Buffer) |
| 110 | + if err := tmpl.Execute(code, cbt); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + formattedCode, err := format.Source(code.Bytes()) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + if err := os.WriteFile(output, formattedCode, 0644); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
0 commit comments