github.com/songzhibin97/gkit@v1.2.13/tools/vto/votodo_test.go (about) 1 package vto 2 3 import ( 4 "testing" 5 6 "github.com/songzhibin97/gkit/tools" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestVoToDo(t *testing.T) { 12 type ( 13 mock1 struct { 14 Name string 15 Age int 16 } 17 mock2 struct { 18 Mock *mock1 19 Ok bool 20 } 21 mock3 struct { 22 Mock *mock1 23 Ok *bool 24 } 25 mock4 struct { 26 String string `default:"handsome"` 27 Int8 int8 `default:"8"` 28 Int16 int16 `default:"16"` 29 Int32 int32 `default:"32"` 30 Int64 int64 `default:"64"` 31 Int int `default:"644"` 32 Float32 float32 `default:"32.23"` 33 Float64 float64 `default:"64.46"` 34 Bool bool `default:"true"` 35 StringPre *string `default:"handsome"` 36 Int8Pre *int8 `default:"8"` 37 Int16Pre *int16 `default:"16"` 38 Int32Pre *int32 `default:"32"` 39 Int64Pre *int64 `default:"64"` 40 IntPre *int `default:"644"` 41 Float32Pre *float32 `default:"32.23"` 42 Float64Pre *float64 `default:"64.46"` 43 BoolPre *bool `default:"true"` 44 } 45 ) 46 var ( 47 String = "handsome" 48 Int8 = int8(8) 49 Int16 = int16(16) 50 Int32 = int32(32) 51 Int64 = int64(64) 52 Int = 644 53 Float32 = float32(32.23) 54 Float64 = 64.46 55 Bool = true 56 ) 57 { 58 m0, m1 := mock1{}, mock1{ 59 Name: "gkit", 60 Age: 1, 61 } 62 err := VoToDo(m0, &m1) 63 assert.Error(t, err, tools.ErrorMustPtr) 64 err = VoToDo(&m0, m1) 65 assert.Error(t, err, tools.ErrorMustPtr) 66 } 67 68 { 69 m0, m1 := mock1{}, mock1{ 70 Name: "gkit", 71 Age: 1, 72 } 73 _ = VoToDo(&m0, &m1) 74 assert.Equal(t, m0, m1) 75 } 76 77 { 78 79 m0, m1 := mock2{}, mock2{ 80 Mock: &mock1{ 81 Name: "gkit", 82 Age: 2, 83 }, 84 Ok: true, 85 } 86 _ = VoToDo(&m0, &m1) 87 assert.Equal(t, m0, m1) 88 } 89 { 90 tt := true 91 m0, m1 := mock2{}, mock3{ 92 Mock: &mock1{ 93 Name: "gkit", 94 Age: 3, 95 }, 96 Ok: &tt, 97 } 98 _ = VoToDo(&m0, &m1) 99 assert.Equal(t, m0.Mock, m1.Mock) 100 assert.Equal(t, m0.Ok, *m1.Ok) 101 } 102 103 { 104 m0, m1 := mock4{}, mock4{} 105 _ = VoToDo(&m0, &m1) 106 m2 := mock4{ 107 String: String, 108 Int8: Int8, 109 Int16: Int16, 110 Int32: Int32, 111 Int64: Int64, 112 Int: Int, 113 Float32: Float32, 114 Float64: Float64, 115 Bool: Bool, 116 StringPre: &String, 117 Int8Pre: &Int8, 118 Int16Pre: &Int16, 119 Int32Pre: &Int32, 120 Int64Pre: &Int64, 121 IntPre: &Int, 122 Float32Pre: &Float32, 123 Float64Pre: &Float64, 124 BoolPre: &Bool, 125 } 126 assert.Equal(t, m0, m2) 127 } 128 { 129 m0, m1 := 1, mock4{} 130 err := VoToDo(&m0, &m1) 131 assert.Error(t, err, tools.ErrorMustStructPtr) 132 } 133 } 134 135 func TestVoToDoRecursion(t *testing.T) { 136 type ( 137 mock1 struct { 138 Name string `json:"name"` 139 } 140 141 mock2 struct { 142 Name string `json:"name"` 143 } 144 145 mock3 struct { 146 Name string `json:"name"` 147 Mock mock1 `json:"mock"` 148 } 149 150 mock4 struct { 151 Name string `json:"name"` 152 Mock mock2 `json:"mock"` 153 } 154 ) 155 156 var ( 157 m1 = mock3{ 158 Name: "m1", 159 Mock: mock1{ 160 Name: "m1.mock1", 161 }, 162 } 163 ) 164 { 165 var m2 mock4 166 err := VoToDo(&m2, &m1) 167 assert.NoError(t, err) 168 assert.Equal(t, m2.Name, m1.Name) 169 assert.Equal(t, m2.Mock.Name, m1.Mock.Name) 170 } 171 } 172 173 func TestVoToDoPlus(t *testing.T) { 174 type ( 175 mock1 struct { 176 Name string `json:"name"` 177 Age int `json:"age"` 178 } 179 mock2 struct { 180 Name1 string `json:"name"` 181 Age int `json:"age1"` 182 } 183 mock3 struct { 184 Name string `json:"name1"` 185 Age int `json:"age"` 186 } 187 mock4 struct { 188 Name1 string `json:"name1"` 189 Name string `json:"name"` 190 } 191 mock5 struct { 192 Name string `gkit:"name1"` 193 Age int `gkit:"age"` 194 } 195 mock6 struct { 196 Name1 string `gkit:"name1"` 197 Name string `gkit:"name"` 198 } 199 mock7 struct { 200 String string `default:"handsome"` 201 Int8 int8 `default:"8"` 202 Int16 int16 `default:"16"` 203 Int32 int32 `default:"32"` 204 Int64 int64 `default:"64"` 205 Int int `default:"644"` 206 Float32 float32 `default:"32.23"` 207 Float64 float64 `default:"64.46"` 208 Bool bool `default:"true"` 209 StringPre *string `default:"handsome"` 210 Int8Pre *int8 `default:"8"` 211 Int16Pre *int16 `default:"16"` 212 Int32Pre *int32 `default:"32"` 213 Int64Pre *int64 `default:"64"` 214 IntPre *int `default:"644"` 215 Float32Pre *float32 `default:"32.23"` 216 Float64Pre *float64 `default:"64.46"` 217 BoolPre *bool `default:"true"` 218 } 219 ) 220 var ( 221 String = "handsome" 222 Int8 = int8(8) 223 Int16 = int16(16) 224 Int32 = int32(32) 225 Int64 = int64(64) 226 Int = 644 227 Float32 = float32(32.23) 228 Float64 = 64.46 229 Bool = true 230 ) 231 232 { 233 m0, m1 := mock1{}, mock2{ 234 Name1: "gkit", 235 Age: 1, 236 } 237 err := VoToDoPlus(m0, &m1, ModelParameters{}) 238 assert.Error(t, err, tools.ErrorMustPtr) 239 err = VoToDoPlus(&m0, m1, ModelParameters{}) 240 assert.Error(t, err, tools.ErrorMustPtr) 241 } 242 243 { 244 m0, m1 := mock1{}, mock2{ 245 Name1: "gkit", 246 Age: 1, 247 } 248 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 249 Model: FieldBind, 250 }) 251 assert.Equal(t, m0.Age, m1.Age) 252 assert.NotEqual(t, m0.Name, m1.Name1) 253 } 254 { 255 m0, m1 := mock1{}, mock2{ 256 Name1: "gkit", 257 Age: 1, 258 } 259 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 260 Model: TagBind, 261 }) 262 assert.Equal(t, m0.Name, m1.Name1) 263 assert.NotEqual(t, m0.Age, m1.Age) 264 } 265 { 266 m0, m1 := mock1{}, mock2{ 267 Name1: "gkit", 268 Age: 1, 269 } 270 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 271 Model: FieldBind | TagBind | OverlayBind, 272 }) 273 assert.Equal(t, m0.Name, m1.Name1) 274 assert.Equal(t, m0.Age, m1.Age) 275 } 276 { 277 m0, m1 := mock3{}, mock4{ 278 Name1: "gkit", 279 Name: "gkit1", 280 } 281 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 282 Model: FieldBind, 283 }) 284 assert.Equal(t, m0.Name, m1.Name) 285 assert.NotEqual(t, m0.Name, m1.Name1) 286 } 287 { 288 m0, m1 := mock3{}, mock4{ 289 Name1: "gkit", 290 Name: "gkit1", 291 } 292 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 293 Model: TagBind, 294 }) 295 assert.Equal(t, m0.Name, m1.Name1) 296 assert.NotEqual(t, m0.Name, m1.Name) 297 } 298 { 299 m0, m1 := mock3{}, mock4{ 300 Name1: "gkit", 301 Name: "gkit1", 302 } 303 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 304 Model: FieldBind | TagBind, 305 }) 306 assert.Equal(t, m0.Name, m1.Name) 307 assert.NotEqual(t, m0.Name, m1.Name1) 308 } 309 { 310 m0, m1 := mock3{}, mock4{ 311 Name1: "gkit", 312 Name: "gkit1", 313 } 314 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 315 Model: FieldBind | TagBind | OverlayBind, 316 }) 317 assert.Equal(t, m0.Name, m1.Name1) 318 assert.NotEqual(t, m0.Name, m1.Name) 319 } 320 321 { 322 m0, m1 := mock5{}, mock6{ 323 Name1: "gkit", 324 Name: "gkit1", 325 } 326 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 327 Model: TagBind, 328 Tag: "gkit", 329 }) 330 assert.Equal(t, m0.Name, m1.Name1) 331 assert.NotEqual(t, m0.Name, m1.Name) 332 } 333 { 334 m0, m1 := mock5{}, mock6{ 335 Name1: "gkit", 336 Name: "gkit1", 337 } 338 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 339 Model: FieldBind | TagBind, 340 Tag: "gkit", 341 }) 342 assert.Equal(t, m0.Name, m1.Name) 343 assert.NotEqual(t, m0.Name, m1.Name1) 344 } 345 { 346 m0, m1 := mock5{}, mock6{ 347 Name1: "gkit", 348 Name: "gkit1", 349 } 350 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 351 Model: FieldBind | TagBind | OverlayBind, 352 Tag: "gkit", 353 }) 354 assert.Equal(t, m0.Name, m1.Name1) 355 assert.NotEqual(t, m0.Name, m1.Name) 356 } 357 { 358 m0, m1 := mock5{}, mock6{ 359 Name1: "gkit", 360 Name: "gkit1", 361 } 362 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 363 Model: FieldBind | TagBind | OverlayBind, 364 Tag: "gkit", 365 }) 366 assert.Equal(t, m0.Name, m1.Name1) 367 assert.NotEqual(t, m0.Name, m1.Name) 368 } 369 370 { 371 m0, m1 := mock7{}, mock7{ 372 String: String, 373 Int8: Int8, 374 Int16: Int16, 375 Int32: Int32, 376 Int64: Int64, 377 Int: Int, 378 Float32: Float32, 379 Float64: Float64, 380 Bool: Bool, 381 StringPre: &String, 382 Int8Pre: &Int8, 383 Int16Pre: &Int16, 384 Int32Pre: &Int32, 385 Int64Pre: &Int64, 386 IntPre: &Int, 387 Float32Pre: &Float32, 388 Float64Pre: &Float64, 389 BoolPre: &Bool, 390 } 391 _ = VoToDoPlus(&m0, &m1, ModelParameters{ 392 Model: DefaultValueBind, 393 }) 394 assert.Equal(t, m0, m1) 395 396 } 397 } 398 399 func TestVoToDoCoverType(t *testing.T) { 400 type ( 401 mock1 struct { 402 Age int 403 Money float32 404 } 405 mock2 struct { 406 Age uint 407 Money float64 408 } 409 ) 410 m1, m2 := mock1{}, mock2{ 411 Age: 10, 412 Money: 10.01, 413 } 414 err := VoToDo(&m1, &m2) 415 assert.NoError(t, err) 416 assert.Equal(t, m1.Age, 10) 417 assert.Equal(t, m1.Money, float32(10.01)) 418 } 419 420 func TestVoToDoNesting(t *testing.T) { 421 type ( 422 Mock1 struct { 423 Age int 424 Money float32 425 } 426 427 Mock2 struct { 428 Age int 429 Money float32 430 } 431 mock1 struct { 432 Mock Mock1 433 } 434 435 mock2 struct { 436 Mock Mock2 437 } 438 ) 439 m1, m2 := mock1{}, mock2{ 440 Mock: Mock2{ 441 Age: 10, 442 Money: 10.01, 443 }, 444 } 445 err := VoToDo(&m1, &m2) 446 assert.NoError(t, err) 447 assert.Equal(t, m1.Mock.Age, 10) 448 assert.Equal(t, m1.Mock.Money, float32(10.01)) 449 } 450 451 func TestVoToDoNesting2(t *testing.T) { 452 type ( 453 Mock1 struct { 454 Age int 455 Money float32 456 } 457 458 Mock2 struct { 459 Age int 460 Money float32 461 } 462 mock1 struct { 463 Mock1 `tag:"mock"` 464 } 465 466 mock2 struct { 467 Mock2 `tag:"mock"` 468 } 469 ) 470 m1, m2 := mock1{}, mock2{ 471 Mock2: Mock2{ 472 Age: 10, 473 Money: 10.01, 474 }, 475 } 476 err := VoToDoPlus(&m1, &m2, ModelParameters{ 477 Model: FieldBind | TagBind, 478 Tag: "tag", 479 TagSqlite: "", 480 FilterTag: nil, 481 }) 482 assert.NoError(t, err) 483 assert.Equal(t, m1.Mock1.Age, 10) 484 assert.Equal(t, m1.Mock1.Money, float32(10.01)) 485 }