-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_test.go
More file actions
98 lines (83 loc) · 2.33 KB
/
debug_test.go
File metadata and controls
98 lines (83 loc) · 2.33 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
package jin
import (
"bytes"
"errors"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsDebugging(t *testing.T) {
SetMode(DebugMode)
assert.True(t, IsDebugging())
SetMode(ReleaseMode)
assert.False(t, IsDebugging())
SetMode(TestMode)
assert.False(t, IsDebugging())
}
func TestDebugPrint(t *testing.T) {
oldWriter := DefaultWriter
r, w, _ := os.Pipe()
DefaultWriter = w
SetMode(DebugMode)
debugPrint("hello %s", "world")
w.Close()
outputBytes, _ := io.ReadAll(r)
output := string(outputBytes)
assert.Contains(t, output, "[JIN-debug] hello world")
r, w, _ = os.Pipe()
DefaultWriter = w
debugPrintWARNINGNew()
w.Close()
outputBytes, _ = io.ReadAll(r)
output = string(outputBytes)
assert.Contains(t, output, "[JIN-debug] [WARNING] Running in \"debug\" mode.")
DefaultWriter = oldWriter
SetMode(TestMode)
}
func TestDebugPrintError(t *testing.T) {
oldErrorWriter := DefaultErrorWriter
r, w, _ := os.Pipe()
DefaultErrorWriter = w
SetMode(DebugMode)
err := errors.New("test error")
debugPrintError(err)
w.Close()
outputBytes, _ := io.ReadAll(r)
output := string(outputBytes)
assert.Contains(t, output, "[JIN-debug] [ERROR] test error")
r, w, _ = os.Pipe()
DefaultErrorWriter = w
debugPrintError(nil)
w.Close()
outputBytes, _ = io.ReadAll(r)
output = string(outputBytes)
assert.Equal(t, "", output)
DefaultErrorWriter = oldErrorWriter
SetMode(TestMode)
}
func TestDebugPrintRoute(t *testing.T) {
oldWriter := DefaultWriter
var out bytes.Buffer
DefaultWriter = &out
SetMode(DebugMode)
handler := func(c *Context) {}
handlers := HandlersChain{handler}
debugPrintRoute("GET", "/test", handlers)
assert.Contains(t, out.String(), "[JIN-debug] GET")
assert.Contains(t, out.String(), "/test")
assert.Contains(t, out.String(), "TestDebugPrintRoute.func1")
assert.Contains(t, out.String(), "(1 handlers)")
out.Reset()
DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
DefaultWriter.Write([]byte(strings.Join([]string{httpMethod, absolutePath, handlerName, "custom"}, " ")))
}
debugPrintRoute("POST", "/custom", handlers)
assert.Contains(t, out.String(), "POST /custom")
assert.Contains(t, out.String(), "TestDebugPrintRoute.func1")
assert.Contains(t, out.String(), "custom")
DefaultWriter = oldWriter
DebugPrintRouteFunc = nil
SetMode(TestMode)
}