Skip to content

Commit 80d1102

Browse files
committed
refactor: example and log
1 parent a676dee commit 80d1102

7 files changed

+56
-21
lines changed

example_test.go

+38-7
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"github.com/go-kod/kod/interceptor/kmetric"
1212
"github.com/go-kod/kod/interceptor/krecovery"
1313
"github.com/go-kod/kod/interceptor/ktrace"
14-
"github.com/go-kod/kod/internal/kslog"
1514
"go.uber.org/mock/gomock"
1615
)
1716

17+
// This example demonstrates how to use [kod.Run] and [kod.Implements] to run a simple application.
1818
func Example_mainComponent() {
1919
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
2020
fmt.Println("Hello, World!")
@@ -26,6 +26,7 @@ func Example_mainComponent() {
2626
// helloWorld shutdown
2727
}
2828

29+
// This example demonstrates how to use [kod.Ref] to reference a component and call a method on it.
2930
func Example_componentRefAndCall() {
3031
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
3132
app.HelloWorld.Get().SayHello(ctx)
@@ -37,6 +38,7 @@ func Example_componentRefAndCall() {
3738
// helloWorld shutdown
3839
}
3940

41+
// This example demonstrates how to use [kod.LazyInit] to defer component initialization until it is needed.
4042
func Example_componentLazyInit() {
4143
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
4244
app.HelloLazy.Get().SayHello(ctx)
@@ -52,6 +54,7 @@ func Example_componentLazyInit() {
5254
// helloWorld shutdown
5355
}
5456

57+
// This example demonstrates how to use [kod.WithFakes] and [kod.Fake] to provide a mock implementation of a component.
5558
func Example_componentMock() {
5659
mock := helloworld.NewMockHelloWorld(gomock.NewController(nil))
5760
mock.EXPECT().SayHello(gomock.Any()).Return()
@@ -65,21 +68,33 @@ func Example_componentMock() {
6568
// Nothing printed from mock
6669
}
6770

71+
// This example demonstrates how to use [kod.WithConfig] to provide a configuration to the application.
6872
func Example_config() {
6973
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
70-
fmt.Println(app.Config().Name)
7174
app.HelloWorld.Get().SayHello(ctx)
7275
return nil
7376
}, kod.WithConfigFile("./examples/helloworld/config.toml"))
7477
// Output:
7578
// helloWorld init
76-
// globalConfig
7779
// Hello, World!config
7880
// helloWorld shutdown
7981
}
8082

83+
// This example demonstrates how to use [kod.WithGlobalConfig] to provide a global configuration to the application.
84+
func Example_configGlobal() {
85+
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
86+
fmt.Println(app.Config().Name)
87+
return nil
88+
}, kod.WithConfigFile("./examples/helloworld/config.toml"))
89+
// Output:
90+
// helloWorld init
91+
// globalConfig
92+
// helloWorld shutdown
93+
}
94+
95+
// This example demonstrates how to use [kod.WithLogger] to provide a custom logger to the application.
8196
func Example_log() {
82-
logger, observer := kslog.NewTestLogger()
97+
logger, observer := kod.NewTestLogger()
8398

8499
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
85100
app.L(ctx).Debug("Hello, World!")
@@ -100,6 +115,7 @@ func Example_log() {
100115
// {"level":"INFO","msg":"Hello, World!","component":"github.com/go-kod/kod/examples/helloworld/HelloWorld"}
101116
}
102117

118+
// This example demonstrates how to use [kod.WithInterceptors] to provide a custom interceptor to the application.
103119
func Example_interceptor() {
104120
interceptor := interceptor.Interceptor(func(ctx context.Context, info interceptor.CallInfo, req, res []interface{}, next interceptor.HandleFunc) error {
105121
fmt.Println("Before call")
@@ -120,6 +136,8 @@ func Example_interceptor() {
120136
// helloWorld shutdown
121137
}
122138

139+
// This example demonstrates how to use built-in interceptors
140+
// Such as [krecovery.Interceptor], [ktrace.Interceptor], and [kmetric.Interceptor] ...
123141
func Example_interceptorBuiltin() {
124142
kod.Run(context.Background(), func(ctx context.Context, app *helloworld.App) error {
125143
app.HelloWorld.Get().SayHello(ctx)
@@ -131,6 +149,7 @@ func Example_interceptorBuiltin() {
131149
// helloWorld shutdown
132150
}
133151

152+
// This example demonstrates how to use [kod.RunTest] to run a test function.
134153
func Example_test() {
135154
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
136155
app.HelloWorld.Get().SayHello(ctx)
@@ -141,6 +160,7 @@ func Example_test() {
141160
// helloWorld shutdown
142161
}
143162

163+
// This example demonstrates how to use [kod.RunTest], [kod.Fake] and [kod.WithFakes] to run a test function with a mock component.
144164
func Example_testWithMockComponent() {
145165
mock := helloworld.NewMockHelloWorld(gomock.NewController(nil))
146166
mock.EXPECT().SayHello(gomock.Any()).Return()
@@ -153,6 +173,7 @@ func Example_testWithMockComponent() {
153173
// Nothing printed from mock
154174
}
155175

176+
// This example demonstrates how to use [kod.RunTest] and [kod.WithConfigFile] to run a test function with a configuration.
156177
func Example_testWithConfig() {
157178
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
158179
fmt.Println(app.Config().Name)
@@ -165,16 +186,26 @@ func Example_testWithConfig() {
165186
// helloWorld shutdown
166187
}
167188

168-
// Example_testWithLogObserver demonstrates how to test log output.
189+
// This example demonstrates how to use [kod.RunTest], [kod.NewTestLogger] and [kod.WithLogger] to run a test function with a custom logger.
169190
func Example_testWithLogObserver() {
170-
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
191+
logger, observer := kod.NewTestLogger()
192+
193+
t := &testing.T{}
194+
kod.RunTest(t, func(ctx context.Context, app *helloworld.App) {
171195
app.L(ctx).Debug("Hello, World!")
172196
app.L(ctx).Info("Hello, World!")
173197
app.L(ctx).Warn("Hello, World!")
174198
app.L(ctx).Error("Hello, World!")
175-
})
199+
}, kod.WithLogger(logger))
200+
201+
fmt.Println(observer.Len())
202+
fmt.Println(observer.ErrorCount())
203+
fmt.Println(observer.Clean().Len())
176204

177205
// Output:
178206
// helloWorld init
179207
// helloWorld shutdown
208+
// 3
209+
// 1
210+
// 0
180211
}

internal/kslog/log_observer.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"encoding/json"
66
"log/slog"
77
"strings"
8+
9+
"github.com/samber/lo"
810
)
911

1012
// removeTime removes the top-level time attribute.
@@ -31,9 +33,7 @@ func (b *observer) parse() []map[string]any {
3133
}
3234

3335
var m map[string]any
34-
if err := json.Unmarshal([]byte(line), &m); err != nil {
35-
panic(err)
36-
}
36+
lo.Must0(json.Unmarshal([]byte(line), &m))
3737

3838
data = append(data, m)
3939
}
@@ -69,9 +69,7 @@ func (b *observer) Filter(filter func(map[string]any) bool) *observer {
6969

7070
buf := new(bytes.Buffer)
7171
for _, line := range filtered {
72-
if err := json.NewEncoder(buf).Encode(line); err != nil {
73-
panic(err)
74-
}
72+
lo.Must0(json.NewEncoder(buf).Encode(line))
7573
}
7674

7775
return &observer{

testing.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import (
1111
"github.com/go-kod/kod/internal/kslog"
1212
)
1313

14-
var NewTestObserver = kslog.NewTestLogger
14+
// NewTestLogger returns a new test logger.
15+
var NewTestLogger = kslog.NewTestLogger
1516

17+
// fakeComponent is a fake component.
1618
type fakeComponent struct {
1719
intf reflect.Type
1820
impl any
1921
}
2022

23+
// Fake returns a fake component.
2124
func Fake[T any](impl any) fakeComponent {
2225
t := reflect.TypeFor[T]()
2326
if _, ok := impl.(T); !ok {
@@ -26,6 +29,7 @@ func Fake[T any](impl any) fakeComponent {
2629
return fakeComponent{intf: t, impl: impl}
2730
}
2831

32+
// options contains options for the runner.
2933
type runner struct {
3034
options []func(*options)
3135
}
@@ -51,6 +55,7 @@ func RunTest3[T1, T2, T3 any](tb testing.TB, body func(context.Context, T1, T2,
5155
runTest(tb, body, opts...)
5256
}
5357

58+
// runTest runs a test function.
5459
func runTest(tb testing.TB, testBody any, opts ...func(*options)) {
5560
tb.Helper()
5661

@@ -61,6 +66,7 @@ func runTest(tb testing.TB, testBody any, opts ...func(*options)) {
6166
}
6267
}
6368

69+
// sub runs a test function.
6470
func (r runner) sub(tb testing.TB, testBody any) error {
6571
tb.Helper()
6672

tests/case1/case_lazy_init_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestLazyInit(t *testing.T) {
13-
log, observer := kod.NewTestObserver()
13+
log, observer := kod.NewTestLogger()
1414

1515
kod.RunTest(t, func(ctx context.Context, k *lazyInitImpl) {
1616
require.Equal(t, 1, observer.Len(), observer.String())
@@ -28,7 +28,7 @@ func TestLazyInit(t *testing.T) {
2828
}
2929

3030
func TestLazyInitTest(t *testing.T) {
31-
log, observer := kod.NewTestObserver()
31+
log, observer := kod.NewTestLogger()
3232

3333
kod.RunTest2(t, func(ctx context.Context, k *lazyInitImpl, comp *lazyInitComponent) {
3434
k.Try(ctx)
@@ -42,7 +42,7 @@ func TestLazyInitTest(t *testing.T) {
4242
}
4343

4444
func TestLazyInitTest2(t *testing.T) {
45-
log, observer := kod.NewTestObserver()
45+
log, observer := kod.NewTestLogger()
4646

4747
kod.RunTest2(t, func(ctx context.Context, k LazyInitImpl, comp LazyInitComponent) {
4848
require.Equal(t, 2, observer.Len(), observer.String())
@@ -54,7 +54,7 @@ func TestLazyInitTest2(t *testing.T) {
5454
}
5555

5656
func TestLazyInitTest3(t *testing.T) {
57-
log, observer := kod.NewTestObserver()
57+
log, observer := kod.NewTestLogger()
5858

5959
kod.RunTest2(t, func(ctx context.Context, k *lazyInitImpl, comp LazyInitComponent) {
6060
k.Try(ctx)

tests/case1/case_log_file_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
func TestLogFile(t *testing.T) {
16-
log, observer := kod.NewTestObserver()
16+
log, observer := kod.NewTestLogger()
1717

1818
kod.RunTest(t, func(ctx context.Context, k Test1Component) {
1919
_, err := k.Foo(ctx, &FooReq{Id: 1})

tests/case1/case_log_level_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func TestLogLevel(t *testing.T) {
13-
log, observer := kod.NewTestObserver()
13+
log, observer := kod.NewTestLogger()
1414

1515
kod.RunTest(t, func(ctx context.Context, k *test1Component) {
1616
observer.Clean()

tests/case1/case_log_mock_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
func TestMockLog(t *testing.T) {
14-
log, observer := kod.NewTestObserver()
14+
log, observer := kod.NewTestLogger()
1515
t.Setenv("KOD_LOG_LEVEL", "error")
1616

1717
kod.RunTest(t, func(ctx context.Context, k Test1Component) {

0 commit comments

Comments
 (0)