-
Notifications
You must be signed in to change notification settings - Fork 2
TestGen refactor (#93) #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
611b33b
e324603
2cdca99
c2142d2
f6bdb38
581fa9a
98b323a
5591942
47d14f6
64050fa
2ee054c
abc9fe5
f06c1cb
7bb3ad6
e0325e6
4f9fa12
427754d
b205a3a
8a8c8f8
c09649e
2f9c8ed
fd656b3
dc1fdf3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Code generated by TestGen. DO NOT EDIT. | ||
|
|
||
| package benchtests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-playground/validator/v10" | ||
| ) | ||
|
|
||
| {{range .Tests}} | ||
| type ValidGen{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `valid:"{{.ValidGenTag}}"` | ||
| } | ||
|
|
||
| type Validator{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `validate:"{{.ValidatorTag}}"` | ||
| } | ||
| {{end}} | ||
|
|
||
| {{range .Tests}} | ||
| func BenchmarkValidGen{{.TestName}}(b *testing.B) { | ||
| data := &ValidGen{{.TestName}}Struct{ | ||
| Field: {{.ValidInput}}, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
| if err := ValidGen{{.TestName}}StructValidate(data); len(err) > 0 { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkValidator{{.TestName}}(b *testing.B) { | ||
| var validate *validator.Validate | ||
|
|
||
| validate = validator.New(validator.WithRequiredStructEnabled()) | ||
| data := &Validator{{.TestName}}Struct{ | ||
| Field: {{.ValidInput}}, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
|
||
| if err := validate.Struct(data); err != nil { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
| {{end}} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Code generated by TestGen. DO NOT EDIT. | ||
|
|
||
| package benchtests | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-playground/validator/v10" | ||
| ) | ||
|
|
||
| {{range .Tests}} | ||
| type ValidGen{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `valid:"{{.ValidGenTag}}"` | ||
| } | ||
|
|
||
| type Validator{{.TestName}}Struct struct { | ||
| Field {{.FieldType}} `validate:"{{.ValidatorTag}}"` | ||
| } | ||
| {{end}} | ||
|
|
||
| {{range .Tests}} | ||
| func BenchmarkValidGen{{.TestName}}(b *testing.B) { | ||
| var validInput {{.BasicType}} = {{.ValidInput}} | ||
| data := &ValidGen{{.TestName}}Struct{ | ||
| Field: &validInput, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
|
||
| if err := ValidGen{{.TestName}}StructValidate(data); len(err) > 0 { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkValidator{{.TestName}}(b *testing.B) { | ||
| var validate *validator.Validate | ||
|
|
||
| validate = validator.New(validator.WithRequiredStructEnabled()) | ||
|
|
||
| var validInput {{.BasicType}} = {{.ValidInput}} | ||
|
|
||
| data := &Validator{{.TestName}}Struct{ | ||
| Field: &validInput, | ||
| } | ||
|
|
||
| for b.Loop() { | ||
|
||
| if err := validate.Struct(data); err != nil { | ||
| b.FailNow() | ||
| } | ||
| } | ||
| } | ||
| {{end}} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "go/format" | ||
| "os" | ||
| "text/template" | ||
| ) | ||
|
|
||
| func ExecTemplate(tplName, tplFile, output string, data any) error { | ||
| tpl, err := os.ReadFile(tplFile) | ||
| if err != nil { | ||
| return fmt.Errorf("error reading %s: %s", tplFile, err) | ||
| } | ||
|
|
||
| tmpl, err := template.New(tplName).Parse(string(tpl)) | ||
| if err != nil { | ||
| return fmt.Errorf("error parsing template %s: %s", tplFile, err) | ||
| } | ||
|
|
||
| code := new(bytes.Buffer) | ||
| if err := tmpl.Execute(code, data); 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/opencodeco/validgen/internal/common" | ||
| "golang.org/x/text/cases" | ||
| "golang.org/x/text/language" | ||
| ) | ||
|
|
||
| type CmpBenchTests struct { | ||
| Tests []CmpBenchTest | ||
| } | ||
|
|
||
| type CmpBenchTest struct { | ||
| TestName string | ||
| FieldType string | ||
| BasicType string | ||
| ValidGenTag string | ||
| ValidatorTag string | ||
| ValidInput string | ||
| } | ||
|
|
||
| func generateComparativePerformanceTests() error { | ||
| if err := generateComparativePerformanceTest("cmp_perf_no_pointer_tests.tpl", "generated_cmp_perf_no_pointer_test.go", false); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := generateComparativePerformanceTest("cmp_perf_pointer_tests.tpl", "generated_cmp_perf_pointer_test.go", true); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func generateComparativePerformanceTest(tplFile, outputFile string, pointer bool) error { | ||
| fmt.Printf("Generating comparative performance tests file: tplFile[%s] outputFile[%s] pointer[%v]\n", tplFile, outputFile, pointer) | ||
|
|
||
| benchTests := CmpBenchTests{} | ||
|
|
||
| for _, typeVal := range typesValidation { | ||
| if typeVal.validatorTag == "" { | ||
| fmt.Printf("Skipping tag %s: go-validator tag not defined\n", typeVal.tag) | ||
| continue | ||
| } | ||
|
|
||
| for _, testCase := range typeVal.testCases { | ||
| if testCase.excludeIf&cmpBenchTests != 0 { | ||
| fmt.Printf("Skipping test: tag %s type %s\n", typeVal.tag, testCase.typeClass) | ||
| continue | ||
| } | ||
| if testCase.excludeIf&noPointer != 0 && !pointer { | ||
| fmt.Printf("Skipping no pointer: tag %s type %s\n", typeVal.tag, testCase.typeClass) | ||
| continue | ||
| } | ||
|
|
||
| normalizedType := testCase.typeClass | ||
| if pointer { | ||
| normalizedType = "*" + normalizedType | ||
| } | ||
|
|
||
| fTypes := common.HelperFromNormalizedToBasicTypes(normalizedType) | ||
| sNames := common.HelperFromNormalizedToStringNames(normalizedType) | ||
|
|
||
| for i := range fTypes { | ||
| validGenTag := typeVal.tag | ||
| if typeVal.argsCount != common.ZeroValue { | ||
| validGenTag += "=" + testCase.validation | ||
| } | ||
| goValidatorTag := typeVal.validatorTag | ||
| if typeVal.argsCount != common.ZeroValue { | ||
| goValidatorTag += "=" + testCase.validation | ||
| } | ||
| testName := cases.Title(language.Und).String(typeVal.tag) + sNames[i] | ||
|
|
||
| basicType, _ := strings.CutPrefix(fTypes[i], "*") | ||
|
|
||
| benchTests.Tests = append(benchTests.Tests, CmpBenchTest{ | ||
| TestName: testName, | ||
| FieldType: fTypes[i], | ||
| BasicType: basicType, | ||
| ValidGenTag: validGenTag, | ||
| ValidatorTag: goValidatorTag, | ||
| ValidInput: strings.ReplaceAll(testCase.validCase, "{{.BasicType}}", basicType), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fmt.Printf("%d test cases were generated\n", len(benchTests.Tests)) | ||
|
|
||
| if err := ExecTemplate("BenchTest", tplFile, outputFile, benchTests); err != nil { | ||
| return fmt.Errorf("error generating comparative performance tests file %s", err) | ||
| } | ||
|
|
||
| fmt.Printf("Generating %s done\n", outputFile) | ||
|
|
||
| return nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
b.Loop()does not exist in the testing.B API. This should bei := 0; i < b.N; i++to properly iterate in a benchmark test.