Skip to content

Commit ebef526

Browse files
committed
feat: add defer
1 parent c52b31a commit ebef526

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

example_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,23 @@ func Example_testWithKoanf() {
321321
// Hello, World!
322322
// helloWorld shutdown
323323
}
324+
325+
// This example demonstrates how to use [kod.RunTest] to run a test function with a defer function.
326+
func Example_testWithDefer() {
327+
kod.RunTest(&testing.T{}, func(ctx context.Context, app *helloworld.App) {
328+
k := kod.FromContext(ctx)
329+
k.Defer("test", func(ctx context.Context) error {
330+
fmt.Println("Defer called")
331+
return nil
332+
})
333+
334+
fmt.Println(app.Config().Name)
335+
app.HelloWorld.Get().SayHello(ctx)
336+
})
337+
// Output:
338+
// helloWorld init
339+
// kod
340+
// Hello, World!
341+
// Defer called
342+
// helloWorld shutdown
343+
}

kod.go

+5
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ func (k *Kod) Config() kodConfig {
386386
return k.config
387387
}
388388

389+
// Defer adds a hook function to the Kod instance.
390+
func (k *Kod) Defer(name string, fn func(context.Context) error) {
391+
k.hooker.Add(hooks.HookFunc{Name: name, Fn: fn})
392+
}
393+
389394
// unmarshalConfig sets the configuration for the Kod instance.
390395
func (k *Kod) unmarshalConfig(key string, out interface{}) error {
391396
err := defaults.Set(out)

0 commit comments

Comments
 (0)