From 8549af1605c0c393c08901dac6f58671b4f44a7e Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Mon, 21 Feb 2022 20:55:38 -0800 Subject: [PATCH] Add more Decorate tests This adds more cases of fx.Decorate tests, specifically for transitive dependencies that are decorated. For example, when a function depends on type A whose constructor depends on type B, and B has a decorator associated with it, we expect type A's constructor to use the decorated value of B in its constructor. --- decorate_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/decorate_test.go b/decorate_test.go index cf3f49446..87db3f57d 100644 --- a/decorate_test.go +++ b/decorate_test.go @@ -213,6 +213,54 @@ func TestDecorateSuccess(t *testing.T) { ) defer app.RequireStart().RequireStop() }) + + t.Run("transitive decoration", func(t *testing.T) { + type Config struct { + Scope string + } + type Logger struct { + Cfg *Config + } + app := fxtest.New(t, + fx.Provide(func() *Config { return &Config{Scope: "root"} }), + fx.Module("child", + fx.Decorate(func() *Config { return &Config{Scope: "child"} }), + fx.Provide(func(cfg *Config) *Logger { return &Logger{Cfg: cfg} }), + fx.Invoke(func(l *Logger) { + assert.Equal(t, "child", l.Cfg.Scope) + }), + ), + ) + defer app.RequireStart().RequireStop() + }) + + t.Run("transitive and scoped decorations", func(t *testing.T) { + type Config struct { + Scope string + } + type Logger struct { + Cfg *Config + } + app := fxtest.New(t, + fx.Provide(func() *Config { + return &Config{Scope: "root"} + }), + fx.Provide(func(cfg *Config) *Logger { + return &Logger{Cfg: &Config{ + Scope: cfg.Scope + " logger", + }} + }), + fx.Module("child", + fx.Decorate(func() *Config { + return &Config{Scope: "child"} + }), + fx.Invoke(func(l *Logger) { + assert.Equal(t, "child logger", l.Cfg.Scope) + }), + ), + ) + defer app.RequireStart().RequireStop() + }) } func TestDecorateFailure(t *testing.T) {