2014-12-15 22:20:33 +03:00
|
|
|
package logrus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestQuoting(t *testing.T) {
|
2014-12-18 09:59:41 +03:00
|
|
|
tf := &TextFormatter{DisableColors: true}
|
2014-12-15 22:20:33 +03:00
|
|
|
|
|
|
|
checkQuoting := func(q bool, value interface{}) {
|
|
|
|
b, _ := tf.Format(WithField("test", value))
|
2014-12-18 09:59:41 +03:00
|
|
|
idx := bytes.Index(b, ([]byte)("test="))
|
|
|
|
cont := bytes.Contains(b[idx+5:], []byte{'"'})
|
2014-12-15 22:20:33 +03:00
|
|
|
if cont != q {
|
|
|
|
if q {
|
|
|
|
t.Errorf("quoting expected for: %#v", value)
|
|
|
|
} else {
|
|
|
|
t.Errorf("quoting not expected for: %#v", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkQuoting(false, "abcd")
|
|
|
|
checkQuoting(false, "v1.0")
|
2015-03-04 17:04:50 +03:00
|
|
|
checkQuoting(false, "1234567890")
|
2014-12-15 22:20:33 +03:00
|
|
|
checkQuoting(true, "/foobar")
|
|
|
|
checkQuoting(true, "x y")
|
|
|
|
checkQuoting(true, "x,y")
|
|
|
|
checkQuoting(false, errors.New("invalid"))
|
|
|
|
checkQuoting(true, errors.New("invalid argument"))
|
|
|
|
}
|
2015-03-09 18:19:51 +03:00
|
|
|
|
|
|
|
// TODO add tests for sorting etc., this requires a parser for the text
|
|
|
|
// formatter output.
|