-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_test.go
More file actions
71 lines (58 loc) · 1.82 KB
/
Copy pathdebug_test.go
File metadata and controls
71 lines (58 loc) · 1.82 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
//go:build debug
// +build debug
package utc
import (
"bytes"
"os"
"strings"
"testing"
)
// TestDebugLogging verifies that debug logging works when enabled
func TestDebugLogging(t *testing.T) {
// Capture stderr
old := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
// Test nil receiver calls
var nilTime *Time
// These should log to stderr in debug mode
_, _ = nilTime.MarshalJSON()
_ = nilTime.UnmarshalJSON([]byte(`"2024-01-02T15:04:05Z"`))
_ = nilTime.UnmarshalText([]byte("2024-01-02T15:04:05Z"))
_ = nilTime.Scan("2024-01-02T15:04:05Z")
// Restore stderr and read output
w.Close()
os.Stderr = old
var buf bytes.Buffer
buf.ReadFrom(r)
output := buf.String()
// Verify debug logs were written
expectedLogs := []string{
"MarshalJSON() called on nil *Time receiver",
"UnmarshalJSON() called on nil *Time receiver",
"UnmarshalText() called on nil *Time receiver",
"Scan() called on nil *Time receiver",
}
for _, expected := range expectedLogs {
if !strings.Contains(output, expected) {
t.Errorf("Expected debug log containing %q, but got: %s", expected, output)
}
}
// Verify the format includes timestamp and file info
if !strings.Contains(output, "[UTC DEBUG]") {
t.Error("Debug logs should include [UTC DEBUG] prefix")
}
}
// TestDebugLogDirect tests the debugLog function directly
func TestDebugLogDirect(t *testing.T) {
// This test just verifies debugLog doesn't panic and initializes correctly
// The actual output verification is done in TestDebugLogging which captures
// output in a more controlled way by testing real usage patterns
// This should not panic
debugLog("test message: %s", "hello")
debugLog("another test")
// Verify debugLogger is initialized (it should be after first call)
if debugLogger == nil {
t.Error("debugLogger should be initialized after debugLog calls")
}
}