Add some test

This commit is contained in:
Olivier Poitrey 2017-05-19 21:57:46 -07:00
parent 06b7e1c922
commit fa2d76dd80
3 changed files with 83 additions and 39 deletions

39
json.go
View File

@ -42,42 +42,3 @@ func appendJSONString(dst []byte, s string) []byte {
} }
return append(dst, '"') return append(dst, '"')
} }
func appendJSONBytes(dst []byte, s []byte) []byte {
dst = append(dst, '"')
for i := 0; i < len(s); {
if b := s[i]; b < utf8.RuneSelf {
switch b {
case '"', '\\':
dst = append(dst, '\\', b)
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
default:
if b >= 0x20 {
dst = append(dst, b)
} else {
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
}
}
i++
continue
}
r, size := utf8.DecodeRune(s[i:])
if r == utf8.RuneError && size == 1 {
dst = append(dst, `\ufffd`...)
i++
continue
}
dst = append(dst, s[i:i+size]...)
i += size
}
return append(dst, '"')
}

51
json_test.go Normal file
View File

@ -0,0 +1,51 @@
package zerolog
import "testing"
func TestAppendJSONString(t *testing.T) {
encodeStringTests := []struct {
in string
out string
}{
{"\\", `"\\"`},
{"\x00", `"\u0000"`},
{"\x01", `"\u0001"`},
{"\x02", `"\u0002"`},
{"\x03", `"\u0003"`},
{"\x04", `"\u0004"`},
{"\x05", `"\u0005"`},
{"\x06", `"\u0006"`},
{"\x07", `"\u0007"`},
{"\x08", `"\b"`},
{"\x09", `"\t"`},
{"\x0a", `"\n"`},
{"\x0b", `"\u000b"`},
{"\x0c", `"\f"`},
{"\x0d", `"\r"`},
{"\x0e", `"\u000e"`},
{"\x0f", `"\u000f"`},
{"\x10", `"\u0010"`},
{"\x11", `"\u0011"`},
{"\x12", `"\u0012"`},
{"\x13", `"\u0013"`},
{"\x14", `"\u0014"`},
{"\x15", `"\u0015"`},
{"\x16", `"\u0016"`},
{"\x17", `"\u0017"`},
{"\x18", `"\u0018"`},
{"\x19", `"\u0019"`},
{"\x1a", `"\u001a"`},
{"\x1b", `"\u001b"`},
{"\x1c", `"\u001c"`},
{"\x1d", `"\u001d"`},
{"\x1e", `"\u001e"`},
{"\x1f", `"\u001f"`},
}
for _, tt := range encodeStringTests {
b := appendJSONString([]byte{}, tt.in)
if got, want := string(b), tt.out; got != want {
t.Errorf("appendJSONString(%q) = %#q, want %#q", tt.in, got, want)
}
}
}

View File

@ -235,3 +235,35 @@ func TestLevelWriter(t *testing.T) {
t.Errorf("invalid ops:\ngot:\n%v\nwant:\n%v", got, want) t.Errorf("invalid ops:\ngot:\n%v\nwant:\n%v", got, want)
} }
} }
func TestContextTimestamp(t *testing.T) {
now = func() time.Time {
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
}
defer func() {
now = time.Now
}()
out := &bytes.Buffer{}
log := New(out).With().Timestamp().Str("foo", "bar").Logger()
log.Log().Msg("hello world")
if got, want := out.String(), `{"time":981173106,"foo":"bar","message":"hello world"}`+"\n"; got != want {
t.Errorf("invalid log output: got %q, want %q", got, want)
}
}
func TestEventTimestamp(t *testing.T) {
now = func() time.Time {
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
}
defer func() {
now = time.Now
}()
out := &bytes.Buffer{}
log := New(out).With().Str("foo", "bar").Logger()
log.Log().Timestamp().Msg("hello world")
if got, want := out.String(), `{"foo":"bar","time":981173106,"message":"hello world"}`+"\n"; got != want {
t.Errorf("invalid log output: got %q, want %q", got, want)
}
}