11 func TestQuoting(t *testing.T) {
12 tf := &TextFormatter{DisableColors: true}
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))
20 t.Errorf("quoting expected for: %#v", value)
22 t.Errorf("quoting not expected for: %#v", value)
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"))
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"))
44 // Test for multi-character quotes.
45 tf.QuoteCharacter = "§~±"
46 checkQuoting(false, "abcd")
47 checkQuoting(true, errors.New("invalid argument"))
49 // Test for quoting empty fields.
50 tf.QuoteEmptyFields = true
51 checkQuoting(true, "")
52 checkQuoting(false, "abcd")
53 checkQuoting(true, errors.New("invalid argument"))
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)]
66 _, e := time.Parse(format, (string)(timeStr))
68 t.Errorf("time string \"%s\" did not match provided time format \"%s\": %s", timeStr, format, e)
72 checkTimeStr("2006-01-02T15:04:05.000000000Z07:00")
73 checkTimeStr("Mon Jan _2 15:04:05 2006")
77 func TestDisableTimestampWithColoredOutput(t *testing.T) {
78 tf := &TextFormatter{DisableTimestamp: true, ForceColors: true}
80 b, _ := tf.Format(WithField("test", "test"))
81 if strings.Contains(string(b), "[0000]") {
82 t.Error("timestamp not expected when DisableTimestamp is true")
86 // TODO add tests for sorting etc., this requires a parser for the text