52 lines
1.6 KiB
Go
52 lines
1.6 KiB
Go
package formatters
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"codeark.it/Bithero-Agency-Go/logfrog-go"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func NewTestStyles() *TextFormatterStyles {
|
|
return &TextFormatterStyles{
|
|
DateTimeStyle: []CliOption{ FgHiBlack },
|
|
FileStyle: []CliOption{ FgGreen },
|
|
CallerStyle: []CliOption{ FgBlue },
|
|
}
|
|
}
|
|
|
|
func TestTextFormatter(t *testing.T) {
|
|
|
|
created, err := time.Parse(time.RFC3339Nano, "2006-01-02T15:04:05.999999999+07:00")
|
|
assert.NoError(t, err)
|
|
|
|
r := logfrog.NewLogRecord(0, created, logfrog.INFO, "hello world", logfrog.Fields{
|
|
"animal": "cat",
|
|
"color": "red",
|
|
})
|
|
|
|
t.Run("colorized LstdFlags", func(t *testing.T) {
|
|
f := NewTextFormatter(true, LstdFlags)
|
|
result, err := f.Format(r)
|
|
assert.NoError(t, err)
|
|
|
|
const line = "\x1b[36m2006/01/02 15:04:05 \x1b[0m\x1b[32mtext_test.go:25:\x1b[0m\x1b[32mTestTextFormatter()\x1b[0m \x1b[1;32m[I]\x1b[0m hello world [\x1b[1;32manimal\x1b[0m=cat \x1b[1;32mcolor\x1b[0m=red]\n"
|
|
|
|
fmt.Print(string(result))
|
|
assert.Equal(t, line, string(result))
|
|
})
|
|
|
|
t.Run("colorized LstdFlags custom colors", func(t *testing.T) {
|
|
f := NewTextFormatterWithStyles(true, LstdFlags, NewTestStyles())
|
|
result, err := f.Format(r)
|
|
assert.NoError(t, err)
|
|
|
|
const line = "\x1b[90m2006/01/02 15:04:05 \x1b[0m\x1b[32mtext_test.go:25:\x1b[0m\x1b[34mTestTextFormatter()\x1b[0m \x1b[1;32m[I]\x1b[0m hello world [\x1b[1;32manimal\x1b[0m=cat \x1b[1;32mcolor\x1b[0m=red]\n"
|
|
|
|
fmt.Print(string(result))
|
|
assert.Equal(t, line, string(result))
|
|
})
|
|
|
|
} |