103 lines
2.9 KiB
Go
103 lines
2.9 KiB
Go
package starlog
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func forceColorForKeywordTest(t *testing.T) func() {
|
|
t.Helper()
|
|
old := NoColor
|
|
NoColor = false
|
|
return func() {
|
|
NoColor = old
|
|
}
|
|
}
|
|
|
|
func TestKeywordMatchDefaultCaseSensitive(t *testing.T) {
|
|
defer forceColorForKeywordTest(t)()
|
|
|
|
core := newLogCore(nil)
|
|
core.keywordColors["error"] = []Attr{FgRed}
|
|
|
|
got := core.highlightKeywords("ERROR error")
|
|
if strings.Contains(got, NewColor(FgRed).Sprint("ERROR")) {
|
|
t.Fatalf("default keyword match should be case-sensitive")
|
|
}
|
|
if !strings.Contains(got, NewColor(FgRed).Sprint("error")) {
|
|
t.Fatalf("default keyword match should highlight exact keyword")
|
|
}
|
|
}
|
|
|
|
func TestKeywordMatchIgnoreCase(t *testing.T) {
|
|
defer forceColorForKeywordTest(t)()
|
|
|
|
core := newLogCore(nil)
|
|
core.keywordColors["error"] = []Attr{FgRed}
|
|
core.keywordMatchOptions = KeywordMatchOptions{IgnoreCase: true}
|
|
|
|
got := core.highlightKeywords("ERROR error Error")
|
|
if !strings.Contains(got, NewColor(FgRed).Sprint("ERROR")) {
|
|
t.Fatalf("ignore-case keyword match should highlight uppercase variant")
|
|
}
|
|
if !strings.Contains(got, NewColor(FgRed).Sprint("Error")) {
|
|
t.Fatalf("ignore-case keyword match should highlight title-case variant")
|
|
}
|
|
}
|
|
|
|
func TestKeywordMatchWholeWord(t *testing.T) {
|
|
defer forceColorForKeywordTest(t)()
|
|
|
|
core := newLogCore(nil)
|
|
core.keywordColors["error"] = []Attr{FgRed}
|
|
core.keywordMatchOptions = KeywordMatchOptions{WholeWord: true}
|
|
|
|
got := core.highlightKeywords("error errors xerror error_x error")
|
|
colored := NewColor(FgRed).Sprint("error")
|
|
if strings.Count(got, colored) != 2 {
|
|
t.Fatalf("whole-word keyword match should only highlight standalone words, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestKeywordMatchWholeWordIgnoreCase(t *testing.T) {
|
|
defer forceColorForKeywordTest(t)()
|
|
|
|
core := newLogCore(nil)
|
|
core.keywordColors["error"] = []Attr{FgRed}
|
|
core.keywordMatchOptions = KeywordMatchOptions{
|
|
IgnoreCase: true,
|
|
WholeWord: true,
|
|
}
|
|
|
|
got := core.highlightKeywords("ERROR errors Error")
|
|
if !strings.Contains(got, NewColor(FgRed).Sprint("ERROR")) {
|
|
t.Fatalf("whole-word+ignore-case should highlight ERROR")
|
|
}
|
|
if !strings.Contains(got, NewColor(FgRed).Sprint("Error")) {
|
|
t.Fatalf("whole-word+ignore-case should highlight Error")
|
|
}
|
|
if strings.Contains(got, NewColor(FgRed).Sprint("error")+"s") {
|
|
t.Fatalf("whole-word+ignore-case should not highlight partial words")
|
|
}
|
|
}
|
|
|
|
func TestKeywordMatchOptionsConfigRoundTrip(t *testing.T) {
|
|
logger := NewStarlog(nil)
|
|
logger.SetKeywordMatchOptions(KeywordMatchOptions{
|
|
IgnoreCase: true,
|
|
WholeWord: true,
|
|
})
|
|
|
|
cfg := logger.GetConfig()
|
|
if !cfg.KeywordMatch.IgnoreCase || !cfg.KeywordMatch.WholeWord {
|
|
t.Fatalf("config snapshot should include keyword match options")
|
|
}
|
|
|
|
logger.SetKeywordMatchOptions(KeywordMatchOptions{})
|
|
logger.ApplyConfig(cfg)
|
|
got := logger.GetKeywordMatchOptions()
|
|
if !got.IgnoreCase || !got.WholeWord {
|
|
t.Fatalf("ApplyConfig should restore keyword match options")
|
|
}
|
|
}
|