github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/validator_z_int_test.go (about) 1 package validator_test 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 . "github.com/onsi/gomega" 9 10 "github.com/machinefi/w3bstream/pkg/depends/kit/validator" 11 "github.com/machinefi/w3bstream/pkg/depends/x/ptrx" 12 "github.com/machinefi/w3bstream/pkg/depends/x/typesx" 13 ) 14 15 func TestInt_New(t *testing.T) { 16 cases := []struct { 17 name string 18 rule string 19 typ reflect.Type 20 expect *validator.Int 21 }{ 22 { 23 "01", "@int8[1,]", rtInt8, &validator.Int{ 24 BitSize: 8, 25 Minimum: ptrx.Int64(1), 26 Maximum: ptrx.Int64(validator.MaxInt(8)), 27 }, 28 }, { 29 "02", "@int16[1,]", rtInt16, &validator.Int{ 30 BitSize: 16, 31 Minimum: ptrx.Int64(1), 32 Maximum: ptrx.Int64(validator.MaxInt(16)), 33 }, 34 }, { 35 "03", "@int[1,]", rtInt32, &validator.Int{ 36 Minimum: ptrx.Int64(1), 37 Maximum: ptrx.Int64(validator.MaxInt(32)), 38 }, 39 }, { 40 "04", "@int[1,1000)", rtInt32, &validator.Int{ 41 Minimum: ptrx.Int64(1), 42 Maximum: ptrx.Int64(1000), 43 ExclusiveMaximum: true, 44 }, 45 }, { 46 "05", "@int(1,1000]", rtInt32, &validator.Int{ 47 Minimum: ptrx.Int64(1), 48 Maximum: ptrx.Int64(1000), 49 ExclusiveMinimum: true, 50 }, 51 }, { 52 "06", "@int[1,]", rtInt32, &validator.Int{ 53 Minimum: ptrx.Int64(1), 54 Maximum: ptrx.Int64(validator.MaxInt(32)), 55 }, 56 }, { 57 "07", "@int[1]", rtInt32, &validator.Int{ 58 Minimum: ptrx.Int64(1), 59 Maximum: ptrx.Int64(1), 60 }, 61 }, { 62 "08", "@int[,1]", rtInt32, &validator.Int{ 63 Maximum: ptrx.Int64(1), 64 }, 65 }, { 66 "09", "@int16{1,2}", rtInt32, &validator.Int{ 67 BitSize: 16, 68 Enums: map[int64]string{1: "1", 2: "2"}, 69 }, 70 }, { 71 "10", "@int16{%2}", rtInt32, &validator.Int{ 72 BitSize: 16, 73 MultipleOf: 2, 74 }, 75 }, { 76 "11", "@int64[1,1000]", rtInt64, &validator.Int{ 77 BitSize: 64, 78 Minimum: ptrx.Int64(1), 79 Maximum: ptrx.Int64(1000), 80 }, 81 }, { 82 "12", "@int<53>", rtInt64, &validator.Int{ 83 BitSize: 53, 84 Maximum: ptrx.Int64(validator.MaxInt(53)), 85 }, 86 }, 87 } 88 for _, c := range cases { 89 c.expect.SetDefault() 90 name := fmt.Sprintf("%s%s%s|%s", c.name, c.typ, c.rule, c.expect.String()) 91 t.Run(name, func(t *testing.T) { 92 ctx := validator.ContextWithFactory(bg, validator.DefaultFactory) 93 r, err := validator.ParseRuleByType([]byte(c.rule), typesx.FromReflectType(c.typ)) 94 NewWithT(t).Expect(err).To(BeNil()) 95 v, err := c.expect.New(ctx, r) 96 NewWithT(t).Expect(err).To(BeNil()) 97 NewWithT(t).Expect(v).To(Equal(c.expect)) 98 }) 99 } 100 } 101 102 func TestInt_NewFailed(t *testing.T) { 103 cases := []struct { 104 name string 105 typ reflect.Type 106 rule string 107 }{ 108 {"01", rtFloat32, "@int16"}, 109 {"02", rtInt8, "@int16"}, 110 {"03", rtInt16, "@int"}, 111 {"04", rtInt32, "@int64"}, 112 {"05", rtInt, "@int<32,2123>"}, 113 {"06", rtInt, "@int<@string>"}, 114 {"07", rtInt, "@int<66>"}, 115 {"08", rtInt, "@int[1,0]"}, 116 {"09", rtInt, "@int[1,-2]"}, 117 {"10", rtInt, "@int[a,]"}, 118 {"11", rtInt, "@int[,a]"}, 119 {"12", rtInt, "@int[a]"}, 120 {"13", rtInt, `@int8{%a}`}, 121 {"14", rtInt, `@int8{A,B,C}`}, 122 } 123 124 for _, c := range cases { 125 r := validator.MustParseRuleStringByType( 126 c.rule, 127 typesx.FromReflectType(c.typ), 128 ) 129 name := fmt.Sprintf("%s|%s|%s", c.name, c.typ, r.Bytes()) 130 t.Run(name, func(t *testing.T) { 131 v := &validator.Int{} 132 ctx := validator.ContextWithFactory(bg, validator.DefaultFactory) 133 _, err := v.New(ctx, r) 134 NewWithT(t).Expect(err).NotTo(BeNil()) 135 // t.Logf("\n%v", err) 136 }) 137 } 138 } 139 140 func TestInt_Validate(t *testing.T) { 141 cases := []struct { 142 values []interface{} 143 validator *validator.Int 144 desc string 145 }{ 146 { 147 []interface{}{reflect.ValueOf(int(1)), int(2), int(3)}, &validator.Int{ 148 Enums: map[int64]string{ 149 1: "1", 150 2: "2", 151 3: "3", 152 }, 153 }, "InEnum", 154 }, { 155 []interface{}{int(2), int(3), int(4)}, &validator.Int{ 156 Minimum: ptrx.Int64(2), 157 Maximum: ptrx.Int64(4), 158 }, "InRange", 159 }, { 160 []interface{}{int8(2), int16(3), int32(4), int64(4)}, &validator.Int{ 161 Minimum: ptrx.Int64(2), 162 Maximum: ptrx.Int64(4), 163 }, "IntTypes", 164 }, { 165 []interface{}{int64(2), int64(3), int64(4)}, &validator.Int{ 166 BitSize: 64, 167 Minimum: ptrx.Int64(2), 168 Maximum: ptrx.Int64(4), 169 }, "InRange", 170 }, { 171 []interface{}{int(2), int(4), int(6)}, &validator.Int{ 172 MultipleOf: 2, 173 }, "MultipleOf", 174 }, 175 } 176 for ci := range cases { 177 c := cases[ci] 178 c.validator.SetDefault() 179 for vi, v := range c.values { 180 name := fmt.Sprintf("%02d%02d|%s|%s|%v", 181 ci+1, vi+1, c.desc, c.validator, v) 182 t.Run(name, func(t *testing.T) { 183 NewWithT(t).Expect(c.validator.Validate(v)).To(BeNil()) 184 }) 185 } 186 } 187 } 188 189 func TestInt_ValidateFailed(t *testing.T) { 190 cases := []struct { 191 values []interface{} 192 validator *validator.Int 193 desc string 194 }{ 195 { 196 []interface{}{uint(2), "string", reflect.ValueOf("1")}, &validator.Int{ 197 BitSize: 64, 198 }, "unsupported type", 199 }, { 200 []interface{}{int(4), int(5), int(6)}, &validator.Int{ 201 Enums: map[int64]string{ 202 1: "1", 203 2: "2", 204 3: "3", 205 }, 206 }, "not in enum", 207 }, { 208 []interface{}{int(1), int(4), int(5)}, &validator.Int{ 209 Minimum: ptrx.Int64(2), 210 Maximum: ptrx.Int64(4), 211 ExclusiveMaximum: true, 212 }, "not in range", 213 }, { 214 []interface{}{int(1), int(3), int(5)}, &validator.Int{ 215 MultipleOf: 2, 216 }, "not multiple of"}, 217 } 218 219 for ci, c := range cases { 220 c.validator.SetDefault() 221 for vi, v := range c.values { 222 name := fmt.Sprintf("%02d%02d|%s|%s|%v", 223 ci+1, vi+1, c.desc, c.validator, v) 224 t.Run(name, func(t *testing.T) { 225 err := c.validator.Validate(v) 226 NewWithT(t).Expect(err).NotTo(BeNil()) 227 // t.Logf("\n%v", err) 228 }) 229 } 230 } 231 }