github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/validator/validator_z_slice_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 TestSlice_New(t *testing.T) { 16 caseSet := map[reflect.Type][]struct { 17 rule string 18 expect *Slice 19 typ reflect.Type 20 }{ 21 reflect.TypeOf([]string{}): { 22 { 23 "@slice[1,1000]", &Slice{ 24 MinItems: 1, 25 MaxItems: ptrx.Uint64(1000), 26 ElemValidator: DefaultFactory.MustCompile(bg, []byte(""), rttString), 27 }, rtSliceString, 28 }, { 29 "@slice<@string[1,2]>[1,]", &Slice{ 30 MinItems: 1, 31 ElemValidator: DefaultFactory.MustCompile(bg, []byte("@string[1,2]"), rttString), 32 }, rtSliceString, 33 }, { 34 "@slice[1]", &Slice{ 35 MinItems: 1, 36 MaxItems: ptrx.Uint64(1), 37 ElemValidator: DefaultFactory.MustCompile(bg, []byte(""), rttString), 38 }, rtSliceString, 39 }, 40 }, 41 } 42 43 for typ, cases := range caseSet { 44 for _, c := range cases { 45 t.Run(fmt.Sprintf("%s %s|%s", typ, c.rule, c.expect.String()), func(t *testing.T) { 46 r := MustParseRuleStringByType(c.rule, typesx.FromReflectType(typ)) 47 v, err := c.expect.New(ctx, r) 48 NewWithT(t).Expect(err).To(BeNil()) 49 NewWithT(t).Expect(v).To(Equal(c.expect)) 50 }) 51 } 52 } 53 } 54 55 func TestSlice_NewFailed(t *testing.T) { 56 cases := []struct { 57 rule string 58 typ reflect.Type 59 }{ 60 {"@slice[2]", rtString}, 61 {"@slice[2]", rtArrayString}, 62 {"@slice<1>", rtSliceString}, 63 {"@slice<1,2,4>", rtSliceString}, 64 {"@slice[1,0]", rtSliceString}, 65 {"@slice[1,-2]", rtSliceString}, 66 {"@slice[a,]", rtSliceString}, 67 {"@slice[-1,1]", rtSliceString}, 68 {"@slice(1,1)", rtSliceString}, 69 {"@slice<@unknown>", rtSliceString}, 70 {"@array[1,2]", rtSliceString}, 71 } 72 73 for i, c := range cases { 74 rule := MustParseRuleStringByType(c.rule, typesx.FromReflectType(c.typ)) 75 t.Run( 76 fmt.Sprintf("%02d_%s|%s", i+1, c.typ, rule.Bytes()), 77 func(t *testing.T) { 78 v := &Slice{} 79 _, err := v.New(ctx, rule) 80 NewWithT(t).Expect(err).NotTo(BeNil()) 81 // t.Logf("\n%v", err) 82 }, 83 ) 84 } 85 } 86 87 func TestSlice_Validate(t *testing.T) { 88 cases := []struct { 89 values []interface{} 90 validator *Slice 91 desc string 92 }{ 93 { 94 []interface{}{ 95 reflect.ValueOf([]string{"1", "2"}), 96 []string{"1", "2", "3"}, 97 []string{"1", "2", "3", "4"}, 98 }, &Slice{ 99 MinItems: 2, 100 MaxItems: ptrx.Uint64(4), 101 }, "InRange", 102 }, { 103 []interface{}{ 104 []string{"1", "2"}, 105 []string{"1", "2", "3"}, 106 []string{"1", "2", "3", "4"}, 107 }, &Slice{ 108 MinItems: 2, 109 MaxItems: ptrx.Uint64(4), 110 ElemValidator: DefaultFactory.MustCompile( 111 bg, []byte("@string[0,]"), rttString, 112 ), 113 }, "ElemValidate", 114 }, 115 } 116 117 for ci, c := range cases { 118 for vi, v := range c.values { 119 name := fmt.Sprintf( 120 "%02d_%02d_%s|%s|%v", ci+1, vi+1, c.desc, c.validator, v, 121 ) 122 t.Run(name, func(t *testing.T) { 123 NewWithT(t).Expect(c.validator.Validate(v)).To(BeNil()) 124 }) 125 } 126 } 127 } 128 129 func TestSlice_ValidateFailed(t *testing.T) { 130 cases := []struct { 131 values []interface{} 132 validator *Slice 133 desc string 134 }{ 135 { 136 []interface{}{ 137 []string{"1"}, 138 []string{"1", "2", "3", "4", "5"}, 139 []string{"1", "2", "3", "4", "5", "6"}, 140 }, &Slice{ 141 MinItems: 2, 142 MaxItems: ptrx.Uint64(4), 143 }, "OutOfRange", 144 }, 145 { 146 []interface{}{ 147 []string{"1", "2"}, 148 []string{"1", "2", "3"}, 149 []string{"1", "2", "3", "4"}, 150 }, &Slice{ 151 MinItems: 2, 152 MaxItems: ptrx.Uint64(4), 153 ElemValidator: DefaultFactory.MustCompile( 154 bg, []byte("@string[2,]"), rttString, 155 ), 156 }, "ElemValidate", 157 }, 158 } 159 160 for ci, c := range cases { 161 for vi, v := range c.values { 162 t.Run( 163 fmt.Sprintf( 164 "%02d_%02d_%s|%s|%v", ci, vi, c.desc, c.validator, v, 165 ), 166 func(t *testing.T) { 167 err := c.validator.Validate(v) 168 NewWithT(t).Expect(err).NotTo(BeNil()) 169 // t.Logf("\n%v", err) 170 }, 171 ) 172 } 173 } 174 } 175 176 func TestSlice(t *testing.T) { 177 r := DefaultFactory.MustCompile( 178 bg, 179 []byte("@slice<@float64<10,4>[-1.000,10000.000]?>"), 180 rttSliceFloat64, 181 ) 182 with := NewWithT(t) 183 err := r.Validate([]float64{ 184 -0.9999, 185 9999.9999, 186 8.9999, 187 0, 188 1, 189 20.1111, 190 }) 191 with.Expect(err).To(BeNil()) 192 193 err = r.Validate([]float64{-1.1}) 194 with.Expect(err).NotTo(BeNil()) 195 // t.Logf("\n%v", err) 196 197 err = r.Validate([]float64{10000.1}) 198 with.Expect(err).NotTo(BeNil()) 199 // t.Logf("\n%v", err) 200 201 err = r.Validate([]float64{0.00005}) 202 with.Expect(err).NotTo(BeNil()) 203 // t.Logf("\n%v", err) 204 }