Skip to content

Commit

Permalink
testing/slogtest: add Run to run cases as subtests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jba committed Sep 8, 2023
1 parent af3bf86 commit a742ae4
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 175 deletions.
1 change: 1 addition & 0 deletions api/next/61758.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pkg testing/slogtest, func Run(*testing.T, func(*testing.T) slog.Handler, func(*testing.T) map[string]interface{}) #61758
6 changes: 3 additions & 3 deletions src/go/build/deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,15 +572,15 @@ var depsRules = `
< testing/iotest
< testing/fstest;
log/slog
< testing/slogtest;
FMT, flag, math/rand
< testing/quick;
FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
< testing;
log/slog, testing
< testing/slogtest;
FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
internal/godebug, math/rand, encoding/hex, crypto/sha256
< internal/fuzz;
Expand Down
31 changes: 31 additions & 0 deletions src/testing/slogtest/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package slogtest_test

import (
"bytes"
"encoding/json"
"log/slog"
"testing"
"testing/slogtest"
)

func TestRun(t *testing.T) {
var buf bytes.Buffer

newHandler := func(*testing.T) slog.Handler {
buf.Reset()
return slog.NewJSONHandler(&buf, nil)
}
result := func(t *testing.T) map[string]any {
m := map[string]any{}
if err := json.Unmarshal(buf.Bytes(), &m); err != nil {
t.Fatal(err)
}
return m
}

slogtest.Run(t, newHandler, result)
}
Loading

0 comments on commit a742ae4

Please sign in to comment.