Files
godoxy-yusing/internal/serialization/validation_string_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

85 lines
2.4 KiB
Go

package serialization
import (
"errors"
"reflect"
"testing"
)
type CustomValidatingString string
func (c CustomValidatingString) Validate() error {
if c == "" {
return errors.New("string cannot be empty")
}
if len(c) < 2 {
return errors.New("string must be at least 2 characters")
}
return nil
}
func TestValidateWithCustomValidator_String(t *testing.T) {
tests := []struct {
name string
input string
wantErr bool
}{
{"regular string - no custom validation", "hello", false},
{"empty regular string - no custom validation", "", false},
{"short regular string - no custom validation", "a", 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_CustomValidatingString(t *testing.T) {
tests := []struct {
name string
input CustomValidatingString
wantErr bool
}{
{"valid custom validating string", CustomValidatingString("hello"), false},
{"invalid custom validating string - empty", CustomValidatingString(""), true},
{"invalid custom validating string - too short", CustomValidatingString("a"), true},
{"valid custom validating string - minimum length", CustomValidatingString("ab"), 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_CustomValidatingStringPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingString
wantErr bool
}{
{"valid custom validating string pointer", ptr(CustomValidatingString("hello")), false},
{"nil custom validating string pointer", nil, true},
{"invalid custom validating string pointer - empty", ptr(CustomValidatingString("")), true},
{"invalid custom validating string pointer - too short", ptr(CustomValidatingString("a")), 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)
}
})
}
}