Skip to content
Myr

default_validator_test.go

This file

Path

binding/default_validator_test.go

88 lines · 3.5 KiB · Go

Last changed by

test support go1.18 (#2990)thinkeroubefore adoption2bde107

History of this file

Take it

The walk stopped after 200 commits: lines older than a550c56 point at it.

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 1–11why, one step back
1// Copyright 2020 Gin Core Team. All rights reserved.
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package binding67import (8 "errors"9 "testing"10)11

Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c

line 12why, one step back
12func TestSliceValidationError(t *testing.T) {

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 13–14why, one step back
13 tests := []struct {
14 name string

Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c

line 15why, one step back
15 err SliceValidationError

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 16–17why, one step back
16 want string
17 }{

Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c

lines 18–20why, one step back
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"},

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

line 21why, one step back
21 {"has two elements",

Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c

line 22why, one step back
22 SliceValidationError{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 23–28why, one step back
23 errors.New("first error"),
24 errors.New("second error"),25 },26 "[0]: first error\n[1]: second error",27 },28 {"has many elements",

Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c

line 29why, one step back
29 SliceValidationError{

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 30–42why, one step back
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 {

Export struct sliceValidateError to allow error casting and rename it as (#2777)edebernisbefore adoption57ede9c

line 43why, one step back
43 t.Errorf("SliceValidationError.Error() = %v, want %v", got, tt.want)

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 44–56why, one step back
44 }
45 })46 }47}4849func 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 string56 v *defaultValidator

test support go1.18 (#2990)thinkeroubefore adoption2bde107

line 57why, one step back
57 obj any

chore: Add go1.17 for testing (#2828)Bo-Yi Wubefore adoptiona550c56

lines 58–88why, one step back
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}