-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods_test.go
More file actions
214 lines (186 loc) · 4.75 KB
/
methods_test.go
File metadata and controls
214 lines (186 loc) · 4.75 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
package dscope
import (
"fmt"
"io"
"strings"
"testing"
)
func TestMethods(t *testing.T) {
s := New(Methods(new(TestMethodsFoo))...)
s.Call(func(
foo int,
) {
if foo != 42 {
t.Fatal()
}
})
}
type TestMethodsFoo struct {
Module
}
func (TestMethodsFoo) Foo() int {
return 42
}
func TestMethodFromFields(t *testing.T) {
type Foo struct {
Foo TestMethodsFoo
}
defs := Methods(new(Foo))
if len(defs) == 0 {
t.Fatal()
}
}
func TestMethodsNil(t *testing.T) {
t.Run("nil typed", func(t *testing.T) {
type M struct {
Module
}
Methods((*M)(nil))
})
t.Run("nil pointer to interface", func(t *testing.T) {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
if !strings.Contains(msg, "nil pointer to interface *io.Reader") {
t.Fatalf("got %s", msg)
}
}()
Methods((*io.Reader)(nil))
})
t.Run("nil interface", func(t *testing.T) {
func() {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
msg := fmt.Sprintf("%v", p)
if !strings.Contains(msg, "invalid value") {
t.Fatalf("got %s", msg)
}
}()
Methods((io.Reader)(nil))
}()
})
}
type testMethodsValueMod struct {
Module
}
func (testMethodsValueMod) Value() int64 { return 1 }
func (*testMethodsValueMod) Pointer() int32 { return 2 }
type testMethodsContainer struct {
Module
V testMethodsValueMod
}
func TestMethodsEmbeddedValue(t *testing.T) {
// This test ensures that we can discover methods on the pointer receiver
// of a module embedded by value.
scope := New(Methods(&testMethodsContainer{})...)
// Should find Value() (int64)
if v := Get[int64](scope); v != 1 {
t.Fatalf("expected 1, got %d", v)
}
// Should find Pointer() (int32)
// Before fix, this fails because we only visit testMethodsValueMod as a value
if v := Get[int32](scope); v != 2 {
t.Fatalf("expected 2, got %d", v)
}
}
func TestMethodsDoublePointer(t *testing.T) {
// This test verifies that we can extract methods from a pointer to a pointer
// e.g. passing **T should find methods defined on *T
m := &testMethodsValueMod{}
scope := New(Methods(&m)...)
// Should find Value() (int64) from T
if v := Get[int64](scope); v != 1 {
t.Fatalf("expected 1, got %d", v)
}
// Should find Pointer() (int32) from *T
if v := Get[int32](scope); v != 2 {
t.Fatalf("expected 2, got %d", v)
}
}
func TestMethodsMultiPointerNil(t *testing.T) {
// This test verifies that Methods handles multi-level nil pointers without panicking.
// e.g., passing **T(nil) should work.
defs := Methods((**TestMethodsFoo)(nil))
if len(defs) == 0 {
t.Fatal("no methods found for multi-level nil pointer")
}
}
func TestMethodsEmbeddedValuePassedByValue(t *testing.T) {
// This test verifies that we can capture methods on pointer receivers
// of embedded modules even when the parent struct is passed by value (non-addressable).
m := testMethodsContainer{
V: testMethodsValueMod{},
}
// Pass by value
scope := New(Methods(m)...)
// Should find Pointer() (int32) from *testMethodsValueMod
// This would fail if we didn't create an addressable copy of the embedded field
if v := Get[int32](scope); v != 2 {
t.Fatalf("expected 2, got %d", v)
}
// Should find Value() (int64) from testMethodsValueMod
if v := Get[int64](scope); v != 1 {
t.Fatalf("expected 1, got %d", v)
}
}
type testMethodsRootValue struct {
Module
}
func (testMethodsRootValue) Value() int64 { return 1 }
func (*testMethodsRootValue) Pointer() int32 { return 2 }
func TestMethodsRootValueAddressability(t *testing.T) {
m := testMethodsRootValue{}
// Passing by value
scope := New(Methods(m)...)
// Should find Value() -> int64
if v := Get[int64](scope); v != 1 {
t.Fatalf("expected 1, got %d", v)
}
// Should find Pointer() -> int32
// Without fix, this fails to find the provider for int32
if v := Get[int32](scope); v != 2 {
t.Fatalf("expected 2, got %d", v)
}
}
func TestMethodsMultiLevelNilInterface(t *testing.T) {
t.Run("nil double pointer to interface", func(t *testing.T) {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
err, ok := p.(error)
if !ok {
t.Fatalf("panic value not an error: %v", p)
}
msg := err.Error()
if !strings.Contains(msg, "nil pointer to interface **io.Reader") {
t.Fatalf("got %s", msg)
}
}()
Methods((**io.Reader)(nil))
})
t.Run("nil triple pointer to interface", func(t *testing.T) {
defer func() {
p := recover()
if p == nil {
t.Fatal("should panic")
}
err, ok := p.(error)
if !ok {
t.Fatalf("panic value not an error: %v", p)
}
msg := err.Error()
if !strings.Contains(msg, "nil pointer to interface ***io.Reader") {
t.Fatalf("got %s", msg)
}
}()
Methods((***io.Reader)(nil))
})
}