1
2
3
4
5 package binding
6
7 import (
8 "errors"
9 "testing"
10 )
11
12 func TestSliceValidationError(t *testing.T) {
13 tests := []struct {
14 name string
15 err SliceValidationError
16 want string
17 }{
18 {"has nil elements", SliceValidationError{errors.New("test error"), nil}, "[0]: test error"},
19 {"has zero elements", SliceValidationError{}, ""},
20 {"has one element", SliceValidationError{errors.New("test one error")}, "[0]: test one error"},
21 {"has two elements",
22 SliceValidationError{
23 errors.New("first error"),
24 errors.New("second error"),
25 },
26 "[0]: first error\n[1]: second error",
27 },
28 {"has many elements",
29 SliceValidationError{
30 errors.New("first error"),
31 errors.New("second error"),
32 nil,
33 nil,
34 nil,
35 errors.New("last error"),
36 },
37 "[0]: first error\n[1]: second error\n[5]: last error",
38 },
39 }
40 for _, tt := range tests {
41 t.Run(tt.name, func(t *testing.T) {
42 if got := tt.err.Error(); got != tt.want {
43 t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)
44 }
45 })
46 }
47 }
48
49 func TestDefaultValidator(t *testing.T) {
50 type exampleStruct struct {
51 A string `binding:"max=8"`
52 B int `binding:"gt=0"`
53 }
54 tests := []struct {
55 name string
56 v *defaultValidator
57 obj any
58 wantErr bool
59 }{
60 {"validate nil obj", &defaultValidator{}, nil, false},
61 {"validate int obj", &defaultValidator{}, 3, false},
62 {"validate struct failed-1", &defaultValidator{}, exampleStruct{A: "123456789", B: 1}, true},
63 {"validate struct failed-2", &defaultValidator{}, exampleStruct{A: "12345678", B: 0}, true},
64 {"validate struct passed", &defaultValidator{}, exampleStruct{A: "12345678", B: 1}, false},
65 {"validate *struct failed-1", &defaultValidator{}, &exampleStruct{A: "123456789", B: 1}, true},
66 {"validate *struct failed-2", &defaultValidator{}, &exampleStruct{A: "12345678", B: 0}, true},
67 {"validate *struct passed", &defaultValidator{}, &exampleStruct{A: "12345678", B: 1}, false},
68 {"validate []struct failed-1", &defaultValidator{}, []exampleStruct{{A: "123456789", B: 1}}, true},
69 {"validate []struct failed-2", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 0}}, true},
70 {"validate []struct passed", &defaultValidator{}, []exampleStruct{{A: "12345678", B: 1}}, false},
71 {"validate []*struct failed-1", &defaultValidator{}, []*exampleStruct{{A: "123456789", B: 1}}, true},
72 {"validate []*struct failed-2", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 0}}, true},
73 {"validate []*struct passed", &defaultValidator{}, []*exampleStruct{{A: "12345678", B: 1}}, false},
74 {"validate *[]struct failed-1", &defaultValidator{}, &[]exampleStruct{{A: "123456789", B: 1}}, true},
75 {"validate *[]struct failed-2", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 0}}, true},
76 {"validate *[]struct passed", &defaultValidator{}, &[]exampleStruct{{A: "12345678", B: 1}}, false},
77 {"validate *[]*struct failed-1", &defaultValidator{}, &[]*exampleStruct{{A: "123456789", B: 1}}, true},
78 {"validate *[]*struct failed-2", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 0}}, true},
79 {"validate *[]*struct passed", &defaultValidator{}, &[]*exampleStruct{{A: "12345678", B: 1}}, false},
80 }
81 for _, tt := range tests {
82 t.Run(tt.name, func(t *testing.T) {
83 if err := tt.v.ValidateStruct(tt.obj); (err != nil) != tt.wantErr {
84 t.Errorf("defaultValidator.Validate() error = %v, wantErr %v", err, tt.wantErr)
85 }
86 })
87 }
88 }
89
View as plain text