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

93 lines
2.9 KiB
Go

package serialization
import (
"errors"
"reflect"
"testing"
)
type CustomValidatingPointerString string
func (c *CustomValidatingPointerString) Validate() error {
if c == nil {
return errors.New("pointer string cannot be nil")
}
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_StringPointer(t *testing.T) {
tests := []struct {
name string
input *string
wantErr bool
}{
{"valid string pointer", ptr("hello"), false},
{"nil string pointer", nil, false},
{"empty string pointer", ptr(""), 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_CustomValidatingPointerStringValue(t *testing.T) {
tests := []struct {
name string
input CustomValidatingPointerString
wantErr bool
}{
{"custom validating pointer string as value - valid", CustomValidatingPointerString("hello"), false},
{"custom validating pointer string as value - empty", CustomValidatingPointerString(""), false},
{"custom validating pointer string as value - short", CustomValidatingPointerString("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_CustomValidatingPointerStringPointer(t *testing.T) {
tests := []struct {
name string
input *CustomValidatingPointerString
wantErr bool
}{
{"valid custom validating pointer string", customStringPointerPtr(CustomValidatingPointerString("hello")), false},
{"nil custom validating pointer string", nil, true}, // Should fail because Validate() checks for nil
{"invalid custom validating pointer string - empty", customStringPointerPtr(CustomValidatingPointerString("")), true},
{"invalid custom validating pointer string - too short", customStringPointerPtr(CustomValidatingPointerString("a")), true},
{"valid custom validating pointer string - minimum length", customStringPointerPtr(CustomValidatingPointerString("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)
}
})
}
}
// Helper function to create CustomValidatingPointerString pointer
func customStringPointerPtr(s CustomValidatingPointerString) *CustomValidatingPointerString {
return &s
}