|
| 1 | +package log |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "golang.org/x/net/context" |
| 8 | +) |
| 9 | + |
| 10 | +func TestLoggerContext(t *testing.T) { |
| 11 | + ctx := context.Background() |
| 12 | + assert.Equal(t, GetLogger(ctx), L) // should be same as L variable |
| 13 | + assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same. |
| 14 | + |
| 15 | + ctx = WithLogger(ctx, G(ctx).WithField("test", "one")) |
| 16 | + assert.Equal(t, GetLogger(ctx).Data["test"], "one") |
| 17 | + assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same. |
| 18 | +} |
| 19 | + |
| 20 | +func TestModuleContext(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + assert.Equal(t, GetModulePath(ctx), "") |
| 23 | + |
| 24 | + ctx = WithModule(ctx, "a") // basic behavior |
| 25 | + assert.Equal(t, GetModulePath(ctx), "a") |
| 26 | + logger := GetLogger(ctx) |
| 27 | + assert.Equal(t, logger.Data["module"], "a") |
| 28 | + |
| 29 | + parent, ctx := ctx, WithModule(ctx, "a") |
| 30 | + assert.Equal(t, ctx, parent) // should be a no-op |
| 31 | + assert.Equal(t, GetModulePath(ctx), "a") |
| 32 | + assert.Equal(t, GetLogger(ctx).Data["module"], "a") |
| 33 | + |
| 34 | + ctx = WithModule(ctx, "b") // new module |
| 35 | + assert.Equal(t, GetModulePath(ctx), "a/b") |
| 36 | + assert.Equal(t, GetLogger(ctx).Data["module"], "a/b") |
| 37 | + |
| 38 | + ctx = WithModule(ctx, "c") // new module |
| 39 | + assert.Equal(t, GetModulePath(ctx), "a/b/c") |
| 40 | + assert.Equal(t, GetLogger(ctx).Data["module"], "a/b/c") |
| 41 | +} |
0 commit comments