From 2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939 Mon Sep 17 00:00:00 2001 From: Marc Pervaz Boocha Date: Thu, 7 Aug 2025 22:55:19 +0530 Subject: Added Better stack traces on panic and auto setup a sensible default --- logging/log_test.go | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) (limited to 'logging/log_test.go') diff --git a/logging/log_test.go b/logging/log_test.go index 93ceaf6..41ee398 100644 --- a/logging/log_test.go +++ b/logging/log_test.go @@ -3,6 +3,7 @@ package logging_test import ( "errors" "log/slog" + "strings" "testing" "go.sudomsg.com/kit/logging" @@ -139,3 +140,132 @@ func TestWithGroup(t *testing.T) { t.Errorf("expected 'user' and 'id' attributes in log record, %v", records) } } + +func TestLogSink(t *testing.T) { + t.Parallel() + + t.Run("RoundTrip", func(t *testing.T) { + t.Parallel() + + testCases := []logging.LogSink{ + logging.SinkStdout, + logging.SinkStderr, + logging.SinkFile, + } + + for _, original := range testCases { + t.Run(original.String(), func(t *testing.T) { + t.Run("case same", func(t *testing.T) { + t.Run(original.String(), func(t *testing.T) { + text, err := original.MarshalText() + if err != nil { + t.Fatalf("MarshalText failed: %v", err) + } + + var decoded logging.LogSink + if err := decoded.UnmarshalText(text); err != nil { + t.Fatalf("UnmarshalText failed: %v", err) + } + + if decoded != original { + t.Fatalf("Round-trip mismatch: got %v, want %v", decoded, original) + } + }) + }) + t.Run("case not same", func(t *testing.T) { + t.Run(original.String(), func(t *testing.T) { + text, err := original.MarshalText() + if err != nil { + t.Fatalf("MarshalText failed: %v", err) + } + + var decoded logging.LogSink + if err := decoded.UnmarshalText([]byte(strings.ToUpper(string(text)))); err != nil { + t.Fatalf("UnmarshalText failed: %v", err) + } + + if decoded != original { + t.Fatalf("Round-trip mismatch: got %v, want %v", decoded, original) + } + }) + }) + + }) + } + }) + + t.Run("Invalid", func(t *testing.T) { + t.Parallel() + + input := "invalid" + var decoded logging.LogSink + + if err := decoded.UnmarshalText([]byte(input)); err == nil { + t.Errorf("expected error for input %q, got none", input) + } + }) +} + +func TestLogFormat(t *testing.T) { + t.Parallel() + + t.Run("RoundTrip", func(t *testing.T) { + t.Parallel() + + testCases := []logging.LogFormat{ + logging.FormatText, + logging.FormatJSON, + } + + for _, original := range testCases { + t.Run(original.String(), func(t *testing.T) { + t.Run("case same", func(t *testing.T) { + t.Run(original.String(), func(t *testing.T) { + text, err := original.MarshalText() + if err != nil { + t.Fatalf("MarshalText failed: %v", err) + } + + var decoded logging.LogFormat + if err := decoded.UnmarshalText(text); err != nil { + t.Fatalf("UnmarshalText failed: %v", err) + } + + if decoded != original { + t.Fatalf("Round-trip mismatch: got %v, want %v", decoded, original) + } + }) + }) + t.Run("case not same", func(t *testing.T) { + t.Run(original.String(), func(t *testing.T) { + text, err := original.MarshalText() + if err != nil { + t.Fatalf("MarshalText failed: %v", err) + } + + var decoded logging.LogFormat + if err := decoded.UnmarshalText([]byte(strings.ToUpper(string(text)))); err != nil { + t.Fatalf("UnmarshalText failed: %v", err) + } + + if decoded != original { + t.Fatalf("Round-trip mismatch: got %v, want %v", decoded, original) + } + }) + }) + + }) + } + }) + + t.Run("Invalid", func(t *testing.T) { + t.Parallel() + + input := "invalid" + var decoded logging.LogFormat + + if err := decoded.UnmarshalText([]byte(input)); err == nil { + t.Errorf("expected error for input %q, got none", input) + } + }) +} -- cgit v1.2.3-70-g09d2