Source file src/testing/slogtest/run_test.go

     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  }
    32  

View as plain text