72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
|
|
package starlog
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestAutoAppendNewlineDefaultOff(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
logger := newStructuredTestLogger(&buf)
|
||
|
|
logger.Infof("hello %d", 1)
|
||
|
|
got := buf.String()
|
||
|
|
if strings.HasSuffix(got, "\n") {
|
||
|
|
t.Fatalf("default behavior should keep no trailing newline for Infof, got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAutoAppendNewlineForInfof(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
logger := newStructuredTestLogger(&buf)
|
||
|
|
logger.SetAutoAppendNewline(true)
|
||
|
|
logger.Infof("hello %d", 2)
|
||
|
|
got := buf.String()
|
||
|
|
if !strings.HasSuffix(got, "\n") {
|
||
|
|
t.Fatalf("Infof should auto append trailing newline when enabled, got %q", got)
|
||
|
|
}
|
||
|
|
if strings.Count(got, "\n") != 1 {
|
||
|
|
t.Fatalf("Infof should append only one newline, got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAutoAppendNewlineNoDoubleForInfoln(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
logger := newStructuredTestLogger(&buf)
|
||
|
|
logger.SetAutoAppendNewline(true)
|
||
|
|
logger.Infoln("line")
|
||
|
|
got := buf.String()
|
||
|
|
if strings.Count(got, "\n") != 1 {
|
||
|
|
t.Fatalf("Infoln should keep single newline with auto append enabled, got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAutoAppendNewlineForWritef(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
logger := newStructuredTestLogger(&buf)
|
||
|
|
logger.SetAutoAppendNewline(true)
|
||
|
|
logger.Writef("raw-%d", 3)
|
||
|
|
got := buf.String()
|
||
|
|
if got != "raw-3\n" {
|
||
|
|
t.Fatalf("Writef should auto append newline when enabled, got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestAutoAppendNewlineConfigApply(t *testing.T) {
|
||
|
|
var buf bytes.Buffer
|
||
|
|
logger := newStructuredTestLogger(&buf)
|
||
|
|
cfg := logger.GetConfig()
|
||
|
|
if cfg.AutoAppendNewline {
|
||
|
|
t.Fatalf("default AutoAppendNewline should be false")
|
||
|
|
}
|
||
|
|
cfg.AutoAppendNewline = true
|
||
|
|
logger.ApplyConfig(cfg)
|
||
|
|
if !logger.GetAutoAppendNewline() {
|
||
|
|
t.Fatalf("AutoAppendNewline should be true after ApplyConfig")
|
||
|
|
}
|
||
|
|
logger.Infof("cfg")
|
||
|
|
if !strings.HasSuffix(buf.String(), "\n") {
|
||
|
|
t.Fatalf("Infof should append newline after config apply")
|
||
|
|
}
|
||
|
|
}
|