diff options
author | Marc Pervaz Boocha <mboocha@sudomsg.com> | 2025-08-07 22:55:19 +0530 |
---|---|---|
committer | Marc Pervaz Boocha <mboocha@sudomsg.com> | 2025-08-07 22:55:19 +0530 |
commit | 2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939 (patch) | |
tree | 3537af98281be071f384c24bc24e4c70a814ba38 /logging/log_test.go | |
parent | Removed the extra logger arguement from http (diff) | |
download | kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.tar kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.tar.gz kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.tar.bz2 kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.tar.lz kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.tar.xz kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.tar.zst kit-2cb8a25e0c0e3ff93a0b96c63fe54a64f212f939.zip |
Added Better stack traces on panic and auto setup a sensible defaultv0.2.0
Diffstat (limited to '')
-rw-r--r-- | logging/log_test.go | 130 |
1 files changed, 130 insertions, 0 deletions
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) + } + }) +} |