initial commit
[govpp.git] / vendor / github.com / Sirupsen / logrus / text_formatter_test.go
1 package logrus
2
3 import (
4         "bytes"
5         "errors"
6         "strings"
7         "testing"
8         "time"
9 )
10
11 func TestQuoting(t *testing.T) {
12         tf := &TextFormatter{DisableColors: true}
13
14         checkQuoting := func(q bool, value interface{}) {
15                 b, _ := tf.Format(WithField("test", value))
16                 idx := bytes.Index(b, ([]byte)("test="))
17                 cont := bytes.Contains(b[idx+5:], []byte(tf.QuoteCharacter))
18                 if cont != q {
19                         if q {
20                                 t.Errorf("quoting expected for: %#v", value)
21                         } else {
22                                 t.Errorf("quoting not expected for: %#v", value)
23                         }
24                 }
25         }
26
27         checkQuoting(false, "")
28         checkQuoting(false, "abcd")
29         checkQuoting(false, "v1.0")
30         checkQuoting(false, "1234567890")
31         checkQuoting(true, "/foobar")
32         checkQuoting(true, "x y")
33         checkQuoting(true, "x,y")
34         checkQuoting(false, errors.New("invalid"))
35         checkQuoting(true, errors.New("invalid argument"))
36
37         // Test for custom quote character.
38         tf.QuoteCharacter = "`"
39         checkQuoting(false, "")
40         checkQuoting(false, "abcd")
41         checkQuoting(true, "/foobar")
42         checkQuoting(true, errors.New("invalid argument"))
43
44         // Test for multi-character quotes.
45         tf.QuoteCharacter = "§~±"
46         checkQuoting(false, "abcd")
47         checkQuoting(true, errors.New("invalid argument"))
48
49         // Test for quoting empty fields.
50         tf.QuoteEmptyFields = true
51         checkQuoting(true, "")
52         checkQuoting(false, "abcd")
53         checkQuoting(true, errors.New("invalid argument"))
54 }
55
56 func TestTimestampFormat(t *testing.T) {
57         checkTimeStr := func(format string) {
58                 customFormatter := &TextFormatter{DisableColors: true, TimestampFormat: format}
59                 customStr, _ := customFormatter.Format(WithField("test", "test"))
60                 timeStart := bytes.Index(customStr, ([]byte)("time="))
61                 timeEnd := bytes.Index(customStr, ([]byte)("level="))
62                 timeStr := customStr[timeStart+5+len(customFormatter.QuoteCharacter) : timeEnd-1-len(customFormatter.QuoteCharacter)]
63                 if format == "" {
64                         format = time.RFC3339
65                 }
66                 _, e := time.Parse(format, (string)(timeStr))
67                 if e != nil {
68                         t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e)
69                 }
70         }
71
72         checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
73         checkTimeStr("Mon Jan _2 15:04:05 2006")
74         checkTimeStr("")
75 }
76
77 func TestDisableTimestampWithColoredOutput(t *testing.T) {
78         tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
79
80         b, _ := tf.Format(WithField("test", "test"))
81         if strings.Contains(string(b), "[0000]") {
82                 t.Error("timestamp not expected when DisableTimestamp is true")
83         }
84 }
85
86 // TODO add tests for sorting etc., this requires a parser for the text
87 // formatter output.