Skip to content

Commit

Permalink
up test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzulk committed Feb 22, 2024
1 parent 1d6c9b7 commit adbe200
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
2 changes: 1 addition & 1 deletion validator.go
Expand Up @@ -157,7 +157,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
structNs: v.str2,
fieldLen: uint8(len(cf.altName)),
structfieldLen: uint8(len(cf.name)),
value: current.Interface(),
value: getValue(current),
param: ct.param,
kind: kind,
typ: current.Type(),
Expand Down
102 changes: 102 additions & 0 deletions validator_test.go
Expand Up @@ -490,6 +490,108 @@ func TestAnonymous(t *testing.T) {
AssertError(t, err, "c", "c", "c", "c", "required")
}

func TestPrivateFieldsStruct(t *testing.T) {
type tc struct {
stct interface{}
errorNum int
}

tcs := []tc{
{
stct: &struct {
f1 int8 `validate:"required"`
f2 int16 `validate:"required"`
f3 int32 `validate:"required"`
f4 int64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: &struct {
f1 uint8 `validate:"required"`
f2 uint16 `validate:"required"`
f3 uint32 `validate:"required"`
f4 uint64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: &struct {
f1 complex64 `validate:"required"`
f2 complex128 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: &struct {
f1 float32 `validate:"required"`
f2 float64 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: struct {
f1 int8 `validate:"required"`
f2 int16 `validate:"required"`
f3 int32 `validate:"required"`
f4 int64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: struct {
f1 uint8 `validate:"required"`
f2 uint16 `validate:"required"`
f3 uint32 `validate:"required"`
f4 uint64 `validate:"required"`
}{},
errorNum: 4,
},
{
stct: struct {
f1 complex64 `validate:"required"`
f2 complex128 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: struct {
f1 float32 `validate:"required"`
f2 float64 `validate:"required"`
}{},
errorNum: 2,
},
{
stct: struct {
f1 *int `validate:"required"`
f2 struct {
f3 int `validate:"required"`
}
}{},
errorNum: 2,
},
{
stct: &struct {
f1 *int `validate:"required"`
f2 struct {
f3 int `validate:"required"`
}
}{},
errorNum: 2,
},
}

validate := New()

for _, tc := range tcs {
err := validate.Struct(tc.stct)
NotEqual(t, err, nil)

errs := err.(ValidationErrors)
Equal(t, len(errs), tc.errorNum)
}
}

func TestAnonymousSameStructDifferentTags(t *testing.T) {
validate := New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
Expand Down

0 comments on commit adbe200

Please sign in to comment.