-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreflect_test.go
90 lines (81 loc) · 1.74 KB
/
reflect_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2020 Xelaj Software
//
// This file is a part of go-dry package.
// See https://github.com/xelaj/go-dry/blob/master/LICENSE for details
package dry
import (
"testing"
)
func Test_ReflectSort(t *testing.T) {
ints := []int{3, 5, 0, 2, 1, 4}
ReflectSort(ints, func(a, b int) bool {
return a < b
})
for i := range ints {
if i != ints[i] {
t.Fail()
}
}
strings := []string{"aaa", "bbb", "abb", "aab"}
ReflectSort(strings, func(a, b string) bool {
return a < b
})
if strings[0] != "aaa" {
t.Fail()
}
if strings[1] != "aab" {
t.Fail()
}
if strings[2] != "abb" {
t.Fail()
}
if strings[3] != "bbb" {
t.Fail()
}
}
type TestStruct struct {
String string
Int int
Uint8 uint8
Float32 float32
Bool bool
}
func Test_ReflectSetStructFieldsFromStringMap(t *testing.T) {
structPtr := new(TestStruct)
m := map[string]string{
"String": "Hello World",
"Int": "666",
"Uint8": "234",
"Float32": "0.01",
"Bool": "true",
}
err := ReflectSetStructFieldsFromStringMap(structPtr, m, true)
if err != nil {
t.Fatal(err)
}
if structPtr.String != "Hello World" ||
structPtr.Int != 666 ||
structPtr.Uint8 != 234 ||
structPtr.Float32 != 0.01 ||
structPtr.Bool != true {
t.Fatalf("Invalid values: %#v", structPtr)
}
m["NotExisting"] = "xxx"
structPtr = new(TestStruct)
err = ReflectSetStructFieldsFromStringMap(structPtr, m, true)
if err == nil {
t.Fail()
}
structPtr = new(TestStruct)
err = ReflectSetStructFieldsFromStringMap(structPtr, m, false)
if err != nil {
t.Fatal(err)
}
if structPtr.String != "Hello World" ||
structPtr.Int != 666 ||
structPtr.Uint8 != 234 ||
structPtr.Float32 != 0.01 ||
structPtr.Bool != true {
t.Fatalf("Invalid values: %#v", structPtr)
}
}