2021-01-03 16:43:34 +03:00
|
|
|
// Copyright 2020 Gin Core Team. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-11-21 16:45:03 +03:00
|
|
|
func TestSliceValidationError(t *testing.T) {
|
2021-01-03 16:43:34 +03:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-11-21 16:45:03 +03:00
|
|
|
err SliceValidationError
|
2021-01-03 16:43:34 +03:00
|
|
|
want string
|
|
|
|
}{
|
2021-11-21 16:45:03 +03:00
|
|
|
{"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
|
|
|
|
{"has zero elements", SliceValidationError{}, ""},
|
|
|
|
{"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
|
2021-06-28 17:05:29 +03:00
|
|
|
{"has two elements",
|
2021-11-21 16:45:03 +03:00
|
|
|
SliceValidationError{
|
2021-06-28 17:05:29 +03:00
|
|
|
errors.New("first error"),
|
|
|
|
errors.New("second error"),
|
|
|
|
},
|
|
|
|
"[0]: first error\n[1]: second error",
|
|
|
|
},
|
|
|
|
{"has many elements",
|
2021-11-21 16:45:03 +03:00
|
|
|
SliceValidationError{
|
2021-06-28 17:05:29 +03:00
|
|
|
errors.New("first error"),
|
|
|
|
errors.New("second error"),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
errors.New("last error"),
|
|
|
|
},
|
|
|
|
"[0]: first error\n[1]: second error\n[5]: last error",
|
|
|
|
},
|
2021-01-03 16:43:34 +03:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := tt.err.Error(); got != tt.want {
|
2021-11-21 16:45:03 +03:00
|
|
|
t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)
|
2021-01-03 16:43:34 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaultValidator(t *testing.T) {
|
|
|
|
type exampleStruct struct {
|
|
|
|
A string `binding:"max=8"`
|
|
|
|
B int `binding:"gt=0"`
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
v *defaultValidator
|
|
|
|
obj interface{}
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{"validate nil obj", &defaultValidator{}, nil, false},
|
|
|
|
{"validate int obj", &defaultValidator{}, 3, false},
|
|
|
|
{"validate struct failed-1", &defaultValidator{}, exampleStruct{A: "123456789", B: 1}, true},
|
|
|
|
{"validate struct failed-2", &defaultValidator{}, exampleStruct{A: "12345678", B: 0}, true},
|
|
|
|
{"validate struct passed", &defaultValidator{}, exampleStruct{A: "12345678", B: 1}, false},
|
|
|
|
{"validate *struct failed-1", &defaultValidator{}, &exampleStruct{A: "123456789", B: 1}, true},
|
|
|
|
{"validate *struct failed-2", &defaultValidator{}, &exampleStruct{A: "12345678", B: 0}, true},
|
|
|
|
{"validate *struct passed", &defaultValidator{}, &exampleStruct{A: "12345678", B: 1}, false},
|
|
|
|
{"validate []struct failed-1", &defaultValidator{}, []exampleStruct{{A: "123456789", B: 1}}, true},
|
|
|
|
{"validate []struct failed-2", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 0}}, true},
|
|
|
|
{"validate []struct passed", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 1}}, false},
|
|
|
|
{"validate []*struct failed-1", &defaultValidator{}, []*exampleStruct{{A: "123456789", B: 1}}, true},
|
|
|
|
{"validate []*struct failed-2", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 0}}, true},
|
|
|
|
{"validate []*struct passed", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 1}}, false},
|
|
|
|
{"validate *[]struct failed-1", &defaultValidator{}, &[]exampleStruct{{A: "123456789", B: 1}}, true},
|
|
|
|
{"validate *[]struct failed-2", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 0}}, true},
|
|
|
|
{"validate *[]struct passed", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 1}}, false},
|
|
|
|
{"validate *[]*struct failed-1", &defaultValidator{}, &[]*exampleStruct{{A: "123456789", B: 1}}, true},
|
|
|
|
{"validate *[]*struct failed-2", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 0}}, true},
|
|
|
|
{"validate *[]*struct passed", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 1}}, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if err := tt.v.ValidateStruct(tt.obj); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("defaultValidator.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|