@@ -3,54 +3,72 @@ package blog
33import (
44 "context"
55 "log/slog"
6+ "strings"
67 "testing"
7-
8- "github.com/letsencrypt/boulder/test"
98)
109
11- func TestContextWithNoAttrs (t * testing.T ) {
12- t .Parallel ()
13-
14- // A fresh context has no attrs attached; fromContext should return an empty slice.
15- test .AssertEquals (t , len (fromContext (context .Background ())), 0 )
16- }
17-
18- func TestContextWithSingleLayer (t * testing.T ) {
19- t .Parallel ()
20-
21- ctx := ContextWith (context .Background (), slog .String ("k1" , "v1" ), slog .Int ("k2" , 2 ))
22- attrs := fromContext (ctx )
23- test .AssertEquals (t , len (attrs ), 2 )
24- test .AssertEquals (t , attrs [0 ].Key , "k1" )
25- test .AssertEquals (t , attrs [1 ].Key , "k2" )
26- }
27-
28- func TestContextWithAccumulatesAcrossLayers (t * testing.T ) {
10+ func TestContextWith (t * testing.T ) {
2911 t .Parallel ()
3012
31- // ContextWith should append to any existing attrs, not replace them.
32- ctx := ContextWith (context .Background (), slog .String ("k1" , "v1" ))
33- ctx = ContextWith (ctx , slog .String ("k2" , "v2" ))
34- ctx = ContextWith (ctx , slog .String ("k3" , "v3" ))
13+ testCases := []struct {
14+ name string
15+ ctxFn func (context.Context ) context.Context
16+ wantKeys []string
17+ wantLog []string
18+ }{
19+ {
20+ name : "empty" ,
21+ ctxFn : func (ctx context.Context ) context.Context { return ctx },
22+ },
23+ {
24+ name : "single layer" ,
25+ ctxFn : func (ctx context.Context ) context.Context {
26+ return ContextWith (ctx , slog .String ("k1" , "v1" ), slog .Int ("k2" , 2 ))
27+ },
28+ wantKeys : []string {"k1" , "k2" },
29+ wantLog : []string {"k1=v1" , "k2=2" },
30+ },
31+ {
32+ name : "multi-layer" ,
33+ ctxFn : func (ctx context.Context ) context.Context {
34+ // ContextWith should append to any existing attrs, not replace them.
35+ ctx = ContextWith (ctx , slog .String ("k1" , "v1" ))
36+ ctx = ContextWith (ctx , slog .String ("k2" , "v2" ))
37+ ctx = ContextWith (ctx , slog .String ("k3" , "v3" ))
38+ return ctx
39+ },
40+ wantKeys : []string {"k1" , "k2" , "k3" },
41+ wantLog : []string {"k1=v1" , "k2=v2" , "k3=v3" },
42+ },
43+ }
3544
36- attrs := fromContext (ctx )
37- test .AssertEquals (t , len (attrs ), 3 )
38- test .AssertEquals (t , attrs [0 ].Key , "k1" )
39- test .AssertEquals (t , attrs [1 ].Key , "k2" )
40- test .AssertEquals (t , attrs [2 ].Key , "k3" )
41- }
42-
43- func TestContextWithAppliedByLogger (t * testing.T ) {
44- t .Parallel ()
45+ for _ , tc := range testCases {
46+ t .Run (tc .name , func (t * testing.T ) {
47+ t .Parallel ()
4548
46- l := NewMock ()
47- ctx := ContextWith (context .Background (), slog .String ("outer" , "yes" ))
48- ctx = ContextWith (ctx , slog .String ("inner" , "also" ))
49+ ctx := tc .ctxFn (t .Context ())
4950
50- l .Info (ctx , "hello" )
51+ attrs := fromContext (ctx )
52+ if len (attrs ) != len (tc .wantKeys ) {
53+ t .Fatalf ("fromContext returned %d attrs, want %d" , len (attrs ), len (tc .wantKeys ))
54+ }
55+ for i , want := range tc .wantKeys {
56+ if attrs [i ].Key != want {
57+ t .Errorf ("attrs[%d].Key = %q, want %q" , i , attrs [i ].Key , want )
58+ }
59+ }
5160
52- got := l .GetAll ()
53- test .AssertEquals (t , len (got ), 1 )
54- test .AssertContains (t , got [0 ], "outer=yes" )
55- test .AssertContains (t , got [0 ], "inner=also" )
61+ l := NewMock ()
62+ l .Info (ctx , "hello" )
63+ got := l .GetAll ()
64+ if len (got ) != 1 {
65+ t .Fatalf ("got %d log lines, want 1: %v" , len (got ), got )
66+ }
67+ for _ , want := range tc .wantLog {
68+ if ! strings .Contains (got [0 ], want ) {
69+ t .Errorf ("log line %q does not contain %q" , got [0 ], want )
70+ }
71+ }
72+ })
73+ }
5674}
0 commit comments