From deb62729bae72936ade3e7649deafffb7aff725b Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Tue, 2 Aug 2016 17:44:06 -0700 Subject: [PATCH] log: add testing for context logger Signed-off-by: Stephen J Day --- log/context_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 log/context_test.go diff --git a/log/context_test.go b/log/context_test.go new file mode 100644 index 0000000..ddff398 --- /dev/null +++ b/log/context_test.go @@ -0,0 +1,41 @@ +package log + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" +) + +func TestLoggerContext(t *testing.T) { + ctx := context.Background() + assert.Equal(t, GetLogger(ctx), L) // should be same as L variable + assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same. + + ctx = WithLogger(ctx, G(ctx).WithField("test", "one")) + assert.Equal(t, GetLogger(ctx).Data["test"], "one") + assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same. +} + +func TestModuleContext(t *testing.T) { + ctx := context.Background() + assert.Equal(t, GetModulePath(ctx), "") + + ctx = WithModule(ctx, "a") // basic behavior + assert.Equal(t, GetModulePath(ctx), "a") + logger := GetLogger(ctx) + assert.Equal(t, logger.Data["module"], "a") + + parent, ctx := ctx, WithModule(ctx, "a") + assert.Equal(t, ctx, parent) // should be a no-op + assert.Equal(t, GetModulePath(ctx), "a") + assert.Equal(t, GetLogger(ctx).Data["module"], "a") + + ctx = WithModule(ctx, "b") // new module + assert.Equal(t, GetModulePath(ctx), "a/b") + assert.Equal(t, GetLogger(ctx).Data["module"], "a/b") + + ctx = WithModule(ctx, "c") // new module + assert.Equal(t, GetModulePath(ctx), "a/b/c") + assert.Equal(t, GetLogger(ctx).Data["module"], "a/b/c") +}