1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
package logr
import (
"bytes"
"strings"
"testing"
)
func TestNew(t *testing.T) {
logger := New("what", false, nil)
if logger.threshold != LevelInfo {
t.Error("expected invalid threshold to be silently set to INFO, got", logger.threshold)
}
}
func TestLogger(t *testing.T) {
var writer bytes.Buffer
logger := New(LevelWarn, false, &writer)
logger.Debug("this is debug")
logger.Info("this is info")
logger.Warn("this is warn")
logger.Error("this is error")
output := writer.String()
if numberOfNewLines := strings.Count(output, "\n"); numberOfNewLines != 2 {
t.Error("expected 2 newlines, got", numberOfNewLines)
}
if strings.Contains(output, "this is debug") {
t.Error("expected no debug message, got", output)
}
if strings.Contains(output, "this is info") {
t.Error("expected no info message, got", output)
}
if !strings.Contains(output, "this is warn") {
t.Error("expected warn message, got", output)
}
if !strings.Contains(output, "this is error") {
t.Error("expected error message, got", output)
}
}
func TestLogger_SetThreshold(t *testing.T) {
var writer bytes.Buffer
logger := New(LevelDebug, false, &writer)
logger.SetThreshold(LevelError)
logger.Debug("this is debug")
logger.Info("this is info")
logger.Warn("this is warn")
logger.Error("this is error")
output := writer.String()
if numberOfNewLines := strings.Count(output, "\n"); numberOfNewLines != 1 {
t.Error("expected 1 newline, got", numberOfNewLines)
}
}
func TestLoggerFormatting(t *testing.T) {
var writer bytes.Buffer
logger := New(LevelDebug, true, &writer)
logger.Debugf("hello, %s", "world")
logger.Infof("%d", 11111)
logger.Warnf("%s got %d%% in math", "John Doe", 87)
logger.Errorf("%s sent %s$%.02f to %s", "John", "CAD", 69.1, "Jane")
output := writer.String()
if !strings.Contains(output, "- DEBUG - hello, world") {
t.Error("expected '- DEBUG - hello, world.', got", output)
}
if !strings.Contains(output, "- INFO - 11111") {
t.Error("expected '- INFO - John Doe got 87% in math', got", output)
}
if !strings.Contains(output, "- WARN - John Doe got 87% in math") {
t.Error("expected '- WARN - John Doe got 87% in math', got", output)
}
if !strings.Contains(output, "- ERROR - John sent CAD$69.10 to Jane") {
t.Error("expected '- ERROR - John sent USD$123.40 to Jane', got", output)
}
}
|