12 func TestFormatting(t *testing.T) {
13 tf := &TextFormatter{DisableColors: true}
15 testCases := []struct {
19 {`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo\n"},
22 for _, tc := range testCases {
23 b, _ := tf.Format(WithField("test", tc.value))
25 if string(b) != tc.expected {
26 t.Errorf("formatting expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
31 func TestQuoting(t *testing.T) {
32 tf := &TextFormatter{DisableColors: true}
34 checkQuoting := func(q bool, value interface{}) {
35 b, _ := tf.Format(WithField("test", value))
36 idx := bytes.Index(b, ([]byte)("test="))
37 cont := bytes.Contains(b[idx+5:], []byte("\""))
40 t.Errorf("quoting expected for: %#v", value)
42 t.Errorf("quoting not expected for: %#v", value)
47 checkQuoting(false, "")
48 checkQuoting(false, "abcd")
49 checkQuoting(false, "v1.0")
50 checkQuoting(false, "1234567890")
51 checkQuoting(false, "/foobar")
52 checkQuoting(false, "foo_bar")
53 checkQuoting(false, "foo@bar")
54 checkQuoting(false, "foobar^")
55 checkQuoting(false, "+/-_^@f.oobar")
56 checkQuoting(true, "foobar$")
57 checkQuoting(true, "&foobar")
58 checkQuoting(true, "x y")
59 checkQuoting(true, "x,y")
60 checkQuoting(false, errors.New("invalid"))
61 checkQuoting(true, errors.New("invalid argument"))
63 // Test for quoting empty fields.
64 tf.QuoteEmptyFields = true
65 checkQuoting(true, "")
66 checkQuoting(false, "abcd")
67 checkQuoting(true, errors.New("invalid argument"))
70 func TestEscaping(t *testing.T) {
71 tf := &TextFormatter{DisableColors: true}
73 testCases := []struct {
81 for _, tc := range testCases {
82 b, _ := tf.Format(WithField("test", tc.value))
83 if !bytes.Contains(b, []byte(tc.expected)) {
84 t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
89 func TestEscaping_Interface(t *testing.T) {
90 tf := &TextFormatter{DisableColors: true}
94 testCases := []struct {
98 {ts, fmt.Sprintf("\"%s\"", ts.String())},
99 {errors.New("error: something went wrong"), "\"error: something went wrong\""},
102 for _, tc := range testCases {
103 b, _ := tf.Format(WithField("test", tc.value))
104 if !bytes.Contains(b, []byte(tc.expected)) {
105 t.Errorf("escaping expected for %q (result was %q instead of %q)", tc.value, string(b), tc.expected)
110 func TestTimestampFormat(t *testing.T) {
111 checkTimeStr := func(format string) {
112 customFormatter := &TextFormatter{DisableColors: true, TimestampFormat: format}
113 customStr, _ := customFormatter.Format(WithField("test", "test"))
114 timeStart := bytes.Index(customStr, ([]byte)("time="))
115 timeEnd := bytes.Index(customStr, ([]byte)("level="))
116 timeStr := customStr[timeStart+5+len("\"") : timeEnd-1-len("\"")]
118 format = time.RFC3339
120 _, e := time.Parse(format, (string)(timeStr))
122 t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e)
126 checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
127 checkTimeStr("Mon Jan _2 15:04:05 2006")
131 func TestDisableTimestampWithColoredOutput(t *testing.T) {
132 tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
134 b, _ := tf.Format(WithField("test", "test"))
135 if strings.Contains(string(b), "[0000]") {
136 t.Error("timestamp not expected when DisableTimestamp is true")
140 // TODO add tests for sorting etc., this requires a parser for the text