forked from fydemy/console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole_test.go
More file actions
158 lines (135 loc) · 2.46 KB
/
console_test.go
File metadata and controls
158 lines (135 loc) · 2.46 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
package console
import (
"bytes"
"io"
"os"
"testing"
)
type TestUser struct {
ID int `json:"id"`
Name string `json:"name"`
}
func captureOutput(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = old
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}
func TestLog(t *testing.T) {
tests := []struct {
name string
input interface{}
}{
{"string", "hello world"},
{"integer", 42},
{"float", 3.14},
{"boolean true", true},
{"boolean false", false},
{"nil", nil},
{"struct", TestUser{ID: 1, Name: "John"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := captureOutput(func() {
Log(tt.input)
})
if output == "" {
t.Errorf("Log() produced no output for %v", tt.input)
}
})
}
}
func TestTable(t *testing.T) {
tests := []struct {
name string
input interface{}
}{
{
"slice of structs",
[]TestUser{
{ID: 1, Name: "John"},
{ID: 2, Name: "Jane"},
},
},
{
"slice of integers",
[]int{1, 2, 3, 4, 5},
},
{
"empty slice",
[]TestUser{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := captureOutput(func() {
Table(tt.input)
})
if output == "" {
t.Errorf("Table() produced no output for %v", tt.input)
}
})
}
}
func TestTableWithNonSlice(t *testing.T) {
output := captureOutput(func() {
Table("not a slice")
})
if output == "" {
t.Error("Table() should produce error output for non-slice input")
}
}
func TestLogLevels(t *testing.T) {
tests := []struct {
name string
fn func(interface{})
}{
{"Info", Info},
{"Warn", Warn},
{"Error", Error},
{"Success", Success},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
output := captureOutput(func() {
tt.fn("test message")
})
if output == "" {
t.Errorf("%s() produced no output", tt.name)
}
})
}
}
func TestDebug(t *testing.T) {
output := captureOutput(func() {
Debug("test label", map[string]string{"key": "value"})
})
if output == "" {
t.Error("Debug() produced no output")
}
}
func BenchmarkLog(b *testing.B) {
user := TestUser{ID: 1, Name: "John Doe"}
for i := 0; i < b.N; i++ {
captureOutput(func() {
Log(user)
})
}
}
func BenchmarkTable(b *testing.B) {
users := []TestUser{
{ID: 1, Name: "John"},
{ID: 2, Name: "Jane"},
{ID: 3, Name: "Bob"},
}
for i := 0; i < b.N; i++ {
captureOutput(func() {
Table(users)
})
}
}