OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / sirupsen / logrus / text_formatter_test.go
1 package logrus
2
3 import (
4         "bytes"
5         "errors"
6         "fmt"
7         "strings"
8         "testing"
9         "time"
10 )
11
12 func TestFormatting(t *testing.T) {
13         tf := &TextFormatter{DisableColors: true}
14
15         testCases := []struct {
16                 value    string
17                 expected string
18         }{
19                 {`foo`, "time=\"0001-01-01T00:00:00Z\" level=panic test=foo\n"},
20         }
21
22         for _, tc := range testCases {
23                 b, _ := tf.Format(WithField("test", tc.value))
24
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)
27                 }
28         }
29 }
30
31 func TestQuoting(t *testing.T) {
32         tf := &TextFormatter{DisableColors: true}
33
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("\""))
38                 if cont != q {
39                         if q {
40                                 t.Errorf("quoting expected for: %#v", value)
41                         } else {
42                                 t.Errorf("quoting not expected for: %#v", value)
43                         }
44                 }
45         }
46
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"))
62
63         // Test for quoting empty fields.
64         tf.QuoteEmptyFields = true
65         checkQuoting(true, "")
66         checkQuoting(false, "abcd")
67         checkQuoting(true, errors.New("invalid argument"))
68 }
69
70 func TestEscaping(t *testing.T) {
71         tf := &TextFormatter{DisableColors: true}
72
73         testCases := []struct {
74                 value    string
75                 expected string
76         }{
77                 {`ba"r`, `ba\"r`},
78                 {`ba'r`, `ba'r`},
79         }
80
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)
85                 }
86         }
87 }
88
89 func TestEscaping_Interface(t *testing.T) {
90         tf := &TextFormatter{DisableColors: true}
91
92         ts := time.Now()
93
94         testCases := []struct {
95                 value    interface{}
96                 expected string
97         }{
98                 {ts, fmt.Sprintf("\"%s\"", ts.String())},
99                 {errors.New("error: something went wrong"), "\"error: something went wrong\""},
100         }
101
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)
106                 }
107         }
108 }
109
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("\"")]
117                 if format == "" {
118                         format = time.RFC3339
119                 }
120                 _, e := time.Parse(format, (string)(timeStr))
121                 if e != nil {
122                         t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e)
123                 }
124         }
125
126         checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
127         checkTimeStr("Mon Jan _2 15:04:05 2006")
128         checkTimeStr("")
129 }
130
131 func TestDisableTimestampWithColoredOutput(t *testing.T) {
132         tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
133
134         b, _ := tf.Format(WithField("test", "test"))
135         if strings.Contains(string(b), "[0000]") {
136                 t.Error("timestamp not expected when DisableTimestamp is true")
137         }
138 }
139
140 // TODO add tests for sorting etc., this requires a parser for the text
141 // formatter output.