Skip to content

Commit a742ae4

Browse files
committed
testing/slogtest: add Run to run cases as subtests
This is an implementation of proposal #61758. It adds a function to slogtest that runs each test case in a subtest, instead of running them all at once. That allows the caller to control which cases are run. Fixes #61706. Fixes #61758. Change-Id: I95108b7b753675203ca7f0f00ccbc242bd9c2a9f Reviewed-on: https://go-review.googlesource.com/c/go/+/516076 Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]>
1 parent af3bf86 commit a742ae4

File tree

4 files changed

+248
-175
lines changed

4 files changed

+248
-175
lines changed

api/next/61758.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pkg testing/slogtest, func Run(*testing.T, func(*testing.T) slog.Handler, func(*testing.T) map[string]interface{}) #61758

src/go/build/deps_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,15 +572,15 @@ var depsRules = `
572572
< testing/iotest
573573
< testing/fstest;
574574
575-
log/slog
576-
< testing/slogtest;
577-
578575
FMT, flag, math/rand
579576
< testing/quick;
580577
581578
FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
582579
< testing;
583580
581+
log/slog, testing
582+
< testing/slogtest;
583+
584584
FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
585585
internal/godebug, math/rand, encoding/hex, crypto/sha256
586586
< internal/fuzz;

src/testing/slogtest/run_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package slogtest_test
6+
7+
import (
8+
"bytes"
9+
"encoding/json"
10+
"log/slog"
11+
"testing"
12+
"testing/slogtest"
13+
)
14+
15+
func TestRun(t *testing.T) {
16+
var buf bytes.Buffer
17+
18+
newHandler := func(*testing.T) slog.Handler {
19+
buf.Reset()
20+
return slog.NewJSONHandler(&buf, nil)
21+
}
22+
result := func(t *testing.T) map[string]any {
23+
m := map[string]any{}
24+
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
25+
t.Fatal(err)
26+
}
27+
return m
28+
}
29+
30+
slogtest.Run(t, newHandler, result)
31+
}

0 commit comments

Comments
 (0)