-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools_test.go
354 lines (318 loc) · 7.91 KB
/
tools_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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package log
import (
"encoding/json"
"fmt"
"runtime"
"strings"
"testing"
"time"
"github.com/goloop/log/layout"
"github.com/goloop/log/level"
"github.com/goloop/trit"
)
// TetsIoCopy tests ioCopy function.
func TestIoCopy(t *testing.T) {
input := "Hello, World!"
r := strings.NewReader(input)
c := make(chan string)
go ioCopy(r, c)
result := <-c
if result != input {
t.Errorf("ioCopy failed, expected %v, got %v", input, result)
}
}
// TestGetStackFrame tests getStackFrame function.
func TestGetStackFrame(t *testing.T) {
frame := getStackFrame(2) // cuerrent function is TestGetStackFrame
if frame == nil {
t.Fatal("Expected frame to not be nil")
}
if frame.FuncName == "" {
t.Errorf("Expected FuncName to not be empty")
}
if frame.FilePath == "" {
t.Errorf("Expected FilePath to not be empty")
}
if frame.FileLine == 0 {
t.Errorf("Expected FileLine to not be zero")
}
if frame.FuncAddress == 0 {
t.Errorf("Expected FuncAddress to not be zero")
}
// Verify the function name
expectedFuncName := "TestGetStackFrame"
if frame.FuncName != expectedFuncName {
t.Errorf("Expected function name to be '%s', got '%s'",
expectedFuncName, frame.FuncName)
}
// Verify the file path
_, fileName, _, _ := runtime.Caller(0)
if !strings.Contains(frame.FilePath, fileName) {
t.Errorf("Expected file path '%s' to contain '%s'",
frame.FilePath, fileName)
}
}
// TestGetStackFramePanicsOnNegativeSkip tests getStackFrame for panic.
func TestGetStackFramePanicsOnLargeSkip(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("The code did not panic")
}
}()
getStackFrame(1024)
}
// TestCutFilePath tests cutFilePath function.
func TestCutFilePath(t *testing.T) {
tests := []struct {
name string
n int
path string
expected string
}{
{
name: "Test case 1: Three sections, cut to two",
n: 2,
path: "/path/to/file",
expected: ".../to/file",
},
{
name: "Test case 2: Four sections, cut to two",
n: 2,
path: "/path/to/another/file",
expected: ".../another/file",
},
{
name: "Test case 3: One section, cut to two",
n: 2,
path: "/file",
expected: "/file",
},
{
name: "Test case 4: Three sections, cut to three",
n: 3,
path: "/path/to/file",
expected: "/path/to/file",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := cutFilePath(test.n, test.path)
if result != test.expected {
t.Errorf("Expected '%s', got '%s'", test.expected, result)
}
})
}
}
// TestTextMessage tests textMessage function.
func TestTextMessage(t *testing.T) {
prefix := "test"
level := level.Info
timestamp := time.Now()
output := &Stdout
output.WithColor = trit.True
output.Layouts = output.Layouts | layout.LineNumber | layout.FuncAddress
stackframe := getStackFrame(2)
tests := []struct {
name string
f string
a []any
e string
}{
{
name: "Text message with formatted string",
f: "formatted string %s",
a: []any{"value"},
e: "formatted string value",
},
{
name: "Text message with multiple formatted values",
f: "formatted string with multiple values %s %d",
a: []any{"value", 1},
e: "formatted string with multiple values value 1",
},
{
name: "Text message with no formatting",
f: "",
a: []any{"value"},
e: "value",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := textMessage(
prefix,
level,
timestamp,
output,
stackframe,
test.f,
test.a...,
)
if !strings.Contains(result, test.e) {
t.Errorf("Message '%s' doesn't contains '%s'", result, test.e)
}
})
}
// Change layouts.
output.Layouts = layout.FullFilePath
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := textMessage(
prefix,
level,
timestamp,
output,
stackframe,
test.f,
test.a...,
)
if !strings.Contains(result, test.e) {
t.Errorf("Message '%s' doesn't contains '%s'", result, test.e)
}
})
}
// Change layouts.
output.Layouts = layout.LineNumber | layout.FuncAddress
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := textMessage(
prefix,
level,
timestamp,
output,
stackframe,
test.f,
test.a...,
)
if !strings.Contains(result, test.e) {
t.Errorf("Message '%s' doesn't contains '%s'", result, test.e)
}
})
}
}
// TestObjectMessage tests objectMessage function.
func TestObjectMessage(t *testing.T) {
prefix := "test"
level := level.Info
timestamp := time.Now()
output := &Stdout
stackframe := getStackFrame(2)
tests := []struct {
name string
f string
a []any
e map[string]interface{}
}{
{
name: "Object message with formatted string",
f: "formatted string %s",
a: []any{"value"},
e: map[string]interface{}{
"prefix": prefix,
"level": "INFO",
"timestamp": timestamp.Format(output.TimestampFormat),
"message": "formatted string value",
"filePath": stackframe.FilePath,
"funcName": stackframe.FuncName,
"funcAddress": fmt.Sprintf("%#x", stackframe.FuncAddress),
"lineNumber": stackframe.FileLine,
},
},
{
name: "Object message with multiple formatted values",
f: "formatted string with multiple values %s %d",
a: []any{"value", 1},
e: map[string]interface{}{
"prefix": prefix,
"level": "INFO",
"timestamp": timestamp.Format(output.TimestampFormat),
"message": "formatted string with multiple values value 1",
"filePath": stackframe.FilePath,
"funcName": stackframe.FuncName,
"funcAddress": fmt.Sprintf("%#x", stackframe.FuncAddress),
"lineNumber": stackframe.FileLine,
},
},
{
name: "Object message with no formatting",
f: "",
a: []any{"value"},
e: map[string]interface{}{
"prefix": prefix,
"level": "INFO",
"timestamp": timestamp.Format(output.TimestampFormat),
"message": "value",
"filePath": stackframe.FilePath,
"funcName": stackframe.FuncName,
"funcAddress": fmt.Sprintf("%#x", stackframe.FuncAddress),
"lineNumber": stackframe.FileLine,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := objectMessage(
prefix,
level,
timestamp,
output,
stackframe,
test.f,
test.a...,
)
// Unmarshal the JSON result into a map
var resultObj map[string]interface{}
err := json.Unmarshal([]byte(result), &resultObj)
if err != nil {
t.Fatalf("Failed to unmarshal JSON: %v", err)
}
if resultObj["level"] != test.e["level"] ||
resultObj["prefix"] != test.e["prefix"] ||
resultObj["message"] != test.e["message"] {
t.Errorf("Expected '%v', got '%v'", test.e, resultObj)
}
})
}
}
/*
// TestGetWriterID tests getWriterID function.
func TestGetWriterID(t *testing.T) {
// Create several types that satisfy the io.Writer interface
file, err := os.Create("test.txt")
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
defer os.Remove("test.txt")
defer file.Close()
buffer := &bytes.Buffer{}
builder := &strings.Builder{}
writer := bufio.NewWriter(buffer)
gzipWriter := gzip.NewWriter(buffer)
pipeReader, pipeWriter := io.Pipe()
defer pipeReader.Close()
defer pipeWriter.Close()
tests := []struct {
name string
input io.Writer
}{
{"os.File", file},
{"bytes.Buffer", buffer},
{"strings.Builder", builder},
{"bufio.Writer", writer},
{"gzip.Writer", gzipWriter},
{"io.PipeWriter", pipeWriter},
}
// Map to store the IDs of the writers
writerIDs := make(map[uintptr]bool)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
id := getWriterID(test.input)
if _, exists := writerIDs[id]; exists {
t.Errorf("Non-unique writer ID returned for type: %s",
test.name)
}
writerIDs[id] = true
})
}
}
*/