-
Notifications
You must be signed in to change notification settings - Fork 22
/
reflect_test.go
94 lines (76 loc) · 1.8 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
91
92
93
94
package logrus_fluent
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
type eyes int
type animal struct {
eyes
Fur bool
skills string
}
type Creature struct {
*animal
Name string
Human bool
Height int
Weight int
Alias string `fluent:"nickname"`
Cute bool `fluent:"-"`
}
func TestConvertToValuError(t *testing.T) {
assert := assert.New(t)
err := errors.New("the error")
data := map[string]interface{}{"error": err}
result := ConvertToValue(data, TagName)
r, ok := result.(map[string]interface{})
assert.True(ok)
assert.Equal(err.Error(), r["error"])
}
func TestConvertToValueStruct(t *testing.T) {
assert := assert.New(t)
v := Creature{
animal: &animal{
eyes: 2,
Fur: true,
skills: "kill,purr",
},
Name: "cat",
Height: 50,
Weight: 4,
Alias: "tama",
}
result := ConvertToValue(v, TagName)
r, ok := result.(map[string]interface{})
assert.True(ok)
assert.Equal(v.Name, r["Name"])
assert.Equal(v.Height, r["Height"])
assert.Equal(v.Weight, r["Weight"])
assert.Equal(v.Alias, r["nickname"])
assert.Equal(v.Human, r["Human"])
assert.Equal(v.Fur, r["Fur"])
assert.NotContains(r, "Cute")
assert.NotContains(r, "skills")
assert.NotContains(r, "eyes")
}
func TestConvertToValueSlice(t *testing.T) {
assert := assert.New(t)
var list []*Creature
list = append(list, &Creature{Name: "cat"})
list = append(list, &Creature{Name: "dog"})
list = append(list, nil)
list = append(list, &Creature{Name: "bird"})
result := ConvertToValue(list, TagName)
r, ok := result.([]interface{})
assert.True(ok)
assert.Len(r, 4)
}
func TestConvertToValueNil(t *testing.T) {
assert := assert.New(t)
result := ConvertToValue(nil, TagName)
assert.Equal(nil, result)
var ptr *Creature
result = ConvertToValue(ptr, TagName)
assert.Equal(nil, result)
}