-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsugar_test.go
More file actions
110 lines (90 loc) · 3.78 KB
/
sugar_test.go
File metadata and controls
110 lines (90 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package slogx_test
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/yylego/slogx"
)
// TestNewSugaredLogger tests SugaredLogger creation
// TestNewSugaredLogger 测试 SugaredLogger 创建
func TestNewSugaredLogger(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
require.NotNil(t, sugar)
}
// TestLogger_Sugar tests Logger.Sugar() method
// TestLogger_Sugar 测试 Logger.Sugar() 方法
func TestLogger_Sugar(t *testing.T) {
logger := slogx.New(setupTestLogger())
sugar := logger.Sugar()
require.NotNil(t, sugar)
sugar.Info("test", "message", 123)
sugar.Debug("1", 2, 3, 4, "5", 6) // Should be "12 3 456"
// Test chaining
logger.Skip(1).Sugar().Info("chained", "call", "works")
logger.WithGroup("test").Sugar().Warn("grouped", "sugar", "logging")
}
// TestSugaredLogger_BasicMethods tests basic logging methods
// TestSugaredLogger_BasicMethods 测试基本日志方法
func TestSugaredLogger_BasicMethods(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
// Sprint behavior: no spaces between strings
sugar.Debug("debug", "message", 123)
sugar.Info("info", "message", 456)
sugar.Warn("warn", "message", 789)
sugar.Error("error", "message", 101112)
}
// TestSugaredLogger_LnMethods tests ln methods with spaces
// TestSugaredLogger_LnMethods 测试带空格的 ln 方法
func TestSugaredLogger_LnMethods(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
// Sprintln behavior: spaces between all args
sugar.Debugln("debug", "message", 123)
sugar.Infoln("info", "message", 456)
sugar.Warnln("warn", "message", 789)
sugar.Errorln("error", "message", 101112)
}
// TestSugaredLogger_BasicContextMethods tests Context methods
// TestSugaredLogger_BasicContextMethods 测试 Context 方法
func TestSugaredLogger_BasicContextMethods(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
ctx := context.Background()
sugar.DebugContext(ctx, "debug", "context", 123)
sugar.InfoContext(ctx, "info", "context", 456)
sugar.WarnContext(ctx, "warn", "context", 789)
sugar.ErrorContext(ctx, "error", "context", 101112)
}
// TestSugaredLogger_BasicContextLnMethods tests Context ln methods
// TestSugaredLogger_BasicContextLnMethods 测试 Context ln 方法
func TestSugaredLogger_BasicContextLnMethods(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
ctx := context.Background()
sugar.DebuglnContext(ctx, "debug", "context", 123)
sugar.InfolnContext(ctx, "info", "context", 456)
sugar.WarnlnContext(ctx, "warn", "context", 789)
sugar.ErrorlnContext(ctx, "error", "context", 101112)
}
// TestSugaredLogger_FormatBehavior tests Sprint vs Sprintln format differences
// TestSugaredLogger_FormatBehavior 测试 Sprint 和 Sprintln 格式差异
func TestSugaredLogger_FormatBehavior(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
sugar.Debug(1, 2, 3, 4, 5, 6) // Should be "1 2 3 4 5 6"
sugar.Debugln(1, 2, 3, 4, 5, 6) // Should be "1 2 3 4 5 6"
sugar.Debug("1", 2, 3, 4, "5", 6) // Should be "12 3 456"
sugar.Debugln("1", 2, 3, 4, "5", 6) // Should be "1 2 3 4 5 6"
}
// TestSugaredLogger_EmptyArgs tests behavior with empty arguments
// TestSugaredLogger_EmptyArgs 测试空参数的行为
func TestSugaredLogger_EmptyArgs(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
sugar.Debug()
sugar.Debugln()
sugar.Info()
sugar.Infoln()
}
// TestSugaredLogger_MixedTypes tests various argument types
// TestSugaredLogger_MixedTypes 测试各种参数类型
func TestSugaredLogger_MixedTypes(t *testing.T) {
sugar := slogx.NewSugaredLogger(slogx.New(setupTestLogger()))
sugar.Info("string", 123, true, 3.14, []int{1, 2, 3})
sugar.Infoln("string", 123, true, 3.14, []int{1, 2, 3})
}