Commit ebef526 1 parent c52b31a commit ebef526 Copy full SHA for ebef526
File tree 2 files changed +25
-0
lines changed
2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -321,3 +321,23 @@ func Example_testWithKoanf() {
321
321
// Hello, World!
322
322
// helloWorld shutdown
323
323
}
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
+ }
Original file line number Diff line number Diff line change @@ -386,6 +386,11 @@ func (k *Kod) Config() kodConfig {
386
386
return k .config
387
387
}
388
388
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
+
389
394
// unmarshalConfig sets the configuration for the Kod instance.
390
395
func (k * Kod ) unmarshalConfig (key string , out interface {}) error {
391
396
err := defaults .Set (out )
You can’t perform that action at this time.
0 commit comments