forked from thoas/go-funk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_test.go
105 lines (82 loc) · 2.99 KB
/
retrieve_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
95
96
97
98
99
100
101
102
103
104
105
package funk
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetSlice(t *testing.T) {
is := assert.New(t)
is.Equal(Get(SliceOf(foo), "ID"), []int{1})
is.Equal(Get(SliceOf(foo), "Bar.Name"), []string{"Test"})
is.Equal(Get(SliceOf(foo), "Bar"), []*Bar{bar})
is.Equal(Get(([]Foo)(nil), "Bar.Name"), []string{})
is.Equal(Get([]Foo{}, "Bar.Name"), []string{})
is.Equal(Get([]*Foo{}, "Bar.Name"), []string{})
}
func TestGetSliceMultiLevel(t *testing.T) {
is := assert.New(t)
is.Equal(Get(foo, "Bar.Bars.Bar.Name"), []string{"Level2-1", "Level2-2"})
is.Equal(Get(SliceOf(foo), "Bar.Bars.Bar.Name"), []string{"Level2-1", "Level2-2"})
}
func TestGetNull(t *testing.T) {
is := assert.New(t)
is.Equal(Get(foo, "EmptyValue.Int64"), int64(10))
is.Equal(Get(foo, "ZeroValue"), nil)
is.Equal(false, Get(foo, "ZeroBoolValue", WithAllowZero()))
is.Equal(nil, Get(fooUnexported, "unexported", WithAllowZero()))
is.Equal(nil, Get(fooUnexported, "unexported", WithAllowZero()))
is.Equal(Get(foo, "ZeroIntValue", WithAllowZero()), 0)
is.Equal(Get(foo, "ZeroIntPtrValue", WithAllowZero()), nil)
is.Equal(Get(foo, "EmptyValue.Int64", WithAllowZero()), int64(10))
is.Equal(Get(SliceOf(foo), "EmptyValue.Int64"), []int64{10})
}
func TestGetNil(t *testing.T) {
is := assert.New(t)
is.Equal(Get(foo2, "Bar.Name"), nil)
is.Equal(Get(foo2, "Bar.Name", WithAllowZero()), "")
is.Equal(Get([]*Foo{foo, foo2}, "Bar.Name"), []string{"Test"})
is.Equal(Get([]*Foo{foo, foo2}, "Bar"), []*Bar{bar})
}
func TestGetMap(t *testing.T) {
is := assert.New(t)
m := map[string]interface{}{
"bar": map[string]interface{}{
"name": "foobar",
},
}
is.Equal("foobar", Get(m, "bar.name"))
is.Equal(nil, Get(m, "foo.name"))
is.Equal([]interface{}{"dark", "dark"}, Get([]map[string]interface{}{m1, m2}, "firstname"))
is.Equal([]interface{}{"test"}, Get([]map[string]interface{}{m1, m2}, "bar.name"))
}
func TestGetThroughInterface(t *testing.T) {
is := assert.New(t)
is.Equal(Get(foo, "BarInterface.Bars.Bar.Name"), []string{"Level2-1", "Level2-2"})
is.Equal(Get(foo, "BarPointer.Bars.Bar.Name"), []string{"Level2-1", "Level2-2"})
}
func TestGetNotFound(t *testing.T) {
is := assert.New(t)
is.Equal(nil, Get(foo, "id"))
is.Equal(nil, Get(foo, "id.id"))
is.Equal(nil, Get(foo, "Bar.id"))
is.Equal(nil, Get(foo, "Bars.id"))
}
func TestGetSimple(t *testing.T) {
is := assert.New(t)
is.Equal(Get(foo, "ID"), 1)
is.Equal(Get(foo, "Bar.Name"), "Test")
result := Get(foo, "Bar.Bars.Name")
is.Equal(result, []string{"Level1-1", "Level1-2"})
}
func TestGetOrElse(t *testing.T) {
is := assert.New(t)
str := "hello world"
is.Equal("hello world", GetOrElse(&str, "foobar"))
is.Equal("hello world", GetOrElse(str, "foobar"))
is.Equal("foobar", GetOrElse(nil, "foobar"))
t.Run("nil with type", func(t *testing.T) {
// simple nil comparison is not sufficient for nil with type.
is.False(interface{}((*string)(nil)) == nil)
// test GetOrElse coveers this case
is.Equal("foobar", GetOrElse((*string)(nil), "foobar"))
})
}