-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug_test.go
More file actions
204 lines (159 loc) · 4.8 KB
/
debug_test.go
File metadata and controls
204 lines (159 loc) · 4.8 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//nolint:paralleltest // Tests modify package-level debug state, cannot run in parallel
package pn532
import (
"bytes"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// saveDebugState saves the current debug state for restoration.
func saveDebugState() (enabled bool, writer any) {
return debugEnabled, sessionLogWriter
}
// restoreDebugState restores saved debug state.
func restoreDebugState(enabled bool, writer any) {
debugEnabled = enabled
if writer == nil {
sessionLogWriter = nil
} else if buf, ok := writer.(*bytes.Buffer); ok {
sessionLogWriter = buf
}
}
func TestDebugf_WritesToSessionLog(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
// Set up a buffer as the session log writer
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false // Disable console output
Debugf("test message %d", 42)
content := buf.String()
assert.Contains(t, content, "DEBUG: test message 42")
assert.Contains(t, content, "\n") // Should have newline
}
func TestDebugf_IncludesTimestamp(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false
Debugf("test message")
content := buf.String()
// Verify timestamp format: HH:MM:SS.mmm
matched, err := regexp.MatchString(`\d{2}:\d{2}:\d{2}\.\d{3} DEBUG:`, content)
require.NoError(t, err)
assert.True(t, matched, "Should include timestamp in format HH:MM:SS.mmm, got: %s", content)
}
func TestDebugf_NilSessionWriter(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
sessionLogWriter = nil
debugEnabled = false
// Should not panic when sessionLogWriter is nil
Debugf("test message %d", 42)
}
func TestDebugln_WritesToSessionLog(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false
Debugln("test message")
content := buf.String()
assert.Contains(t, content, "DEBUG: test message")
}
func TestDebugln_IncludesTimestamp(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false
Debugln("test message")
content := buf.String()
// Verify timestamp format: HH:MM:SS.mmm
matched, err := regexp.MatchString(`\d{2}:\d{2}:\d{2}\.\d{3} DEBUG:`, content)
require.NoError(t, err)
assert.True(t, matched, "Should include timestamp in format HH:MM:SS.mmm, got: %s", content)
}
func TestDebugln_NilSessionWriter(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
sessionLogWriter = nil
debugEnabled = false
// Should not panic when sessionLogWriter is nil
Debugln("test", "message")
}
func TestSetDebugEnabled(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
// Test enabling debug
SetDebugEnabled(true)
assert.True(t, debugEnabled)
// Test disabling debug
SetDebugEnabled(false)
assert.False(t, debugEnabled)
// Test toggling
SetDebugEnabled(true)
assert.True(t, debugEnabled)
}
func TestDebugf_MultipleMessages(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false
Debugf("message 1")
Debugf("message 2")
Debugf("message 3")
content := buf.String()
lines := strings.Split(strings.TrimSpace(content), "\n")
assert.Len(t, lines, 3, "Should have 3 log lines")
assert.Contains(t, lines[0], "message 1")
assert.Contains(t, lines[1], "message 2")
assert.Contains(t, lines[2], "message 3")
}
func TestDebugf_FormatSpecifiers(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false
Debugf("int: %d, string: %s, hex: %02X", 42, "test", 0xAB)
content := buf.String()
assert.Contains(t, content, "int: 42")
assert.Contains(t, content, "string: test")
assert.Contains(t, content, "hex: AB")
}
func TestDebugln_MultipleArgs(t *testing.T) {
origEnabled, origWriter := saveDebugState()
t.Cleanup(func() {
restoreDebugState(origEnabled, origWriter)
})
var buf bytes.Buffer
sessionLogWriter = &buf
debugEnabled = false
Debugln("value1", 42, "value2", true)
content := buf.String()
// fmt.Sprint concatenates without spaces
assert.Contains(t, content, "value142value2true")
}