-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions_test.go
More file actions
162 lines (122 loc) · 3.88 KB
/
Copy pathoptions_test.go
File metadata and controls
162 lines (122 loc) · 3.88 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
package lsremote
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hiddeco/go-ls-remote/trace"
"github.com/hiddeco/go-ls-remote/transport"
)
// recordingTracer is a no-op [trace.Tracer] used only to verify that
// [WithTracer] stores the exact value it was handed.
type recordingTracer struct{}
func (recordingTracer) OnPacketEvent(*trace.PacketEvent) {}
func (recordingTracer) OnEvent(trace.Event) {}
func TestDialConfigZeroValue(t *testing.T) {
t.Parallel()
var c dialConfig
assert.Nil(t, c.registry,
"zero-value registry is nil so Dial can fall back to defaults")
assert.Nil(t, c.tracer,
"zero-value tracer is nil so emission sites short-circuit")
assert.Empty(t, c.userAgent,
"zero-value userAgent is empty so the transport default applies")
assert.Nil(t, c.protocol,
"zero-value protocol is nil so negotiation is automatic")
}
func TestWithTransports(t *testing.T) {
t.Parallel()
reg := transport.NewRegistry()
var c dialConfig
WithTransports(reg).applyDial(&c)
assert.Same(t, reg, c.registry,
"WithTransports records the supplied Registry pointer verbatim")
}
func TestWithTransports_Nil(t *testing.T) {
t.Parallel()
var c dialConfig
WithTransports(nil).applyDial(&c)
assert.Nil(t, c.registry,
"WithTransports(nil) leaves registry nil so Dial-time defaulting kicks in")
}
func TestWithTracer(t *testing.T) {
t.Parallel()
tr := recordingTracer{}
var c dialConfig
WithTracer(tr).applyDial(&c)
assert.Equal(t, tr, c.tracer,
"WithTracer records the supplied Tracer value")
}
func TestWithTracer_Nil(t *testing.T) {
t.Parallel()
var c dialConfig
WithTracer(nil).applyDial(&c)
assert.Nil(t, c.tracer,
"WithTracer(nil) leaves the tracer nil so tracing stays disabled")
}
func TestWithUserAgent(t *testing.T) {
t.Parallel()
var c dialConfig
WithUserAgent("foo/1.0").applyDial(&c)
assert.Equal(t, "foo/1.0", c.userAgent)
}
func TestWithUserAgent_Empty(t *testing.T) {
t.Parallel()
var c dialConfig
WithUserAgent("").applyDial(&c)
assert.Empty(t, c.userAgent,
"WithUserAgent(\"\") leaves the field empty so transport default applies")
}
func TestWithProtocol(t *testing.T) {
t.Parallel()
var c dialConfig
WithProtocol(ProtocolV2).applyDial(&c)
require.NotNil(t, c.protocol,
"WithProtocol stores a non-nil pointer so callers can distinguish "+
"\"pin to v0\" from \"auto-negotiate\"")
assert.Equal(t, ProtocolV2, *c.protocol)
}
func TestWithProtocol_V0(t *testing.T) {
t.Parallel()
// Verify that pinning ProtocolV0 (the zero value of the integer
// type) round-trips through the pointer correctly — the whole reason
// dialConfig.protocol is a pointer rather than a plain value.
var c dialConfig
WithProtocol(ProtocolV0).applyDial(&c)
require.NotNil(t, c.protocol)
assert.Equal(t, ProtocolV0, *c.protocol)
}
func TestOptionComposition(t *testing.T) {
t.Parallel()
// Apply every option to a single config and confirm each took
// effect — the realistic call shape inside Dial.
reg := transport.NewRegistry()
tr := recordingTracer{}
var c dialConfig
opts := []Option{
WithTransports(reg),
WithTracer(tr),
WithUserAgent("ua/2.0"),
WithProtocol(ProtocolV2),
}
for _, o := range opts {
o.applyDial(&c)
}
assert.Same(t, reg, c.registry)
assert.Equal(t, tr, c.tracer)
assert.Equal(t, "ua/2.0", c.userAgent)
require.NotNil(t, c.protocol)
assert.Equal(t, ProtocolV2, *c.protocol)
}
func TestWithProtocol_IndependentPointers(t *testing.T) {
t.Parallel()
// Two separate WithProtocol calls must produce independent storage
// — re-applying WithProtocol to a fresh config must not see writes
// to a prior config's pointer.
var a, b dialConfig
WithProtocol(ProtocolV2).applyDial(&a)
WithProtocol(ProtocolV0).applyDial(&b)
require.NotNil(t, a.protocol)
require.NotNil(t, b.protocol)
assert.Equal(t, ProtocolV2, *a.protocol)
assert.Equal(t, ProtocolV0, *b.protocol)
}