Skip to content

Commit

Permalink
Add slogtest test
Browse files Browse the repository at this point in the history
  • Loading branch information
veqryn committed Mar 7, 2024
1 parent b4b61a6 commit 8a36ae4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions slogtest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package slogctx_test

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

slogctx "github.com/veqryn/slog-context"
)

func TestSlogtest(t *testing.T) {
var buf bytes.Buffer
h := slogctx.NewHandler(slog.NewJSONHandler(&buf, nil), nil)

results := func() []map[string]any {
ms, err := parseLines(buf.Bytes(), parseJSON)
if err != nil {
t.Fatal(err)
}
return ms
}
if err := slogtest.TestHandler(h, results); err != nil {
t.Fatal(err)
}
}

func parseLines(src []byte, parse func([]byte) (map[string]any, error)) ([]map[string]any, error) {
fmt.Println(string(src))
var records []map[string]any
for _, line := range bytes.Split(src, []byte{'\n'}) {
if len(line) == 0 {
continue
}
m, err := parse(line)
if err != nil {
return nil, fmt.Errorf("%s: %w", string(line), err)
}
records = append(records, m)
}
return records, nil
}

func parseJSON(bs []byte) (map[string]any, error) {
var m map[string]any
if err := json.Unmarshal(bs, &m); err != nil {
return nil, err
}
return m, nil
}

0 comments on commit 8a36ae4

Please sign in to comment.