Skip to content

Commit 04d0fe8

Browse files
committed
chore: improve test case
1 parent ebef526 commit 04d0fe8

File tree

4 files changed

+119
-7
lines changed

4 files changed

+119
-7
lines changed

kod_test.go

+100
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package kod
33
import (
44
"context"
55
"testing"
6+
"time"
67

8+
"github.com/knadh/koanf/v2"
79
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
811
"go.uber.org/goleak"
912
)
1013

@@ -51,3 +54,100 @@ func TestConfigEnv(t *testing.T) {
5154
assert.Equal(t, k.config.Version, "1.0.0")
5255
assert.Equal(t, k.config.Env, "dev")
5356
}
57+
58+
type testComponent struct {
59+
Implements[testInterface]
60+
WithConfig[testConfig]
61+
initialized bool
62+
initErr error
63+
shutdown bool
64+
shutdownErr error
65+
}
66+
67+
type testConfig struct {
68+
Value string `default:"default"`
69+
}
70+
71+
type testInterface interface {
72+
IsInitialized() bool
73+
}
74+
75+
func (c *testComponent) Init(context.Context) error {
76+
c.initialized = true
77+
return c.initErr
78+
}
79+
80+
func (c *testComponent) Shutdown(context.Context) error {
81+
c.shutdown = true
82+
return c.shutdownErr
83+
}
84+
85+
func (c *testComponent) IsInitialized() bool {
86+
return c.initialized
87+
}
88+
89+
func (c *testComponent) implements(testInterface) {}
90+
91+
func TestConfigurationLoading(t *testing.T) {
92+
tests := []struct {
93+
name string
94+
koanf *koanf.Koanf
95+
filename string
96+
wantErr bool
97+
}{
98+
{
99+
name: "custom koanf",
100+
koanf: koanf.New("."), // 使用 koanf.New() 替代空实例
101+
},
102+
{
103+
name: "invalid file extension",
104+
filename: "config.invalid",
105+
wantErr: true,
106+
},
107+
{
108+
name: "missing file",
109+
filename: "notexist.yaml",
110+
wantErr: true, // Should use defaults
111+
},
112+
}
113+
114+
for _, tt := range tests {
115+
t.Run(tt.name, func(t *testing.T) {
116+
opts := []func(*options){
117+
WithConfigFile(tt.filename),
118+
}
119+
if tt.koanf != nil {
120+
opts = append(opts, WithKoanf(tt.koanf))
121+
}
122+
123+
k, err := newKod(context.Background(), opts...)
124+
if tt.wantErr {
125+
assert.Error(t, err)
126+
return
127+
}
128+
require.NoError(t, err)
129+
130+
cfg := k.Config()
131+
assert.NotEmpty(t, cfg.Name)
132+
assert.NotEmpty(t, cfg.Env)
133+
assert.Equal(t, 5*time.Second, cfg.ShutdownTimeout)
134+
})
135+
}
136+
}
137+
138+
func TestDeferHooks(t *testing.T) {
139+
k, err := newKod(context.Background())
140+
require.NoError(t, err)
141+
142+
executed := false
143+
k.Defer("test", func(context.Context) error {
144+
executed = true
145+
return nil
146+
})
147+
148+
ctx, cancel := context.WithCancel(context.Background())
149+
defer cancel()
150+
151+
k.hooker.Do(ctx)
152+
assert.True(t, executed)
153+
}

registry_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package kod
22

33
import (
4+
"context"
45
"io"
56
"reflect"
67
"strings"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
1012

1113
"github.com/go-kod/kod/internal/registry"
1214
)
@@ -104,3 +106,19 @@ func TestCycleRegistrations(t *testing.T) {
104106
t.Fatalf("checkCircularDependency: got %q, want %q", err, want)
105107
}
106108
}
109+
110+
func TestGetImpl(t *testing.T) {
111+
k, err := newKod(context.Background())
112+
require.NoError(t, err)
113+
114+
_, err = k.getImpl(context.Background(), reflect.TypeOf(struct{}{}))
115+
assert.Error(t, err) // Should fail for unregistered type
116+
}
117+
118+
func TestGetIntf(t *testing.T) {
119+
k, err := newKod(context.Background())
120+
require.NoError(t, err)
121+
122+
_, err = k.getIntf(context.Background(), reflect.TypeOf((*interface{})(nil)).Elem())
123+
assert.Error(t, err) // Should fail for unregistered interface
124+
}

testing.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func runTest(tb testing.TB, testBody any, opts ...func(*options)) {
6161

6262
err := runner{options: opts}.sub(tb, testBody)
6363
if err != nil {
64-
tb.Logf("runTest failed: %v", err)
64+
tb.Errorf("runTest failed: %v", err)
6565
tb.FailNow()
6666
}
6767
}

testing_test.go

-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ import (
1010
"github.com/go-kod/kod/internal/mock"
1111
)
1212

13-
type testComponent struct {
14-
Implements[testInterface]
15-
}
16-
17-
type testInterface interface{}
18-
1913
func Test_testRunner_sub(t *testing.T) {
2014
t.Run("failure", func(t *testing.T) {
2115
mock.ExpectFailure(t, func(tb testing.TB) {

0 commit comments

Comments
 (0)