Skip to content

Commit 2665157

Browse files
committedDec 7, 2023
Add tests for context with slog
1 parent 5153ab2 commit 2665157

File tree

3 files changed

+116
-29
lines changed

3 files changed

+116
-29
lines changed
 

‎context_slog_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Copyright 2021 The logr Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
*/
19+
20+
package logr
21+
22+
import (
23+
"context"
24+
"log/slog"
25+
"os"
26+
"testing"
27+
)
28+
29+
func TestContextWithSlog(t *testing.T) {
30+
ctx := context.Background()
31+
32+
if out := FromContextAsSlogLogger(ctx); out != nil {
33+
t.Errorf("expected no logger, got %#v", out)
34+
}
35+
36+
// Write as slog...
37+
slogger := slog.New(slog.NewJSONHandler(os.Stderr, nil))
38+
sctx := NewContextWithSlogLogger(ctx, slogger)
39+
40+
// ...read as logr
41+
if out, err := FromContext(sctx); err != nil {
42+
t.Errorf("unexpected error: %v", err)
43+
} else if _, ok := out.sink.(*slogSink); !ok {
44+
t.Errorf("expected output to be type *logr.slogSink, got %T", out.sink)
45+
}
46+
47+
// ...read as slog
48+
if out := FromContextAsSlogLogger(sctx); out == nil {
49+
t.Errorf("expected a *slog.JSONHandler, got nil")
50+
} else if _, ok := out.Handler().(*slog.JSONHandler); !ok {
51+
t.Errorf("expected output to be type *slog.JSONHandler, got %T", out.Handler())
52+
}
53+
54+
// Write as logr...
55+
logger := Discard()
56+
lctx := NewContext(ctx, logger)
57+
58+
// ...read as slog
59+
if out := FromContextAsSlogLogger(lctx); out == nil {
60+
t.Errorf("expected a *log.slogHandler, got nil")
61+
} else if _, ok := out.Handler().(*slogHandler); !ok {
62+
t.Errorf("expected output to be type *logr.slogHandler, got %T", out.Handler())
63+
}
64+
65+
// ...read as logr is covered in the non-slog test
66+
}

‎context_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2021 The logr Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package logr
18+
19+
import (
20+
"context"
21+
"testing"
22+
)
23+
24+
func TestContext(t *testing.T) {
25+
ctx := context.Background()
26+
27+
if out, err := FromContext(ctx); err == nil {
28+
t.Errorf("expected error, got %#v", out)
29+
} else if _, ok := err.(notFoundError); !ok {
30+
t.Errorf("expected a notFoundError, got %#v", err)
31+
}
32+
33+
out := FromContextOrDiscard(ctx)
34+
if out.sink != nil {
35+
t.Errorf("expected a nil sink, got %#v", out)
36+
}
37+
38+
sink := &testLogSink{}
39+
logger := New(sink)
40+
lctx := NewContext(ctx, logger)
41+
if out, err := FromContext(lctx); err != nil {
42+
t.Errorf("unexpected error: %v", err)
43+
} else if p, _ := out.sink.(*testLogSink); p != sink {
44+
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
45+
}
46+
out = FromContextOrDiscard(lctx)
47+
if p, _ := out.sink.(*testLogSink); p != sink {
48+
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
49+
}
50+
}

‎logr_test.go

-29
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package logr
1818

1919
import (
20-
"context"
2120
"errors"
2221
"fmt"
2322
"reflect"
@@ -414,34 +413,6 @@ func TestWithCallDepthIncremental(t *testing.T) {
414413
}
415414
}
416415

417-
func TestContext(t *testing.T) {
418-
ctx := context.TODO()
419-
420-
if out, err := FromContext(ctx); err == nil {
421-
t.Errorf("expected error, got %#v", out)
422-
} else if _, ok := err.(notFoundError); !ok {
423-
t.Errorf("expected a notFoundError, got %#v", err)
424-
}
425-
426-
out := FromContextOrDiscard(ctx)
427-
if out.sink != nil {
428-
t.Errorf("expected a nil sink, got %#v", out)
429-
}
430-
431-
sink := &testLogSink{}
432-
logger := New(sink)
433-
lctx := NewContext(ctx, logger)
434-
if out, err := FromContext(lctx); err != nil {
435-
t.Errorf("unexpected error: %v", err)
436-
} else if p, _ := out.sink.(*testLogSink); p != sink {
437-
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
438-
}
439-
out = FromContextOrDiscard(lctx)
440-
if p, _ := out.sink.(*testLogSink); p != sink {
441-
t.Errorf("expected output to be the same as input, got in=%p, out=%p", sink, p)
442-
}
443-
}
444-
445416
func TestIsZero(t *testing.T) {
446417
var l Logger
447418
if !l.IsZero() {

0 commit comments

Comments
 (0)
Please sign in to comment.