aboutsummaryrefslogtreecommitdiffstats
path: root/logging/log_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'logging/log_test.go')
-rw-r--r--logging/log_test.go130
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)
+ }
+ })
+}