diff --git a/slogx/context_test.go b/slogx/context_test.go index c0bb4274ce4e600948092bdc888c4fa4a370a174..3d44e8f6ee224fc9a358c959a578001f353f6c19 100644 --- a/slogx/context_test.go +++ b/slogx/context_test.go @@ -11,18 +11,18 @@ import ( func TestWithTraceID(t *testing.T) { ctx := context.Background() traceID := "test-trace-id" - + newCtx := WithTraceID(ctx, traceID) - + if newCtx == nil { t.Fatal("Expected context to be returned") } - + value := newCtx.Value("trace_id") if value == nil { t.Fatal("Expected trace_id to be in context") } - + if valueStr, ok := value.(string); ok { if valueStr != traceID { t.Errorf("Expected trace_id to be %s, got %s", traceID, valueStr) @@ -69,17 +69,17 @@ func TestFromContext(t *testing.T) { t.Run(tt.name, func(t *testing.T) { ctx := tt.contextFunc() attrs := FromContext(ctx) - + if tt.expectAttrs { if len(attrs) != 1 { t.Fatalf("Expected 1 attribute, got %d", len(attrs)) } - + attr := attrs[0] if attr.Key != "trace_id" { t.Errorf("Expected key to be 'trace_id', got %s", attr.Key) } - + if attr.Value.String() != tt.expectedValue { t.Errorf("Expected value to be %s, got %s", tt.expectedValue, attr.Value.String()) } @@ -95,15 +95,15 @@ func TestFromContext(t *testing.T) { func TestLogFunctions(t *testing.T) { buf := &bytes.Buffer{} SetWriter(buf) - + // 测试各种日志级别函数 Debug("debug message", "key", "debug_value") Info("info message", "key", "info_value") Warn("warn message", "key", "warn_value") Error("error message", "key", "error_value") - + output := buf.String() - + testCases := []struct { level string message string @@ -115,16 +115,16 @@ func TestLogFunctions(t *testing.T) { {"WARN", "warn message", "key", "warn_value"}, {"ERROR", "error message", "key", "error_value"}, } - + for _, tc := range testCases { if !strings.Contains(output, tc.message) { t.Errorf("Expected output to contain '%s', got: %s", tc.message, output) } - + if !strings.Contains(output, tc.key) { t.Errorf("Expected output to contain key '%s', got: %s", tc.key, output) } - + if !strings.Contains(output, tc.value) { t.Errorf("Expected output to contain value '%s', got: %s", tc.value, output) } @@ -134,23 +134,23 @@ func TestLogFunctions(t *testing.T) { func TestLogFunctionsWithContext(t *testing.T) { buf := &bytes.Buffer{} SetWriter(buf) - + ctx := WithTraceID(context.Background(), "test-trace-id") - + DebugCtx(ctx, "debug message", "key", "debug_value") InfoCtx(ctx, "info message", "key", "info_value") WarnCtx(ctx, "warn message", "key", "warn_value") ErrorCtx(ctx, "error message", "key", "error_value") - + output := buf.String() - + // 检查消息内容 testCases := []string{ "debug message", "info message", "warn message", "error message", "key", "debug_value", "info_value", "warn_value", "error_value", "trace_id", "test-trace-id", } - + for _, expected := range testCases { if !strings.Contains(output, expected) { t.Errorf("Expected output to contain '%s', got: %s", expected, output) @@ -162,30 +162,30 @@ func TestToAttrs(t *testing.T) { // 测试正常的键值对 args := []any{"key1", "value1", "key2", 42, "key3", true} attrs := toAttrs(args...) - + if len(attrs) != 3 { t.Errorf("Expected 3 attributes, got %d", len(attrs)) } - + // 测试奇数个参数的情况 oddArgs := []any{"key1", "value1", "key2"} oddAttrs := toAttrs(oddArgs...) - + if len(oddAttrs) != 1 { t.Errorf("Expected 1 attribute with odd args, got %d", len(oddAttrs)) } - + // 测试非字符串键的情况 nonStringKeyArgs := []any{42, "value1", "key2", "value2"} nonStringKeyAttrs := toAttrs(nonStringKeyArgs...) - + if len(nonStringKeyAttrs) != 1 { t.Errorf("Expected 1 attribute with non-string key, got %d", len(nonStringKeyAttrs)) } - + // 测试空参数 emptyAttrs := toAttrs() if len(emptyAttrs) != 0 { t.Errorf("Expected 0 attributes with empty args, got %d", len(emptyAttrs)) } -} \ No newline at end of file +} diff --git a/slogx/handler_test.go b/slogx/handler_test.go index d7dfbbc6a95391ebcac1083605e4970b80c26fa6..c3e91c817fa88361bddf872dbfe2a69b1129bdfe 100644 --- a/slogx/handler_test.go +++ b/slogx/handler_test.go @@ -54,9 +54,9 @@ func (m *mockHandler) WithGroup(name string) slog.Handler { func TestMultiHandler(t *testing.T) { mock1 := &mockHandler{enabled: true} mock2 := &mockHandler{enabled: true} - + multi := NewMultiHandler(mock1, mock2) - + if multi == nil { t.Fatal("Expected MultiHandler to be created") } @@ -65,7 +65,7 @@ func TestMultiHandler(t *testing.T) { func TestMultiHandlerEnabled(t *testing.T) { enabledHandler := &mockHandler{enabled: true} disabledHandler := &mockHandler{enabled: false} - + tests := []struct { name string handlers []slog.Handler @@ -91,7 +91,7 @@ func TestMultiHandlerEnabled(t *testing.T) { expected: false, }, } - + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { multi := NewMultiHandler(tt.handlers...) @@ -106,28 +106,28 @@ func TestMultiHandlerEnabled(t *testing.T) { func TestMultiHandlerHandle(t *testing.T) { mock1 := &mockHandler{enabled: true} mock2 := &mockHandler{enabled: true} - + multi := NewMultiHandler(mock1, mock2) - + record := slog.NewRecord(slog.Time{}, slog.LevelInfo, "test message", 0) err := multi.Handle(context.Background(), record) - + if err != nil { t.Errorf("Expected no error, got %v", err) } - + if len(mock1.records) != 1 { t.Errorf("Expected mock1 to have 1 record, got %d", len(mock1.records)) } - + if len(mock2.records) != 1 { t.Errorf("Expected mock2 to have 1 record, got %d", len(mock2.records)) } - + if mock1.records[0].Message != record.Message { t.Errorf("Expected mock1 record message to be '%s', got '%s'", record.Message, mock1.records[0].Message) } - + if mock2.records[0].Message != record.Message { t.Errorf("Expected mock2 record message to be '%s', got '%s'", record.Message, mock2.records[0].Message) } @@ -136,22 +136,22 @@ func TestMultiHandlerHandle(t *testing.T) { func TestMultiHandlerWithAttrs(t *testing.T) { mock1 := &mockHandler{enabled: true} mock2 := &mockHandler{enabled: true} - + multi := NewMultiHandler(mock1, mock2) - + attrs := []slog.Attr{slog.String("key", "value")} newHandler := multi.WithAttrs(attrs) - + if newHandler == nil { t.Fatal("Expected new handler to be returned") } - + // Check that the new handler is also a MultiHandler multiNew, ok := newHandler.(*MultiHandler) if !ok { t.Fatal("Expected new handler to be a MultiHandler") } - + if len(multiNew.handlers) != 2 { t.Errorf("Expected new MultiHandler to have 2 handlers, got %d", len(multiNew.handlers)) } @@ -160,21 +160,21 @@ func TestMultiHandlerWithAttrs(t *testing.T) { func TestMultiHandlerWithGroup(t *testing.T) { mock1 := &mockHandler{enabled: true} mock2 := &mockHandler{enabled: true} - + multi := NewMultiHandler(mock1, mock2) - + newHandler := multi.WithGroup("test_group") - + if newHandler == nil { t.Fatal("Expected new handler to be returned") } - + // Check that the new handler is also a MultiHandler multiNew, ok := newHandler.(*MultiHandler) if !ok { t.Fatal("Expected new handler to be a MultiHandler") } - + if len(multiNew.handlers) != 2 { t.Errorf("Expected new MultiHandler to have 2 handlers, got %d", len(multiNew.handlers)) } @@ -182,31 +182,31 @@ func TestMultiHandlerWithGroup(t *testing.T) { func TestMultiHandlerIntegration(t *testing.T) { var buf1, buf2 bytes.Buffer - + handler1 := slog.NewTextHandler(&buf1, nil) handler2 := slog.NewJSONHandler(&buf2, nil) - + multi := NewMultiHandler(handler1, handler2) logger := slog.New(multi) - + logger.Info("integration test", "key", "value") - + textOutput := buf1.String() jsonOutput := buf2.String() - + if !strings.Contains(textOutput, "integration test") { t.Errorf("Expected text output to contain 'integration test', got: %s", textOutput) } - + if !strings.Contains(jsonOutput, "integration test") { t.Errorf("Expected JSON output to contain 'integration test', got: %s", jsonOutput) } - + if !strings.Contains(textOutput, "key") || !strings.Contains(textOutput, "value") { t.Errorf("Expected text output to contain key-value pair, got: %s", textOutput) } - + if !strings.Contains(jsonOutput, "key") || !strings.Contains(jsonOutput, "value") { t.Errorf("Expected JSON output to contain key-value pair, got: %s", jsonOutput) } -} \ No newline at end of file +} diff --git a/slogx/logger_test.go b/slogx/logger_test.go index 137b2b9080c4b399ff7b24d1c8c410198f966f6e..e3e7d42fdd12bfacb64bd21a71b5319d305efac6 100644 --- a/slogx/logger_test.go +++ b/slogx/logger_test.go @@ -41,7 +41,7 @@ func TestSetupWithConfig(t *testing.T) { WithStdout: false, AddSource: false, } - + // 重定向输出到 buffer SetWriter(buf) Setup(cfg) @@ -91,10 +91,10 @@ func TestNewLogger(t *testing.T) { t.Run(tt.name, func(t *testing.T) { buf := &bytes.Buffer{} SetWriter(buf) - + logger := newLogger(tt.config) logger.Info("test message") - + output := buf.String() if !strings.Contains(output, "test message") { t.Errorf("Expected log to contain 'test message', got: %s", output) @@ -106,9 +106,9 @@ func TestNewLogger(t *testing.T) { func TestSetWriter(t *testing.T) { buf := &bytes.Buffer{} SetWriter(buf) - + L().Info("test message") - + output := buf.String() if !strings.Contains(output, "test message") { t.Errorf("Expected log to be written to buffer, got: %s", output) @@ -127,4 +127,4 @@ func TestConcurrentSetup(t *testing.T) { Setup(cfg) }() } -} \ No newline at end of file +} diff --git a/slogx/middleware/http_test.go b/slogx/middleware/http_test.go index 9824ea6e6849650942de7ab8c0594d25d5cc1f12..93f465e1e25d01c26144c4682b00997a92d5664e 100644 --- a/slogx/middleware/http_test.go +++ b/slogx/middleware/http_test.go @@ -253,4 +253,4 @@ func TestHTTPMiddlewareWithDuration(t *testing.T) { // 粗略验证持续时间不太离谱(应该大于等于10毫秒,但考虑到测试环境可能较慢,设一个较低的阈值) // 注意:由于测试环境的不确定性,这里不做严格的数值校验 -} \ No newline at end of file +} diff --git a/slogx/rotation_test.go b/slogx/rotation_test.go index 51e7d5de76f77f2043b6e52738537c7d781fe42e..6497c204b20e66029bbad4e1bbc6d33f974c6505 100644 --- a/slogx/rotation_test.go +++ b/slogx/rotation_test.go @@ -8,12 +8,12 @@ func TestNewRotatingWriter(t *testing.T) { // 测试创建旋转写入器 filename := "/tmp/test-log.txt" writer := NewRotatingWriter(filename) - + if writer == nil { t.Error("Expected RotatingWriter to be created") } - + // 验证返回的对象实现了 io.Writer 接口 // 这会在编译时检查,如果未实现会报错 var _ interface{ Write([]byte) (int, error) } = writer -} \ No newline at end of file +}