-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexers_test.go
139 lines (129 loc) · 2.64 KB
/
indexers_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package refutil
import (
"testing"
)
func TestStructIndexer(t *testing.T) {
str := struct {
Test1 string
Test2 int
}{
Test1: "test",
Test2: 1,
}
data := NewValue(str)
indexer := NewStructIndexer(data)
if len(indexer.Keys()) != 2 {
t.Fatal()
}
k1 := indexer.Keys()[0].String()
k2 := indexer.Keys()[1].String()
if k1 != "Test1" || k2 != "Test2" {
t.Fatal()
}
key1, v1 := data.At(0)
if v1.String() != "test" || key1.String() != "Test1" {
t.Fatal()
}
key2, v2 := data.At(1)
if v2.Interface().(int) != 1 || key2.String() != "Test2" {
t.Fatal()
}
}
func TestMapIndexer(t *testing.T) {
str := map[string]interface{}{
"Test1": "test",
"Test2": 1,
}
data := NewValue(str)
indexer := NewMapIndexer(data)
if len(indexer.Keys()) != 2 {
t.Fatal()
}
k1 := indexer.Keys()[0].String()
k2 := indexer.Keys()[1].String()
if k1 != "Test1" || k2 != "Test2" {
t.Fatal()
}
key1, v1 := data.At(0)
if v1.String() != "test" || key1.String() != "Test1" {
t.Fatal()
}
key2, v2 := data.At(1)
if v2.Interface().(int) != 1 || key2.String() != "Test2" {
t.Fatal()
}
}
func TestSliceIndexer(t *testing.T) {
str := []string{"1", "2"}
data := NewValue(str)
indexer := NewSliceIndexer(data)
if len(indexer.Keys()) != 2 {
t.Fatal()
}
k1 := indexer.Keys()[0].String()
k2 := indexer.Keys()[1].String()
if k1 != "0" || k2 != "1" {
t.Fatal()
}
key1, v1 := data.At(0)
if v1.String() != "1" || key1.String() != "0" {
t.Fatal()
}
key2, v2 := data.At(1)
if v2.String() != "2" || key2.String() != "1" {
t.Fatal()
}
}
func TestCanIndex(t *testing.T) {
if !NewValue([]uint{1, 2, 3}).CanIndex() {
t.Fatal()
}
if !NewValue("123").CanIndex() {
t.Fatal()
}
if NewValue(struct{}{}).CanIndex() {
t.Fatal()
}
}
func TestIndex(t *testing.T) {
val := NewValue([]uint{1, 2, 3})
if val.Index(1).InterfaceOrNil().(uint) != uint(2) {
t.Fatal()
}
}
func TestCanUseIndexer(t *testing.T) {
if !NewValue([]uint{}).CanUseIndexer() {
t.Fatal()
}
if NewValue("").CanUseIndexer() {
t.Fatal()
}
}
func TestIndexer(t *testing.T) {
val := NewValue([]uint{1, 2, 3})
_ = val.Indexer().(*SliceIndexer)
val = NewValue(map[string]string{})
_ = val.Indexer().(*MapIndexer)
val = NewValue(struct{}{})
_ = val.Indexer().(*StructIndexer)
defer func() {
if r := recover(); r != ErrArgumentNotIndexable {
t.Fatal()
}
}()
NewValue("").Indexer()
}
func TestIndexerAt(t *testing.T) {
val := NewValue([]uint{1, 2, 3})
k, v := val.At(0)
if k.String() != "0" || v.String() != "1" {
t.Fatal()
}
}
func TestDataIndexerAt(t *testing.T) {
val := NewData([]uint{1, 2, 3})
k, v := val.At(0)
if k.String() != "0" || v.String() != "1" {
t.Fatal()
}
}