-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffset_test.go
More file actions
220 lines (185 loc) · 4.91 KB
/
offset_test.go
File metadata and controls
220 lines (185 loc) · 4.91 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package typutil_test
import (
"context"
"net/url"
"testing"
"github.com/KarpelesLab/typutil"
)
func TestOffsetGet(t *testing.T) {
ctx := context.Background()
t.Run("map[string]any", func(t *testing.T) {
m := map[string]any{"foo": "bar", "num": 42}
val, err := typutil.OffsetGet(ctx, m, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "bar" {
t.Errorf("expected 'bar', got %v", val)
}
val, err = typutil.OffsetGet(ctx, m, "num")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != 42 {
t.Errorf("expected 42, got %v", val)
}
val, err = typutil.OffsetGet(ctx, m, "missing")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != nil {
t.Errorf("expected nil for missing key, got %v", val)
}
})
t.Run("map[string]string", func(t *testing.T) {
m := map[string]string{"foo": "bar", "hello": "world"}
val, err := typutil.OffsetGet(ctx, m, "foo")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "bar" {
t.Errorf("expected 'bar', got %v", val)
}
val, err = typutil.OffsetGet(ctx, m, "missing")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "" {
t.Errorf("expected empty string for missing key, got %v", val)
}
})
t.Run("url.Values", func(t *testing.T) {
v := url.Values{}
v.Set("name", "alice")
v.Add("tags", "a")
v.Add("tags", "b")
val, err := typutil.OffsetGet(ctx, v, "name")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "alice" {
t.Errorf("expected 'alice', got %v", val)
}
// Should return first value for multi-value key
val, err = typutil.OffsetGet(ctx, v, "tags")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "a" {
t.Errorf("expected 'a', got %v", val)
}
// Missing key should return nil
val, err = typutil.OffsetGet(ctx, v, "missing")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != nil {
t.Errorf("expected nil for missing key, got %v", val)
}
})
t.Run("[]any slice", func(t *testing.T) {
s := []any{"first", "second", "third"}
val, err := typutil.OffsetGet(ctx, s, "0")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "first" {
t.Errorf("expected 'first', got %v", val)
}
val, err = typutil.OffsetGet(ctx, s, "2")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "third" {
t.Errorf("expected 'third', got %v", val)
}
// Out of bounds should return nil
val, err = typutil.OffsetGet(ctx, s, "10")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != nil {
t.Errorf("expected nil for out of bounds, got %v", val)
}
// Invalid index should return error
_, err = typutil.OffsetGet(ctx, s, "not-a-number")
if err == nil {
t.Errorf("expected error for invalid index")
}
})
t.Run("generic map with string key via reflection", func(t *testing.T) {
type customMap map[string]int
m := customMap{"one": 1, "two": 2}
val, err := typutil.OffsetGet(ctx, m, "one")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != 1 {
t.Errorf("expected 1, got %v", val)
}
val, err = typutil.OffsetGet(ctx, m, "missing")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != nil {
t.Errorf("expected nil for missing key, got %v", val)
}
})
t.Run("unsupported type", func(t *testing.T) {
_, err := typutil.OffsetGet(ctx, 42, "key")
if err == nil {
t.Errorf("expected error for unsupported type")
}
_, err = typutil.OffsetGet(ctx, "string", "key")
if err == nil {
t.Errorf("expected error for unsupported type")
}
})
}
// offsetGetterImpl implements the offsetGetter interface for testing
type offsetGetterImpl struct {
data map[string]any
}
func (o *offsetGetterImpl) OffsetGet(ctx context.Context, key string) (any, error) {
return o.data[key], nil
}
func TestOffsetGetWithInterface(t *testing.T) {
ctx := context.Background()
getter := &offsetGetterImpl{
data: map[string]any{"key1": "value1", "key2": 123},
}
val, err := typutil.OffsetGet(ctx, getter, "key1")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "value1" {
t.Errorf("expected 'value1', got %v", val)
}
val, err = typutil.OffsetGet(ctx, getter, "key2")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != 123 {
t.Errorf("expected 123, got %v", val)
}
}
// valueReaderImpl implements the valueReader interface for testing
type valueReaderImpl struct {
value any
}
func (v *valueReaderImpl) ReadValue(ctx context.Context) (any, error) {
return v.value, nil
}
func TestOffsetGetWithValueReader(t *testing.T) {
ctx := context.Background()
reader := &valueReaderImpl{
value: map[string]any{"nested": "value"},
}
val, err := typutil.OffsetGet(ctx, reader, "nested")
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if val != "value" {
t.Errorf("expected 'value', got %v", val)
}
}