Skip to content

Commit 312e06a

Browse files
authored
Merge pull request #3 from go-waitfor/copilot/fix-f4ba3991-b30b-49c4-a436-2cbba5bb19db
Improve code coverage from 13.9% to 100%
2 parents 6e9e047 + d39d1c0 commit 312e06a

4 files changed

Lines changed: 542 additions & 0 deletions

File tree

helpers_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package waitfor
2+
3+
import (
4+
"net/url"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestUse(t *testing.T) {
11+
// Create a mock module function
12+
mockModule := func() ([]string, ResourceFactory) {
13+
schemes := []string{"mock", "test"}
14+
factory := func(_ *url.URL) (Resource, error) {
15+
return &TestResource{}, nil
16+
}
17+
return schemes, factory
18+
}
19+
20+
// Test Use function
21+
config := Use(mockModule)
22+
23+
assert.Equal(t, []string{"mock", "test"}, config.Scheme)
24+
assert.NotNil(t, config.Factory)
25+
26+
// Test that the factory works
27+
testURL, _ := url.Parse("mock://example")
28+
resource, err := config.Factory(testURL)
29+
assert.NoError(t, err)
30+
assert.NotNil(t, resource)
31+
}
32+
33+
func TestUse_WithEmptySchemes(t *testing.T) {
34+
// Create a module with empty schemes
35+
mockModule := func() ([]string, ResourceFactory) {
36+
schemes := []string{}
37+
factory := func(_ *url.URL) (Resource, error) {
38+
return &TestResource{}, nil
39+
}
40+
return schemes, factory
41+
}
42+
43+
config := Use(mockModule)
44+
45+
assert.Empty(t, config.Scheme)
46+
assert.NotNil(t, config.Factory)
47+
}
48+
49+
func TestUse_WithNilFactory(t *testing.T) {
50+
// Create a module with nil factory
51+
mockModule := func() ([]string, ResourceFactory) {
52+
schemes := []string{"nil"}
53+
return schemes, nil
54+
}
55+
56+
config := Use(mockModule)
57+
58+
assert.Equal(t, []string{"nil"}, config.Scheme)
59+
assert.Nil(t, config.Factory)
60+
}

options_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package waitfor
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewOptions_Defaults(t *testing.T) {
11+
opts := newOptions([]Option{})
12+
13+
assert.Equal(t, time.Duration(5)*time.Second, opts.interval)
14+
assert.Equal(t, time.Duration(60)*time.Second, opts.maxInterval)
15+
assert.Equal(t, uint64(5), opts.attempts)
16+
}
17+
18+
func TestNewOptions_WithSetters(t *testing.T) {
19+
setters := []Option{
20+
WithInterval(10),
21+
WithMaxInterval(120),
22+
WithAttempts(15),
23+
}
24+
25+
opts := newOptions(setters)
26+
27+
assert.Equal(t, time.Duration(10)*time.Second, opts.interval)
28+
assert.Equal(t, time.Duration(120)*time.Second, opts.maxInterval)
29+
assert.Equal(t, uint64(15), opts.attempts)
30+
}
31+
32+
func TestWithInterval(t *testing.T) {
33+
option := WithInterval(30)
34+
opts := &Options{}
35+
36+
option(opts)
37+
38+
assert.Equal(t, time.Duration(30)*time.Second, opts.interval)
39+
}
40+
41+
func TestWithMaxInterval(t *testing.T) {
42+
option := WithMaxInterval(90)
43+
opts := &Options{}
44+
45+
option(opts)
46+
47+
assert.Equal(t, time.Duration(90)*time.Second, opts.maxInterval)
48+
}
49+
50+
func TestWithAttempts(t *testing.T) {
51+
option := WithAttempts(20)
52+
opts := &Options{}
53+
54+
option(opts)
55+
56+
assert.Equal(t, uint64(20), opts.attempts)
57+
}
58+
59+
func TestCombinedOptions(t *testing.T) {
60+
opts := newOptions([]Option{
61+
WithInterval(2),
62+
WithMaxInterval(30),
63+
WithAttempts(8),
64+
})
65+
66+
assert.Equal(t, time.Duration(2)*time.Second, opts.interval)
67+
assert.Equal(t, time.Duration(30)*time.Second, opts.maxInterval)
68+
assert.Equal(t, uint64(8), opts.attempts)
69+
}

registry_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package waitfor
22

33
import (
44
"context"
5+
"errors"
56
"net/url"
67
"testing"
78

@@ -36,3 +37,117 @@ func TestRegistry_Register(t *testing.T) {
3637
assert.NoError(t, err)
3738
assert.NotNilf(t, rsc, "resource not found")
3839
}
40+
41+
func TestRegistry_Register_NewScheme(t *testing.T) {
42+
r := newRegistry([]ResourceConfig{})
43+
44+
factory := func(_ *url.URL) (Resource, error) {
45+
return &TestResource{}, nil
46+
}
47+
48+
err := r.Register("custom", factory)
49+
assert.NoError(t, err)
50+
51+
// Verify the scheme was registered
52+
rsc, err := r.Resolve("custom://test")
53+
assert.NoError(t, err)
54+
assert.NotNil(t, rsc)
55+
}
56+
57+
func TestRegistry_Register_DuplicateScheme(t *testing.T) {
58+
factory := func(_ *url.URL) (Resource, error) {
59+
return &TestResource{}, nil
60+
}
61+
62+
r := newRegistry([]ResourceConfig{
63+
{
64+
Scheme: []string{"existing"},
65+
Factory: factory,
66+
},
67+
})
68+
69+
// Try to register the same scheme again
70+
err := r.Register("existing", factory)
71+
assert.Error(t, err)
72+
assert.Contains(t, err.Error(), "resource is already registered with a given scheme:")
73+
}
74+
75+
func TestRegistry_Register_WithWhitespace(t *testing.T) {
76+
r := newRegistry([]ResourceConfig{})
77+
78+
factory := func(_ *url.URL) (Resource, error) {
79+
return &TestResource{}, nil
80+
}
81+
82+
// Register with whitespace - should be trimmed
83+
err := r.Register(" spaced ", factory)
84+
assert.NoError(t, err)
85+
86+
// Verify it was registered with trimmed name
87+
rsc, err := r.Resolve("spaced://test")
88+
assert.NoError(t, err)
89+
assert.NotNil(t, rsc)
90+
}
91+
92+
func TestRegistry_Resolve_InvalidURL(t *testing.T) {
93+
r := newRegistry([]ResourceConfig{})
94+
95+
// Test with invalid URL
96+
rsc, err := r.Resolve("://invalid-url")
97+
assert.Error(t, err)
98+
assert.Nil(t, rsc)
99+
}
100+
101+
func TestRegistry_Resolve_UnknownScheme(t *testing.T) {
102+
r := newRegistry([]ResourceConfig{})
103+
104+
// Test with unknown scheme
105+
rsc, err := r.Resolve("unknown://test")
106+
assert.Error(t, err)
107+
assert.Nil(t, rsc)
108+
assert.Contains(t, err.Error(), "resource with a given scheme is not found:")
109+
}
110+
111+
func TestRegistry_Resolve_FactoryError(t *testing.T) {
112+
factory := func(_ *url.URL) (Resource, error) {
113+
return nil, errors.New("factory error")
114+
}
115+
116+
r := newRegistry([]ResourceConfig{
117+
{
118+
Scheme: []string{"error"},
119+
Factory: factory,
120+
},
121+
})
122+
123+
rsc, err := r.Resolve("error://test")
124+
assert.Error(t, err)
125+
assert.Nil(t, rsc)
126+
assert.Contains(t, err.Error(), "factory error")
127+
}
128+
129+
func TestRegistry_List(t *testing.T) {
130+
factory := func(_ *url.URL) (Resource, error) {
131+
return &TestResource{}, nil
132+
}
133+
134+
r := newRegistry([]ResourceConfig{
135+
{
136+
Scheme: []string{"http", "https", "custom"},
137+
Factory: factory,
138+
},
139+
})
140+
141+
schemes := r.List()
142+
assert.Len(t, schemes, 3)
143+
assert.Contains(t, schemes, "http")
144+
assert.Contains(t, schemes, "https")
145+
assert.Contains(t, schemes, "custom")
146+
}
147+
148+
func TestRegistry_List_Empty(t *testing.T) {
149+
r := newRegistry([]ResourceConfig{})
150+
151+
schemes := r.List()
152+
assert.Empty(t, schemes)
153+
}

0 commit comments

Comments
 (0)