From fa2d76dd8027025632aa61515b3c23a081a13017 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Fri, 19 May 2017 21:57:46 -0700 Subject: [PATCH] Add some test --- json.go | 39 --------------------------------------- json_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ log_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 json_test.go diff --git a/json.go b/json.go index f3971d1..2b78542 100644 --- a/json.go +++ b/json.go @@ -42,42 +42,3 @@ func appendJSONString(dst []byte, s string) []byte { } 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, '"') -} diff --git a/json_test.go b/json_test.go new file mode 100644 index 0000000..fe12dee --- /dev/null +++ b/json_test.go @@ -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) + } + } +} diff --git a/log_test.go b/log_test.go index c253ea5..4409b35 100644 --- a/log_test.go +++ b/log_test.go @@ -235,3 +235,35 @@ func TestLevelWriter(t *testing.T) { 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) + } +}