2024-10-01 13:47:50 +08:00
|
|
|
package gologger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormat(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
logEvent LogEvent
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Info level with format string",
|
|
|
|
logEvent: LogEvent{
|
|
|
|
Ts: time.Now(),
|
|
|
|
Category: "test_category",
|
|
|
|
Level: Info,
|
|
|
|
Data: []interface{}{"%s: %v", "key", "value"},
|
|
|
|
},
|
|
|
|
want: "test_category : INFO - key: value",
|
|
|
|
},
|
2024-10-01 15:55:21 +08:00
|
|
|
{
|
|
|
|
name: "Info level with one format string",
|
|
|
|
logEvent: LogEvent{
|
|
|
|
Ts: time.Now(),
|
|
|
|
Category: "test_category",
|
|
|
|
Level: Info,
|
|
|
|
Data: []interface{}{"%s", "key"},
|
|
|
|
},
|
|
|
|
want: "test_category : INFO - key",
|
|
|
|
},
|
2024-10-01 13:47:50 +08:00
|
|
|
{
|
|
|
|
name: "Error level without format string",
|
|
|
|
logEvent: LogEvent{
|
|
|
|
Ts: time.Now(),
|
|
|
|
Category: "test_category",
|
|
|
|
Level: Error,
|
|
|
|
Data: []interface{}{"error occurred", "additional info"},
|
|
|
|
},
|
|
|
|
want: "test_category : ERROR - error occurred additional info",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if got := format(tt.logEvent); strings.Index(got, tt.want) == -1 {
|
|
|
|
t.Errorf("format() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|