@@ -11,10 +11,11 @@ import (
11
11
"github.com/go-kod/kod/interceptor/kmetric"
12
12
"github.com/go-kod/kod/interceptor/krecovery"
13
13
"github.com/go-kod/kod/interceptor/ktrace"
14
+ "github.com/go-kod/kod/internal/kslog"
14
15
"go.uber.org/mock/gomock"
15
16
)
16
17
17
- func Example_helloWorld () {
18
+ func Example_mainComponent () {
18
19
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
19
20
fmt .Println ("Hello, World!" )
20
21
return nil
@@ -25,7 +26,7 @@ func Example_helloWorld() {
25
26
// helloWorld shutdown
26
27
}
27
28
28
- func Example_callComponent () {
29
+ func Example_componentRefAndCall () {
29
30
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
30
31
app .HelloWorld .Get ().SayHello (ctx )
31
32
return nil
@@ -36,7 +37,22 @@ func Example_callComponent() {
36
37
// helloWorld shutdown
37
38
}
38
39
39
- func Example_mockComponent () {
40
+ func Example_componentLazyInit () {
41
+ kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
42
+ app .HelloLazy .Get ().SayHello (ctx )
43
+ app .HelloWorld .Get ().SayHello (ctx )
44
+ return nil
45
+ })
46
+ // Output:
47
+ // helloWorld init
48
+ // lazyHelloBob init
49
+ // Hello, Bob!
50
+ // Hello, World!
51
+ // lazyHelloBob shutdown
52
+ // helloWorld shutdown
53
+ }
54
+
55
+ func Example_componentMock () {
40
56
mock := helloworld .NewMockHelloWorld (gomock .NewController (nil ))
41
57
mock .EXPECT ().SayHello (gomock .Any ()).Return ()
42
58
@@ -63,28 +79,25 @@ func Example_config() {
63
79
}
64
80
65
81
func Example_log () {
66
- wrapper , observer := kod . NewLogObserver ()
82
+ logger , observer := kslog . NewTestLogger ()
67
83
68
- kod .Run ( context . Background () , func (ctx context.Context , app * helloworld.App ) error {
84
+ kod .RunTest ( & testing. T {} , func (ctx context.Context , app * helloworld.App ) {
69
85
app .L (ctx ).Debug ("Hello, World!" )
70
86
app .L (ctx ).Info ("Hello, World!" )
71
87
app .L (ctx ).Warn ("Hello, World!" )
72
88
app .L (ctx ).Error ("Hello, World!" )
73
- return nil
74
- }, kod .WithLogWrapper (wrapper ))
75
-
76
- fmt .Println (observer .Len ())
77
- for _ , entry := range observer .All () {
78
- fmt .Println (entry .Level , entry .Message )
79
- }
89
+ app .HelloWorld .Get ().SayHello (ctx )
90
+ }, kod .WithLogger (logger ))
80
91
92
+ fmt .Println (observer )
81
93
// Output:
82
94
// helloWorld init
95
+ // Hello, World!
83
96
// helloWorld shutdown
84
- // 3
85
- // INFO Hello, World!
86
- // WARN Hello, World!
87
- // ERROR Hello, World!
97
+ // {"level":"INFO","msg":"Hello, World!","component":"github.com/go-kod/kod/Main"}
98
+ // {"level":"WARN","msg":" Hello, World!","component":"github.com/go-kod/kod/Main"}
99
+ // {"level":"ERROR","msg":" Hello, World!","component":"github.com/go-kod/kod/Main"}
100
+ // {"level":"INFO","msg":" Hello, World!","component":"github.com/go-kod/kod/examples/helloworld/HelloWorld"}
88
101
}
89
102
90
103
func Example_interceptor () {
@@ -107,7 +120,7 @@ func Example_interceptor() {
107
120
// helloWorld shutdown
108
121
}
109
122
110
- func Example_builtinInterceptor () {
123
+ func Example_interceptorBuiltin () {
111
124
kod .Run (context .Background (), func (ctx context.Context , app * helloworld.App ) error {
112
125
app .HelloWorld .Get ().SayHello (ctx )
113
126
return nil
@@ -140,17 +153,28 @@ func Example_testWithMockComponent() {
140
153
// Nothing printed from mock
141
154
}
142
155
143
- func Example_lazyInit () {
144
- kod .Run ( context . Background () , func (ctx context.Context , app * helloworld.App ) error {
145
- app .HelloBob . Get ().SayHello ( ctx )
156
+ func Example_testWithConfig () {
157
+ kod .RunTest ( & testing. T {} , func (ctx context.Context , app * helloworld.App ) {
158
+ fmt . Println ( app .Config ().Name )
146
159
app .HelloWorld .Get ().SayHello (ctx )
147
- return nil
160
+ }, kod .WithConfigFile ("./examples/helloworld/config.toml" ))
161
+ // Output:
162
+ // helloWorld init
163
+ // globalConfig
164
+ // Hello, World!config
165
+ // helloWorld shutdown
166
+ }
167
+
168
+ // Example_testWithLogObserver demonstrates how to test log output.
169
+ func Example_testWithLogObserver () {
170
+ kod .RunTest (& testing.T {}, func (ctx context.Context , app * helloworld.App ) {
171
+ app .L (ctx ).Debug ("Hello, World!" )
172
+ app .L (ctx ).Info ("Hello, World!" )
173
+ app .L (ctx ).Warn ("Hello, World!" )
174
+ app .L (ctx ).Error ("Hello, World!" )
148
175
})
176
+
149
177
// Output:
150
178
// helloWorld init
151
- // lazyHelloBob init
152
- // Hello, Bob!
153
- // Hello, World!
154
- // lazyHelloBob shutdown
155
179
// helloWorld shutdown
156
180
}
0 commit comments