-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
153 lines (123 loc) · 3.57 KB
/
Copy pathcache_test.go
File metadata and controls
153 lines (123 loc) · 3.57 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
package structwalker
import (
"reflect"
"testing"
)
func TestTypeCache_GetAndCacheHit(t *testing.T) {
cache := newTypeCache()
type Sample struct {
Name string `json:"name" validate:"required"`
Email string `json:"email"`
}
st := reflect.TypeOf(Sample{})
keys := []string{"json", "validate"}
meta1 := cache.get(st, keys, nil)
if meta1 == nil {
t.Fatal("expected non-nil meta")
}
if len(meta1.Fields) != 2 {
t.Errorf("len(Fields) = %d, want 2", len(meta1.Fields))
}
// Second call should return cached value
meta2 := cache.get(st, keys, nil)
if meta1 != meta2 {
t.Error("expected same pointer on cache hit")
}
}
func TestTypeCache_FieldMeta(t *testing.T) {
cache := newTypeCache()
type Inner struct {
Value int `json:"value"`
}
type Outer struct {
Name string `json:"name"`
Inner Inner `json:"inner"`
Ptr *Inner `json:"ptr"`
Items []Inner `json:"items"`
Lookup map[string]string `json:"lookup"`
}
st := reflect.TypeOf(Outer{})
meta := cache.get(st, []string{"json"}, nil)
if len(meta.Fields) != 5 {
t.Fatalf("len(Fields) = %d, want 5", len(meta.Fields))
}
// Name: string, not struct/ptr/slice/map
name := meta.Fields[0]
if name.IsStruct || name.IsPtr || name.IsSlice || name.IsMap {
t.Errorf("Name field: unexpected flags: struct=%v ptr=%v slice=%v map=%v",
name.IsStruct, name.IsPtr, name.IsSlice, name.IsMap)
}
// Inner: struct
inner := meta.Fields[1]
if !inner.IsStruct {
t.Error("Inner should be IsStruct")
}
// Ptr: pointer to struct
ptr := meta.Fields[2]
if !ptr.IsPtr || !ptr.IsStruct {
t.Errorf("Ptr: IsPtr=%v IsStruct=%v, want both true", ptr.IsPtr, ptr.IsStruct)
}
// Items: slice
items := meta.Fields[3]
if !items.IsSlice {
t.Error("Items should be IsSlice")
}
// Lookup: map
lookup := meta.Fields[4]
if !lookup.IsMap {
t.Error("Lookup should be IsMap")
}
}
func TestTypeCache_HandlerKeys(t *testing.T) {
cache := newTypeCache()
type Sample struct {
Name string `json:"name" validate:"required"`
Email string `validate:"email"`
Age int `json:"age"`
}
st := reflect.TypeOf(Sample{})
meta := cache.get(st, []string{"json", "validate"}, nil)
// Name has both json and validate
if len(meta.Fields[0].HandlerKeys) != 2 {
t.Errorf("Name HandlerKeys = %v, want 2 keys", meta.Fields[0].HandlerKeys)
}
// Email has only validate
if len(meta.Fields[1].HandlerKeys) != 1 || meta.Fields[1].HandlerKeys[0] != "validate" {
t.Errorf("Email HandlerKeys = %v, want [validate]", meta.Fields[1].HandlerKeys)
}
// Age has only json
if len(meta.Fields[2].HandlerKeys) != 1 || meta.Fields[2].HandlerKeys[0] != "json" {
t.Errorf("Age HandlerKeys = %v, want [json]", meta.Fields[2].HandlerKeys)
}
}
func TestTypeCache_Clear(t *testing.T) {
cache := newTypeCache()
type Sample struct {
Name string `json:"name"`
}
st := reflect.TypeOf(Sample{})
cache.get(st, []string{"json"}, nil)
cache.clear()
// After clear, should rebuild
meta := cache.get(st, []string{}, nil)
if meta == nil {
t.Fatal("expected non-nil meta after clear")
}
// With no handler keys, no fields should have HandlerKeys
if len(meta.Fields[0].HandlerKeys) != 0 {
t.Errorf("expected no HandlerKeys after clear with empty keys")
}
}
func TestTypeCache_Invalidate(t *testing.T) {
cache := newTypeCache()
type Sample struct {
Name string `json:"name"`
}
st := reflect.TypeOf(Sample{})
meta1 := cache.get(st, []string{"json"}, nil)
cache.invalidate(st)
meta2 := cache.get(st, []string{"json"}, nil)
if meta1 == meta2 {
t.Error("expected different pointer after invalidate")
}
}