package colog import ( "log" "testing" "time" ) type formatTest struct { entry Entry prefix string flags int width int colors bool output string } // TTime is the fixed point in time for all formatting tests var TTime = time.Date(2015, time.August, 1, 20, 45, 30, 9999, time.UTC) var formatterTests = []formatTest{ { entry: Entry{ Level: LInfo, Message: []byte("some message"), }, output: "[INF] some message\n", }, { entry: Entry{ Time: TTime, Level: LDebug, Message: []byte("some message"), }, flags: log.Ldate, output: "[DBG] 2015/08/01 some message\n", }, { entry: Entry{ Time: TTime, Level: LDebug, Message: []byte("some message"), }, colors: true, width: 40, flags: log.Ldate, output: "[\x1b[0;36mDBG\x1b[0m] 2015/08/01 some message\n", }, { entry: Entry{ Time: TTime, Level: LDebug, Message: []byte("some message"), }, colors: true, width: 140, flags: log.Ldate, output: "[\x1b[0;36mDBG\x1b[0m] 2015/08/01 some message\n"}, { entry: Entry{ Time: TTime, Level: LDebug, File: "/src/file.go", Line: 142, Message: []byte("some message"), }, colors: true, width: 140, flags: log.Llongfile, output: "[\x1b[0;36mDBG\x1b[0m] \x1b[1;30msrc/file.go:142:\x1b[0m some message\n", }, } func TestStdFormatter(t *testing.T) { for _, tt := range formatterTests { f := StdFormatter{ Flag: tt.flags, Colors: tt.colors, } if tt.width > 0 { terminalWidth = fixedWidthTerminal(tt.width) } b, err := f.Format(&tt.entry) if err != nil { t.Fatal(err) } if string(b) != tt.output { t.Errorf("Unexpected formatter output:\n%s\nVS\n%s", b, tt.output) } } } // stub for terminal width function func fixedWidthTerminal(width int) func(int) int { return func(fd int) int { return width } }