2015-06-27 03:18:52 -04:00
|
|
|
package jsonlog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJSONLogMarshalJSON(t *testing.T) {
|
2016-04-08 12:15:08 -04:00
|
|
|
logs := map[*JSONLog]string{
|
|
|
|
&JSONLog{Log: `"A log line with \\"`}: `^{\"log\":\"\\\"A log line with \\\\\\\\\\\"\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Log: "A log line"}: `^{\"log\":\"A log line\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Log: "A log line with \r"}: `^{\"log\":\"A log line with \\r\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Log: "A log line with & < >"}: `^{\"log\":\"A log line with \\u0026 \\u003c \\u003e\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Log: "A log line with utf8 : 🚀 ψ ω β"}: `^{\"log\":\"A log line with utf8 : 🚀 ψ ω β\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Stream: "stdout"}: `^{\"stream\":\"stdout\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{}: `^{\"time\":\".{20,}\"}$`,
|
2015-06-27 03:18:52 -04:00
|
|
|
// These ones are a little weird
|
2016-04-08 12:15:08 -04:00
|
|
|
&JSONLog{Log: "\u2028 \u2029"}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Log: string([]byte{0xaF})}: `^{\"log\":\"\\ufffd\",\"time\":\".{20,}\"}$`,
|
|
|
|
&JSONLog{Log: string([]byte{0x7F})}: `^{\"log\":\"\x7f\",\"time\":\".{20,}\"}$`,
|
2015-06-27 03:18:52 -04:00
|
|
|
}
|
|
|
|
for jsonLog, expression := range logs {
|
|
|
|
data, err := jsonLog.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
res := string(data)
|
|
|
|
t.Logf("Result of WriteLog: %q", res)
|
|
|
|
logRe := regexp.MustCompile(expression)
|
|
|
|
if !logRe.MatchString(res) {
|
|
|
|
t.Fatalf("Log line not in expected format [%v]: %q", expression, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|