Files
godoxy-yusing/internal/serialization/validation_struct_test.go
yusing 1579f490c0 refactor: replace gperr with standard errors package and simplify string parsing
- Replace gperr.Error return types with standard error across test files
- Replace gperr.New with errors.New in validation and serialization tests
- Update API documentation in README files to use error instead of gperr.Error
- Simplify string parsing using strings.Cut in docker/label.go
- Update benchmarks to use NewTestEntrypoint and remove task package dependency
2026-02-10 16:59:19 +08:00

66 lines
1.9 KiB
Go

package serialization
import (
"errors"
"reflect"
"testing"
)
type CustomValidatingStruct struct {
Value string
}
func (c CustomValidatingStruct) Validate() error {
if c.Value == "" {
return errors.New("value cannot be empty")
}
if len(c.Value) < 3 {
return errors.New("value must be at least 3 characters")
}
return nil
}
func TestValidateWithCustomValidator_Struct(t *testing.T) {
tests := []struct {
name string
input CustomValidatingStruct
wantErr bool
}{
{"valid custom validating struct", CustomValidatingStruct{Value: "hello"}, false},
{"invalid custom validating struct - empty", CustomValidatingStruct{Value: ""}, true},
{"invalid custom validating struct - too short", CustomValidatingStruct{Value: "hi"}, true},
{"valid custom validating struct - minimum length", CustomValidatingStruct{Value: "abc"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestValidateWithCustomValidator_CustomValidatingStructPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingStruct
wantErr bool
}{
{"valid custom validating struct pointer", &CustomValidatingStruct{Value: "hello"}, false},
{"nil custom validating struct pointer", nil, true},
{"invalid custom validating struct pointer - empty", &CustomValidatingStruct{Value: ""}, true},
{"invalid custom validating struct pointer - too short", &CustomValidatingStruct{Value: "hi"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ValidateWithCustomValidator(reflect.ValueOf(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("ValidateWithCustomValidator() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}