|
1 | 1 | package ognl
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "reflect" |
4 | 6 | "testing"
|
| 7 | + "unsafe" |
5 | 8 |
|
6 | 9 | "github.com/stretchr/testify/assert"
|
7 | 10 | )
|
@@ -255,3 +258,153 @@ func TestRepetition2(t *testing.T) {
|
255 | 258 | vv := Get(r, "repetition").Value()
|
256 | 259 | assert.Equal(t, vv, "r2")
|
257 | 260 | }
|
| 261 | + |
| 262 | +// TestBaseGet 测试基础类型 |
| 263 | +func TestBaseType(t *testing.T) { |
| 264 | + p := 1 |
| 265 | + mock := struct { |
| 266 | + Bool_t bool |
| 267 | + bool_t bool |
| 268 | + Int_t int |
| 269 | + int_t int |
| 270 | + Int8_t int8 |
| 271 | + int8_t int8 |
| 272 | + Int16_t int16 |
| 273 | + int16_t int16 |
| 274 | + Int32_t int32 |
| 275 | + int32_t int32 |
| 276 | + Int64_t int64 |
| 277 | + int64_t int64 |
| 278 | + Uint_t uint |
| 279 | + uint_t uint |
| 280 | + Uint8_t uint8 |
| 281 | + uint8_t uint8 |
| 282 | + Uint16_t uint16 |
| 283 | + uint16_t uint16 |
| 284 | + Uint32_t uint32 |
| 285 | + uint32_t uint32 |
| 286 | + Uint64_t uint64 |
| 287 | + uint64_t uint64 |
| 288 | + Uintptr_t uintptr |
| 289 | + uintptr_t uintptr |
| 290 | + Float32_t float32 |
| 291 | + float32_t float32 |
| 292 | + Float64_t float64 |
| 293 | + float64_t float64 |
| 294 | + Complex64_t complex64 |
| 295 | + complex64_t complex64 |
| 296 | + Complex128_t complex128 |
| 297 | + complex128_t complex128 |
| 298 | + Array_t [1]int |
| 299 | + array_t [1]int |
| 300 | + Chan_t chan string |
| 301 | + chan_t chan string |
| 302 | + Func_t func() |
| 303 | + func_t func() |
| 304 | + Interface_t interface{} |
| 305 | + interface_t interface{} |
| 306 | + Map_t map[string]interface{} |
| 307 | + map_t map[string]interface{} |
| 308 | + Pointer_t *int |
| 309 | + pointer_t *int |
| 310 | + Slice_t []interface{} |
| 311 | + slice_t []interface{} |
| 312 | + String_t string |
| 313 | + string_t string |
| 314 | + Struct_t struct{} |
| 315 | + struct_t struct{} |
| 316 | + UnsafePointer_t unsafe.Pointer |
| 317 | + unsafePointer_t unsafe.Pointer |
| 318 | + Byte_t byte |
| 319 | + byte_t byte |
| 320 | + Rune_t rune |
| 321 | + rune_t rune |
| 322 | + }{ |
| 323 | + Bool_t: false, |
| 324 | + bool_t: true, |
| 325 | + Int_t: 13, |
| 326 | + int_t: 14, |
| 327 | + Int8_t: 15, |
| 328 | + int8_t: 16, |
| 329 | + Int16_t: 17, |
| 330 | + int16_t: 18, |
| 331 | + Int32_t: 19, |
| 332 | + int32_t: 20, |
| 333 | + Int64_t: 21, |
| 334 | + int64_t: 22, |
| 335 | + Uint_t: 3, |
| 336 | + uint_t: 4, |
| 337 | + Uint8_t: 5, |
| 338 | + uint8_t: 6, |
| 339 | + Uint16_t: 7, |
| 340 | + uint16_t: 8, |
| 341 | + Uint32_t: 9, |
| 342 | + uint32_t: 10, |
| 343 | + Uint64_t: 11, |
| 344 | + uint64_t: 12, |
| 345 | + Uintptr_t: 31, |
| 346 | + uintptr_t: 32, |
| 347 | + Float32_t: 23, |
| 348 | + float32_t: 23.1, |
| 349 | + Float64_t: 24, |
| 350 | + float64_t: 24.1, |
| 351 | + Complex64_t: 25, |
| 352 | + complex64_t: 25.1, |
| 353 | + Complex128_t: 26, |
| 354 | + complex128_t: 26.1, |
| 355 | + Array_t: [1]int{1}, |
| 356 | + array_t: [1]int{1}, |
| 357 | + Chan_t: make(chan string, 5), |
| 358 | + chan_t: make(chan string, 5), |
| 359 | + Func_t: func() { fmt.Println("666") }, |
| 360 | + func_t: func() { fmt.Println("666") }, |
| 361 | + Interface_t: `1`, |
| 362 | + interface_t: `2`, |
| 363 | + Map_t: map[string]interface{}{"1": 1}, |
| 364 | + map_t: map[string]interface{}{"1": 1}, |
| 365 | + Pointer_t: nil, |
| 366 | + pointer_t: nil, |
| 367 | + Slice_t: []interface{}{1, "2", "3"}, |
| 368 | + slice_t: []interface{}{1, "2", "3"}, |
| 369 | + String_t: "1", |
| 370 | + string_t: "2", |
| 371 | + Struct_t: struct{}{}, |
| 372 | + struct_t: struct{}{}, |
| 373 | + UnsafePointer_t: unsafe.Pointer(&p), |
| 374 | + unsafePointer_t: unsafe.Pointer(&p), |
| 375 | + Byte_t: byte(27), |
| 376 | + byte_t: byte(28), |
| 377 | + Rune_t: rune(29), |
| 378 | + rune_t: rune(30), |
| 379 | + } |
| 380 | + |
| 381 | + mokeValue := reflect.ValueOf(mock) |
| 382 | + mokeType := reflect.TypeOf(mock) |
| 383 | + mokeField := mokeValue.NumField() |
| 384 | + |
| 385 | + for i := 0; i < mokeField; i++ { |
| 386 | + structField := mokeType.Field(i) |
| 387 | + |
| 388 | + cp := reflect.New(mokeValue.Type()).Elem() |
| 389 | + cp.Set(mokeValue) |
| 390 | + rv := cp.FieldByName(structField.Name) |
| 391 | + res := reflect.NewAt(rv.Type(), unsafe.Pointer(rv.UnsafeAddr())).Elem().Interface() |
| 392 | + |
| 393 | + // 获取基本数据value |
| 394 | + value := Get(mock, structField.Name).Value() |
| 395 | + if rv.Kind() == reflect.Func { |
| 396 | + v1 := reflect.ValueOf(res) |
| 397 | + v2 := reflect.ValueOf(value) |
| 398 | + if !(v1.Pointer() == v2.Pointer()) { |
| 399 | + t.Fatalf("%s: incorrect response; want: %d, got: %d", structField.Name, v1.Pointer(), v2.Pointer()) |
| 400 | + } |
| 401 | + } else { |
| 402 | + assert.Equal(t, res, value) |
| 403 | + } |
| 404 | + |
| 405 | + // 获取基本数据Type |
| 406 | + Get(mock, structField.Name).Type() |
| 407 | + tp := Get(mock, structField.Name).Type() |
| 408 | + assert.Equal(t, int(rv.Kind()), int(tp)) |
| 409 | + } |
| 410 | +} |
0 commit comments