|
| 1 | +// Copyright (c) 2016 Uber Technologies, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package zap |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "io" |
| 26 | + "math" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | + "github.com/uber-go/zap/spywrite" |
| 32 | +) |
| 33 | + |
| 34 | +func newTextEncoder(opts ...TextOption) *textEncoder { |
| 35 | + return NewTextEncoder(opts...).(*textEncoder) |
| 36 | +} |
| 37 | + |
| 38 | +func withTextEncoder(f func(*textEncoder)) { |
| 39 | + enc := newTextEncoder() |
| 40 | + f(enc) |
| 41 | + enc.Free() |
| 42 | +} |
| 43 | + |
| 44 | +func assertTextOutput(t testing.TB, desc string, expected string, f func(Encoder)) { |
| 45 | + withTextEncoder(func(enc *textEncoder) { |
| 46 | + f(enc) |
| 47 | + assert.Equal(t, expected, string(enc.bytes), "Unexpected encoder output after adding a %s.", desc) |
| 48 | + }) |
| 49 | + withTextEncoder(func(enc *textEncoder) { |
| 50 | + enc.AddString("foo", "bar") |
| 51 | + f(enc) |
| 52 | + expectedPrefix := "foo=bar" |
| 53 | + if expected != "" { |
| 54 | + // If we expect output, it should be space-separated from the previous |
| 55 | + // field. |
| 56 | + expectedPrefix += " " |
| 57 | + } |
| 58 | + assert.Equal(t, expectedPrefix+expected, string(enc.bytes), "Unexpected encoder output after adding a %s as a second field.", desc) |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestTextEncoderFields(t *testing.T) { |
| 63 | + tests := []struct { |
| 64 | + desc string |
| 65 | + expected string |
| 66 | + f func(Encoder) |
| 67 | + }{ |
| 68 | + {"string", "k=v", func(e Encoder) { e.AddString("k", "v") }}, |
| 69 | + {"string", "k=", func(e Encoder) { e.AddString("k", "") }}, |
| 70 | + {"bool", "k=true", func(e Encoder) { e.AddBool("k", true) }}, |
| 71 | + {"bool", "k=false", func(e Encoder) { e.AddBool("k", false) }}, |
| 72 | + {"int", "k=42", func(e Encoder) { e.AddInt("k", 42) }}, |
| 73 | + {"int64", "k=42", func(e Encoder) { e.AddInt64("k", 42) }}, |
| 74 | + {"int64", fmt.Sprintf("k=%d", math.MaxInt64), func(e Encoder) { e.AddInt64("k", math.MaxInt64) }}, |
| 75 | + {"uint", "k=42", func(e Encoder) { e.AddUint("k", 42) }}, |
| 76 | + {"uint64", "k=42", func(e Encoder) { e.AddUint64("k", 42) }}, |
| 77 | + {"uint64", fmt.Sprintf("k=%d", uint64(math.MaxUint64)), func(e Encoder) { e.AddUint64("k", math.MaxUint64) }}, |
| 78 | + {"float64", "k=1", func(e Encoder) { e.AddFloat64("k", 1.0) }}, |
| 79 | + {"float64", "k=10000000000", func(e Encoder) { e.AddFloat64("k", 1e10) }}, |
| 80 | + {"float64", "k=NaN", func(e Encoder) { e.AddFloat64("k", math.NaN()) }}, |
| 81 | + {"float64", "k=+Inf", func(e Encoder) { e.AddFloat64("k", math.Inf(1)) }}, |
| 82 | + {"float64", "k=-Inf", func(e Encoder) { e.AddFloat64("k", math.Inf(-1)) }}, |
| 83 | + {"marshaler", "k={loggable=yes}", func(e Encoder) { |
| 84 | + assert.NoError(t, e.AddMarshaler("k", loggable{true}), "Unexpected error calling MarshalLog.") |
| 85 | + }}, |
| 86 | + {"marshaler", "k={}", func(e Encoder) { |
| 87 | + assert.Error(t, e.AddMarshaler("k", loggable{false}), "Expected an error calling MarshalLog.") |
| 88 | + }}, |
| 89 | + {"map[string]string", "k=map[loggable:yes]", func(e Encoder) { |
| 90 | + assert.NoError(t, e.AddObject("k", map[string]string{"loggable": "yes"}), "Unexpected error serializing a map.") |
| 91 | + }}, |
| 92 | + {"arbitrary object", "k={Name:jane}", func(e Encoder) { |
| 93 | + assert.NoError(t, e.AddObject("k", struct{ Name string }{"jane"}), "Unexpected error serializing a struct.") |
| 94 | + }}, |
| 95 | + } |
| 96 | + |
| 97 | + for _, tt := range tests { |
| 98 | + assertTextOutput(t, tt.desc, tt.expected, tt.f) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestTextWriteEntry(t *testing.T) { |
| 103 | + entry := &Entry{Level: InfoLevel, Message: "Something happened.", Time: epoch} |
| 104 | + tests := []struct { |
| 105 | + enc Encoder |
| 106 | + expected string |
| 107 | + name string |
| 108 | + }{ |
| 109 | + { |
| 110 | + enc: NewTextEncoder(), |
| 111 | + expected: "[I] 1970-01-01T00:00:00Z Something happened.", |
| 112 | + name: "RFC822", |
| 113 | + }, |
| 114 | + { |
| 115 | + enc: NewTextEncoder(TextTimeFormat(time.RFC822)), |
| 116 | + expected: "[I] 01 Jan 70 00:00 UTC Something happened.", |
| 117 | + name: "RFC822", |
| 118 | + }, |
| 119 | + { |
| 120 | + enc: NewTextEncoder(TextTimeFormat("")), |
| 121 | + expected: "[I] Something happened.", |
| 122 | + name: "empty layout", |
| 123 | + }, |
| 124 | + { |
| 125 | + enc: NewTextEncoder(TextNoTime()), |
| 126 | + expected: "[I] Something happened.", |
| 127 | + name: "NoTime", |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + sink := &testBuffer{} |
| 132 | + for _, tt := range tests { |
| 133 | + assert.NoError( |
| 134 | + t, |
| 135 | + tt.enc.WriteEntry(sink, entry.Message, entry.Level, entry.Time), |
| 136 | + "Unexpected failure writing entry with text time formatter %s.", tt.name, |
| 137 | + ) |
| 138 | + assert.Equal(t, tt.expected, sink.Stripped(), "Unexpected output from text time formatter %s.", tt.name) |
| 139 | + sink.Reset() |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestTextWriteEntryLevels(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + level Level |
| 146 | + expected string |
| 147 | + }{ |
| 148 | + {DebugLevel, "D"}, |
| 149 | + {InfoLevel, "I"}, |
| 150 | + {WarnLevel, "W"}, |
| 151 | + {ErrorLevel, "E"}, |
| 152 | + {PanicLevel, "P"}, |
| 153 | + {FatalLevel, "F"}, |
| 154 | + {Level(42), "42"}, |
| 155 | + } |
| 156 | + |
| 157 | + sink := &testBuffer{} |
| 158 | + enc := NewTextEncoder(TextNoTime()) |
| 159 | + for _, tt := range tests { |
| 160 | + assert.NoError( |
| 161 | + t, |
| 162 | + enc.WriteEntry(sink, "Fake message.", tt.level, epoch), |
| 163 | + "Unexpected failure writing entry with level %s.", tt.level, |
| 164 | + ) |
| 165 | + expected := fmt.Sprintf("[%s] Fake message.", tt.expected) |
| 166 | + assert.Equal(t, expected, sink.Stripped(), "Unexpected text output for level %s.", tt.level) |
| 167 | + sink.Reset() |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestTextClone(t *testing.T) { |
| 172 | + parent := &textEncoder{bytes: make([]byte, 0, 128)} |
| 173 | + clone := parent.Clone() |
| 174 | + |
| 175 | + // Adding to the parent shouldn't affect the clone, and vice versa. |
| 176 | + parent.AddString("foo", "bar") |
| 177 | + clone.AddString("baz", "bing") |
| 178 | + |
| 179 | + assert.Equal(t, "foo=bar", string(parent.bytes), "Unexpected serialized fields in parent encoder.") |
| 180 | + assert.Equal(t, "baz=bing", string(clone.(*textEncoder).bytes), "Unexpected serialized fields in cloned encoder.") |
| 181 | +} |
| 182 | + |
| 183 | +func TestTextWriteEntryFailure(t *testing.T) { |
| 184 | + withTextEncoder(func(enc *textEncoder) { |
| 185 | + tests := []struct { |
| 186 | + sink io.Writer |
| 187 | + msg string |
| 188 | + }{ |
| 189 | + {nil, "Expected an error when writing to a nil sink."}, |
| 190 | + {spywrite.FailWriter{}, "Expected an error when writing to sink fails."}, |
| 191 | + {spywrite.ShortWriter{}, "Expected an error on partial writes to sink."}, |
| 192 | + } |
| 193 | + for _, tt := range tests { |
| 194 | + err := enc.WriteEntry(tt.sink, "hello", InfoLevel, time.Unix(0, 0)) |
| 195 | + assert.Error(t, err, tt.msg) |
| 196 | + } |
| 197 | + }) |
| 198 | +} |
| 199 | + |
| 200 | +func TestTextTimeOptions(t *testing.T) { |
| 201 | + epoch := time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 202 | + entry := &Entry{Level: InfoLevel, Message: "Something happened.", Time: epoch} |
| 203 | + |
| 204 | + enc := NewTextEncoder() |
| 205 | + |
| 206 | + sink := &testBuffer{} |
| 207 | + enc.AddString("foo", "bar") |
| 208 | + err := enc.WriteEntry(sink, entry.Message, entry.Level, entry.Time) |
| 209 | + assert.NoError(t, err, "WriteEntry returned an unexpected error.") |
| 210 | + assert.Equal( |
| 211 | + t, |
| 212 | + "[I] 1970-01-01T00:00:00Z Something happened. foo=bar", |
| 213 | + sink.Stripped(), |
| 214 | + ) |
| 215 | +} |
0 commit comments