44 lines
926 B
Go
44 lines
926 B
Go
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"regexp"
|
|
"testing"
|
|
)
|
|
|
|
type TestWriter struct {
|
|
str string
|
|
}
|
|
|
|
func TestLogging(t *testing.T) {
|
|
t.Run("basic logging", func(t *testing.T) {
|
|
var strWriter, errWriter *bytes.Buffer
|
|
|
|
strWriter = bytes.NewBuffer(make([]byte, 0, 10000))
|
|
errWriter = bytes.NewBuffer(make([]byte, 0, 10000))
|
|
|
|
stdLogger, errLogger := CreateStdoutLogger(strWriter, errWriter)
|
|
errLogger.Printf("%d\n", 100)
|
|
stdLogger.Printf("OKA %d Done\n", 77)
|
|
stdLogger.Println("Another line")
|
|
|
|
const prefixMatch = `\[\d+\]: `
|
|
|
|
err := errWriter.String()
|
|
if ok, err := regexp.MatchString("^"+prefixMatch+"100\n$", err); !ok {
|
|
if err != nil {
|
|
t.Logf("err: %v", err)
|
|
}
|
|
t.Fatal(err)
|
|
}
|
|
str := strWriter.String()
|
|
if ok, err := regexp.MatchString("^"+prefixMatch+"OKA 77 Done\n"+prefixMatch+"Another line\n$", str); !ok {
|
|
if err != nil {
|
|
t.Logf("err: %v", err)
|
|
}
|
|
t.Fatal(str)
|
|
}
|
|
})
|
|
}
|
|
|