File: handler_test.go

package info (click to toggle)
golang-github-chainguard-dev-clog 1.7.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 424 kB
  • sloc: makefile: 4
file content (53 lines) | stat: -rw-r--r-- 938 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package clog

import (
	"bytes"
	"context"
	"encoding/json"
	"log/slog"
	"reflect"
	"testing"
)

func TestContextHandler(t *testing.T) {
	ctx := context.Background()
	ctx = WithValues(ctx, "foo", "bar")
	ctx2 := WithValues(ctx,
		"a", "b",
		"c", "d",
	)
	ctx = WithValues(ctx, "b", 1)

	for _, tc := range []struct {
		ctx  context.Context
		want map[string]any
	}{
		{ctx, map[string]any{
			"b":   float64(1),
			"foo": "bar",
		}},
		{ctx2, map[string]any{
			"foo": "bar",
			"a":   "b",
			"c":   "d",
		}},
	} {
		t.Run("", func(t *testing.T) {
			b := new(bytes.Buffer)
			log := slog.New(NewHandler(slog.NewJSONHandler(b, testopts)))
			log.InfoContext(tc.ctx, "")

			tc.want["level"] = "INFO"
			tc.want["msg"] = ""

			var got map[string]any
			if err := json.Unmarshal(b.Bytes(), &got); err != nil {
				t.Fatal(err)
			}

			if !reflect.DeepEqual(tc.want, got) {
				t.Errorf("want %v, got %v", tc.want, got)
			}
		})
	}
}