-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_test.go
133 lines (104 loc) · 3.09 KB
/
format_test.go
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package slogtelegram
import (
"log/slog"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestDefaultFormatter_Format(t *testing.T) {
formatter := NewFormatter(FormatterOptions{})
record := slog.Record{
Level: slog.LevelInfo,
Time: time.Date(2024, 12, 29, 15, 0, 0, 0, time.UTC),
Message: "Test message",
}
record.AddAttrs(slog.String("key1", "value1"))
record.AddAttrs(slog.Int("key2", 42))
formatted, err := formatter.Format(record)
assert.NoError(t, err)
expected := `<i>ℹ️ Info</i>
<i>⌚️ 2024-12-29T15:00:00Z</i>
<b>💬 Test message</b>
<tg-spoiler><blockquote expandable>🔘 key1: value1
🔘 key2: 42
</blockquote></tg-spoiler>`
assert.Equal(t, expected, strings.TrimSpace(formatted))
}
func TestCustomTemplate(t *testing.T) {
customTemplate := `{{time .}} | {{level .}} | {{message .}}`
formatter := NewFormatter(FormatterOptions{
Template: customTemplate,
})
record := slog.Record{
Level: slog.LevelWarn,
Time: time.Date(2024, 12, 29, 15, 0, 0, 0, time.UTC),
Message: "Warning message",
}
formatted, err := formatter.Format(record)
assert.NoError(t, err)
expected := `<i>⌚️ 2024-12-29T15:00:00Z</i> | <i>⚠️ Warning</i> | <b>💬 Warning message</b>`
assert.Equal(t, expected, strings.TrimSpace(formatted))
}
func TestCustomFormatters(t *testing.T) {
formatter := NewFormatter(FormatterOptions{
LevelFormatter: func(record slog.Record) string {
return "CustomLevel"
},
TimeFormatter: func(record slog.Record) string {
return "CustomTime"
},
MessageFormatter: func(record slog.Record) string {
return "CustomMessage"
},
AttrsFormatter: func(record slog.Record) string {
return "CustomAttrs"
},
})
record := slog.Record{
Level: slog.LevelDebug,
Time: time.Now(),
Message: "Debug message",
}
record.AddAttrs(slog.String("key", "value"))
formatted, err := formatter.Format(record)
assert.NoError(t, err)
expected := `CustomLevel
CustomTime
CustomMessage
<tg-spoiler><blockquote expandable>CustomAttrs</blockquote></tg-spoiler>`
assert.Equal(t, expected, strings.TrimSpace(formatted))
}
func TestEmptyAttrs(t *testing.T) {
formatter := NewFormatter(FormatterOptions{})
record := slog.Record{
Level: slog.LevelInfo,
Time: time.Date(2024, 12, 29, 15, 0, 0, 0, time.UTC),
Message: "Message with no attrs",
}
formatted, err := formatter.Format(record)
assert.NoError(t, err)
expected := `<i>ℹ️ Info</i>
<i>⌚️ 2024-12-29T15:00:00Z</i>
<b>💬 Message with no attrs</b>`
assert.Equal(t, expected, strings.TrimSpace(formatted))
}
func TestCustomInstance(t *testing.T) {
customFormatter := &formatterMock{}
formatter := NewFormatter(FormatterOptions{
Instance: customFormatter,
})
record := slog.Record{}
customFormatter.On("Format", record).Return("custom", nil)
_, err := formatter.Format(record)
assert.NoError(t, err)
customFormatter.AssertExpectations(t)
}
type formatterMock struct {
mock.Mock
}
func (m *formatterMock) Format(record slog.Record) (string, error) {
args := m.Called(record)
return args.String(0), args.Error(1)
}