60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
//go:build go1.18
|
|
// +build go1.18
|
|
|
|
package starlog
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func FuzzTextAndJSONFormatter(f *testing.F) {
|
|
f.Add("hello", "user_id", "42")
|
|
f.Add("error happened", "token", "abc123")
|
|
|
|
f.Fuzz(func(t *testing.T, message string, key string, value string) {
|
|
entry := &Entry{
|
|
Context: context.Background(),
|
|
Level: LvInfo,
|
|
LevelName: "INFO",
|
|
Message: message,
|
|
Fields: Fields{},
|
|
}
|
|
if key != "" {
|
|
entry.Fields[key] = value
|
|
}
|
|
|
|
text, err := NewTextFormatter().Format(entry)
|
|
if err != nil {
|
|
t.Fatalf("text format failed: %v", err)
|
|
}
|
|
if len(text) == 0 && (message != "" || len(entry.Fields) > 0) {
|
|
t.Fatalf("text formatter output is unexpectedly empty")
|
|
}
|
|
|
|
js, err := NewJSONFormatter().Format(entry)
|
|
if err != nil {
|
|
t.Fatalf("json format failed: %v", err)
|
|
}
|
|
if len(js) == 0 {
|
|
t.Fatalf("json formatter output is unexpectedly empty")
|
|
}
|
|
})
|
|
}
|
|
|
|
func FuzzKeywordHighlight(f *testing.F) {
|
|
f.Add("panic at line", "panic")
|
|
f.Add("token leaked", "token")
|
|
|
|
f.Fuzz(func(t *testing.T, input string, keyword string) {
|
|
core := newLogCore(nil)
|
|
core.showColor = true
|
|
core.onlyColorLevel = true
|
|
core.keywordColors = map[string][]Attr{}
|
|
if keyword != "" {
|
|
core.keywordColors[keyword] = []Attr{FgRed}
|
|
}
|
|
_ = core.highlightKeywords(input)
|
|
})
|
|
}
|