Skip to content

Commit

Permalink
Update tests to use zap.Field alias
Browse files Browse the repository at this point in the history
Update zap's tests to use the new `zap.Field` alias. Keeping this change
in a separate commit lets us verify that changing to an alias is
backward-compatible for callers.
  • Loading branch information
Akshay Shah authored and akshayjshah committed Apr 13, 2018
1 parent 38f1589 commit 7307fae
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 71 deletions.
2 changes: 1 addition & 1 deletion array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func BenchmarkBoolsReflect(b *testing.B) {
func TestArrayWrappers(t *testing.T) {
tests := []struct {
desc string
field zapcore.Field
field Field
expected []interface{}
}{
{"empty bools", Bools("", []bool{}), []interface{}(nil)},
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/zap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func newSampledLogger(lvl zapcore.Level) *zap.Logger {
))
}

func fakeFields() []zapcore.Field {
return []zapcore.Field{
func fakeFields() []zap.Field {
return []zap.Field{
zap.Int("int", _tenInts[0]),
zap.Ints("ints", _tenInts),
zap.String("string", _tenStrings[0]),
Expand Down
10 changes: 5 additions & 5 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func TestErrorConstructors(t *testing.T) {

tests := []struct {
name string
field zapcore.Field
expect zapcore.Field
field Field
expect Field
}{
{"Error", Skip(), Error(nil)},
{"Error", zapcore.Field{Key: "error", Type: zapcore.ErrorType, Interface: fail}, Error(fail)},
{"Error", Field{Key: "error", Type: zapcore.ErrorType, Interface: fail}, Error(fail)},
{"NamedError", Skip(), NamedError("foo", nil)},
{"NamedError", zapcore.Field{Key: "foo", Type: zapcore.ErrorType, Interface: fail}, NamedError("foo", fail)},
{"NamedError", Field{Key: "foo", Type: zapcore.ErrorType, Interface: fail}, NamedError("foo", fail)},
{"Any:Error", Any("k", errors.New("v")), NamedError("k", errors.New("v"))},
{"Any:Errors", Any("k", []error{errors.New("v")}), Errors("k", []error{errors.New("v")})},
}
Expand All @@ -58,7 +58,7 @@ func TestErrorConstructors(t *testing.T) {
func TestErrorArrayConstructor(t *testing.T) {
tests := []struct {
desc string
field zapcore.Field
field Field
expected []interface{}
}{
{"empty errors", Errors("", []error{}), []interface{}(nil)},
Expand Down
58 changes: 29 additions & 29 deletions field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (n username) MarshalLogObject(enc zapcore.ObjectEncoder) error {
return nil
}

func assertCanBeReused(t testing.TB, field zapcore.Field) {
func assertCanBeReused(t testing.TB, field Field) {
var wg sync.WaitGroup

for i := 0; i < 100; i++ {
Expand Down Expand Up @@ -65,34 +65,34 @@ func TestFieldConstructors(t *testing.T) {

tests := []struct {
name string
field zapcore.Field
expect zapcore.Field
field Field
expect Field
}{
{"Skip", zapcore.Field{Type: zapcore.SkipType}, Skip()},
{"Binary", zapcore.Field{Key: "k", Type: zapcore.BinaryType, Interface: []byte("ab12")}, Binary("k", []byte("ab12"))},
{"Bool", zapcore.Field{Key: "k", Type: zapcore.BoolType, Integer: 1}, Bool("k", true)},
{"Bool", zapcore.Field{Key: "k", Type: zapcore.BoolType, Integer: 1}, Bool("k", true)},
{"ByteString", zapcore.Field{Key: "k", Type: zapcore.ByteStringType, Interface: []byte("ab12")}, ByteString("k", []byte("ab12"))},
{"Complex128", zapcore.Field{Key: "k", Type: zapcore.Complex128Type, Interface: 1 + 2i}, Complex128("k", 1+2i)},
{"Complex64", zapcore.Field{Key: "k", Type: zapcore.Complex64Type, Interface: complex64(1 + 2i)}, Complex64("k", 1+2i)},
{"Duration", zapcore.Field{Key: "k", Type: zapcore.DurationType, Integer: 1}, Duration("k", 1)},
{"Int", zapcore.Field{Key: "k", Type: zapcore.Int64Type, Integer: 1}, Int("k", 1)},
{"Int64", zapcore.Field{Key: "k", Type: zapcore.Int64Type, Integer: 1}, Int64("k", 1)},
{"Int32", zapcore.Field{Key: "k", Type: zapcore.Int32Type, Integer: 1}, Int32("k", 1)},
{"Int16", zapcore.Field{Key: "k", Type: zapcore.Int16Type, Integer: 1}, Int16("k", 1)},
{"Int8", zapcore.Field{Key: "k", Type: zapcore.Int8Type, Integer: 1}, Int8("k", 1)},
{"String", zapcore.Field{Key: "k", Type: zapcore.StringType, String: "foo"}, String("k", "foo")},
{"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Integer: 0, Interface: time.UTC}, Time("k", time.Unix(0, 0).In(time.UTC))},
{"Time", zapcore.Field{Key: "k", Type: zapcore.TimeType, Integer: 1000, Interface: time.UTC}, Time("k", time.Unix(0, 1000).In(time.UTC))},
{"Uint", zapcore.Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint("k", 1)},
{"Uint64", zapcore.Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint64("k", 1)},
{"Uint32", zapcore.Field{Key: "k", Type: zapcore.Uint32Type, Integer: 1}, Uint32("k", 1)},
{"Uint16", zapcore.Field{Key: "k", Type: zapcore.Uint16Type, Integer: 1}, Uint16("k", 1)},
{"Uint8", zapcore.Field{Key: "k", Type: zapcore.Uint8Type, Integer: 1}, Uint8("k", 1)},
{"Uintptr", zapcore.Field{Key: "k", Type: zapcore.UintptrType, Integer: 10}, Uintptr("k", 0xa)},
{"Reflect", zapcore.Field{Key: "k", Type: zapcore.ReflectType, Interface: ints}, Reflect("k", ints)},
{"Stringer", zapcore.Field{Key: "k", Type: zapcore.StringerType, Interface: addr}, Stringer("k", addr)},
{"Object", zapcore.Field{Key: "k", Type: zapcore.ObjectMarshalerType, Interface: name}, Object("k", name)},
{"Skip", Field{Type: zapcore.SkipType}, Skip()},
{"Binary", Field{Key: "k", Type: zapcore.BinaryType, Interface: []byte("ab12")}, Binary("k", []byte("ab12"))},
{"Bool", Field{Key: "k", Type: zapcore.BoolType, Integer: 1}, Bool("k", true)},
{"Bool", Field{Key: "k", Type: zapcore.BoolType, Integer: 1}, Bool("k", true)},
{"ByteString", Field{Key: "k", Type: zapcore.ByteStringType, Interface: []byte("ab12")}, ByteString("k", []byte("ab12"))},
{"Complex128", Field{Key: "k", Type: zapcore.Complex128Type, Interface: 1 + 2i}, Complex128("k", 1+2i)},
{"Complex64", Field{Key: "k", Type: zapcore.Complex64Type, Interface: complex64(1 + 2i)}, Complex64("k", 1+2i)},
{"Duration", Field{Key: "k", Type: zapcore.DurationType, Integer: 1}, Duration("k", 1)},
{"Int", Field{Key: "k", Type: zapcore.Int64Type, Integer: 1}, Int("k", 1)},
{"Int64", Field{Key: "k", Type: zapcore.Int64Type, Integer: 1}, Int64("k", 1)},
{"Int32", Field{Key: "k", Type: zapcore.Int32Type, Integer: 1}, Int32("k", 1)},
{"Int16", Field{Key: "k", Type: zapcore.Int16Type, Integer: 1}, Int16("k", 1)},
{"Int8", Field{Key: "k", Type: zapcore.Int8Type, Integer: 1}, Int8("k", 1)},
{"String", Field{Key: "k", Type: zapcore.StringType, String: "foo"}, String("k", "foo")},
{"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: 0, Interface: time.UTC}, Time("k", time.Unix(0, 0).In(time.UTC))},
{"Time", Field{Key: "k", Type: zapcore.TimeType, Integer: 1000, Interface: time.UTC}, Time("k", time.Unix(0, 1000).In(time.UTC))},
{"Uint", Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint("k", 1)},
{"Uint64", Field{Key: "k", Type: zapcore.Uint64Type, Integer: 1}, Uint64("k", 1)},
{"Uint32", Field{Key: "k", Type: zapcore.Uint32Type, Integer: 1}, Uint32("k", 1)},
{"Uint16", Field{Key: "k", Type: zapcore.Uint16Type, Integer: 1}, Uint16("k", 1)},
{"Uint8", Field{Key: "k", Type: zapcore.Uint8Type, Integer: 1}, Uint8("k", 1)},
{"Uintptr", Field{Key: "k", Type: zapcore.UintptrType, Integer: 10}, Uintptr("k", 0xa)},
{"Reflect", Field{Key: "k", Type: zapcore.ReflectType, Interface: ints}, Reflect("k", ints)},
{"Stringer", Field{Key: "k", Type: zapcore.StringerType, Interface: addr}, Stringer("k", addr)},
{"Object", Field{Key: "k", Type: zapcore.ObjectMarshalerType, Interface: name}, Object("k", name)},
{"Any:ObjectMarshaler", Any("k", name), Object("k", name)},
{"Any:ArrayMarshaler", Any("k", bools([]bool{true})), Array("k", bools([]bool{true}))},
{"Any:Stringer", Any("k", addr), Stringer("k", addr)},
Expand Down Expand Up @@ -139,7 +139,7 @@ func TestFieldConstructors(t *testing.T) {
{"Any:Duration", Any("k", time.Second), Duration("k", time.Second)},
{"Any:Durations", Any("k", []time.Duration{time.Second}), Durations("k", []time.Duration{time.Second})},
{"Any:Fallback", Any("k", struct{}{}), Reflect("k", struct{}{})},
{"Namespace", Namespace("k"), zapcore.Field{Key: "k", Type: zapcore.NamespaceType}},
{"Namespace", Namespace("k"), Field{Key: "k", Type: zapcore.NamespaceType}},
}

for _, tt := range tests {
Expand Down
8 changes: 4 additions & 4 deletions global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestReplaceGlobals(t *testing.T) {
S().Info("captured")
expected := observer.LoggedEntry{
Entry: zapcore.Entry{Message: "captured"},
Context: []zapcore.Field{},
Context: []Field{},
}
assert.Equal(
t,
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestRedirectStdLog(t *testing.T) {

assert.Equal(t, []observer.LoggedEntry{{
Entry: zapcore.Entry{Message: "redirected"},
Context: []zapcore.Field{},
Context: []Field{},
}}, logs.AllUntimed(), "Unexpected global log output.")
})

Expand Down Expand Up @@ -190,7 +190,7 @@ func TestRedirectStdLogAt(t *testing.T) {

assert.Equal(t, []observer.LoggedEntry{{
Entry: zapcore.Entry{Level: level, Message: "redirected"},
Context: []zapcore.Field{},
Context: []Field{},
}}, logs.AllUntimed(), "Unexpected global log output.")
})
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestRedirectStdLogAtInvalid(t *testing.T) {
func checkStdLogMessage(t *testing.T, msg string, logs *observer.ObservedLogs) {
require.Equal(t, 1, logs.Len(), "Expected exactly one entry to be logged")
entry := logs.AllUntimed()[0]
assert.Equal(t, []zapcore.Field{}, entry.Context, "Unexpected entry context.")
assert.Equal(t, []Field{}, entry.Context, "Unexpected entry context.")
assert.Equal(t, "redirected", entry.Entry.Message, "Unexpected entry message.")
assert.Regexp(
t,
Expand Down
4 changes: 2 additions & 2 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ func Benchmark100Fields(b *testing.B) {
// Don't include allocating these helper slices in the benchmark. Since
// access to them isn't synchronized, we can't run the benchmark in
// parallel.
first := make([]zapcore.Field, batchSize)
second := make([]zapcore.Field, batchSize)
first := make([]Field, batchSize)
second := make([]Field, batchSize)
b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand Down
16 changes: 8 additions & 8 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestLoggerInitialFields(t *testing.T) {
logger.Info("")
assert.Equal(
t,
observer.LoggedEntry{Context: []zapcore.Field{Int("foo", 42), String("bar", "baz")}},
observer.LoggedEntry{Context: []Field{Int("foo", 42), String("bar", "baz")}},
logs.AllUntimed()[0],
"Unexpected output with initial fields set.",
)
Expand All @@ -106,9 +106,9 @@ func TestLoggerWith(t *testing.T) {
logger.Info("")

assert.Equal(t, []observer.LoggedEntry{
{Context: []zapcore.Field{Int("foo", 42), String("one", "two")}},
{Context: []zapcore.Field{Int("foo", 42), String("three", "four")}},
{Context: []zapcore.Field{Int("foo", 42)}},
{Context: []Field{Int("foo", 42), String("one", "two")}},
{Context: []Field{Int("foo", 42), String("three", "four")}},
{Context: []Field{Int("foo", 42)}},
}, logs.AllUntimed(), "Unexpected cross-talk between child loggers.")
})
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestLoggerLogFatal(t *testing.T) {
func TestLoggerLeveledMethods(t *testing.T) {
withLogger(t, DebugLevel, nil, func(logger *Logger, logs *observer.ObservedLogs) {
tests := []struct {
method func(string, ...zapcore.Field)
method func(string, ...Field)
expectedLevel zapcore.Level
}{
{logger.Debug, DebugLevel},
Expand Down Expand Up @@ -232,7 +232,7 @@ func TestLoggerDPanic(t *testing.T) {
assert.NotPanics(t, func() { logger.DPanic("") })
assert.Equal(
t,
[]observer.LoggedEntry{{Entry: zapcore.Entry{Level: DPanicLevel}, Context: []zapcore.Field{}}},
[]observer.LoggedEntry{{Entry: zapcore.Entry{Level: DPanicLevel}, Context: []Field{}}},
logs.AllUntimed(),
"Unexpected log output from DPanic in production mode.",
)
Expand All @@ -241,7 +241,7 @@ func TestLoggerDPanic(t *testing.T) {
assert.Panics(t, func() { logger.DPanic("") })
assert.Equal(
t,
[]observer.LoggedEntry{{Entry: zapcore.Entry{Level: DPanicLevel}, Context: []zapcore.Field{}}},
[]observer.LoggedEntry{{Entry: zapcore.Entry{Level: DPanicLevel}, Context: []Field{}}},
logs.AllUntimed(),
"Unexpected log output from DPanic in development mode.",
)
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestLoggerConcurrent(t *testing.T) {
t,
observer.LoggedEntry{
Entry: zapcore.Entry{Level: InfoLevel},
Context: []zapcore.Field{String("foo", "bar")},
Context: []Field{String("foo", "bar")},
},
obs,
"Unexpected log output.",
Expand Down
40 changes: 20 additions & 20 deletions sugar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,86 +37,86 @@ func TestSugarWith(t *testing.T) {
ignored := func(msg interface{}) observer.LoggedEntry {
return observer.LoggedEntry{
Entry: zapcore.Entry{Level: DPanicLevel, Message: _oddNumberErrMsg},
Context: []zapcore.Field{Any("ignored", msg)},
Context: []Field{Any("ignored", msg)},
}
}
nonString := func(pairs ...invalidPair) observer.LoggedEntry {
return observer.LoggedEntry{
Entry: zapcore.Entry{Level: DPanicLevel, Message: _nonStringKeyErrMsg},
Context: []zapcore.Field{Array("invalid", invalidPairs(pairs))},
Context: []Field{Array("invalid", invalidPairs(pairs))},
}
}

tests := []struct {
desc string
args []interface{}
expected []zapcore.Field
expected []Field
errLogs []observer.LoggedEntry
}{
{
desc: "nil args",
args: nil,
expected: []zapcore.Field{},
expected: []Field{},
errLogs: nil,
},
{
desc: "empty slice of args",
args: []interface{}{},
expected: []zapcore.Field{},
expected: []Field{},
errLogs: nil,
},
{
desc: "just a dangling key",
args: []interface{}{"should ignore"},
expected: []zapcore.Field{},
expected: []Field{},
errLogs: []observer.LoggedEntry{ignored("should ignore")},
},
{
desc: "well-formed key-value pairs",
args: []interface{}{"foo", 42, "true", "bar"},
expected: []zapcore.Field{Int("foo", 42), String("true", "bar")},
expected: []Field{Int("foo", 42), String("true", "bar")},
errLogs: nil,
},
{
desc: "just a structured field",
args: []interface{}{Int("foo", 42)},
expected: []zapcore.Field{Int("foo", 42)},
expected: []Field{Int("foo", 42)},
errLogs: nil,
},
{
desc: "structured field and a dangling key",
args: []interface{}{Int("foo", 42), "dangling"},
expected: []zapcore.Field{Int("foo", 42)},
expected: []Field{Int("foo", 42)},
errLogs: []observer.LoggedEntry{ignored("dangling")},
},
{
desc: "structured field and a dangling non-string key",
args: []interface{}{Int("foo", 42), 13},
expected: []zapcore.Field{Int("foo", 42)},
expected: []Field{Int("foo", 42)},
errLogs: []observer.LoggedEntry{ignored(13)},
},
{
desc: "key-value pair and a dangling key",
args: []interface{}{"foo", 42, "dangling"},
expected: []zapcore.Field{Int("foo", 42)},
expected: []Field{Int("foo", 42)},
errLogs: []observer.LoggedEntry{ignored("dangling")},
},
{
desc: "pairs, a structured field, and a dangling key",
args: []interface{}{"first", "field", Int("foo", 42), "baz", "quux", "dangling"},
expected: []zapcore.Field{String("first", "field"), Int("foo", 42), String("baz", "quux")},
expected: []Field{String("first", "field"), Int("foo", 42), String("baz", "quux")},
errLogs: []observer.LoggedEntry{ignored("dangling")},
},
{
desc: "one non-string key",
args: []interface{}{"foo", 42, true, "bar"},
expected: []zapcore.Field{Int("foo", 42)},
expected: []Field{Int("foo", 42)},
errLogs: []observer.LoggedEntry{nonString(invalidPair{2, true, "bar"})},
},
{
desc: "pairs, structured fields, non-string keys, and a dangling key",
args: []interface{}{"foo", 42, true, "bar", Int("structure", 11), 42, "reversed", "baz", "quux", "dangling"},
expected: []zapcore.Field{Int("foo", 42), Int("structure", 11), String("baz", "quux")},
expected: []Field{Int("foo", 42), Int("structure", 11), String("baz", "quux")},
errLogs: []observer.LoggedEntry{
ignored("dangling"),
nonString(invalidPair{2, true, "bar"}, invalidPair{5, 42, "reversed"}),
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestSugarFieldsInvalidPairs(t *testing.T) {

// Double-check that the actual message was logged.
require.Equal(t, 2, len(output), "Unexpected number of entries logged.")
require.Equal(t, observer.LoggedEntry{Context: []zapcore.Field{}}, output[1], "Unexpected non-error log entry.")
require.Equal(t, observer.LoggedEntry{Context: []Field{}}, output[1], "Unexpected non-error log entry.")

// Assert that the error message's structured fields serialize properly.
require.Equal(t, 1, len(output[0].Context), "Expected one field in error entry context.")
Expand Down Expand Up @@ -175,7 +175,7 @@ func TestSugarStructuredLogging(t *testing.T) {
// Common to all test cases.
context := []interface{}{"foo", "bar"}
extra := []interface{}{"baz", false}
expectedFields := []zapcore.Field{String("foo", "bar"), Bool("baz", false)}
expectedFields := []Field{String("foo", "bar"), Bool("baz", false)}

for _, tt := range tests {
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestSugarConcatenatingLogging(t *testing.T) {

// Common to all test cases.
context := []interface{}{"foo", "bar"}
expectedFields := []zapcore.Field{String("foo", "bar")}
expectedFields := []Field{String("foo", "bar")}

for _, tt := range tests {
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestSugarTemplatedLogging(t *testing.T) {

// Common to all test cases.
context := []interface{}{"foo", "bar"}
expectedFields := []zapcore.Field{String("foo", "bar")}
expectedFields := []Field{String("foo", "bar")}

for _, tt := range tests {
withSugar(t, DebugLevel, nil, func(logger *SugaredLogger, logs *observer.ObservedLogs) {
Expand Down Expand Up @@ -287,7 +287,7 @@ func TestSugarPanicLogging(t *testing.T) {
assert.Panics(t, func() { tt.f(sugar) }, "Expected panic-level logger calls to panic.")
if tt.expectedMsg != "" {
assert.Equal(t, []observer.LoggedEntry{{
Context: []zapcore.Field{},
Context: []Field{},
Entry: zapcore.Entry{Message: tt.expectedMsg, Level: PanicLevel},
}}, logs.AllUntimed(), "Unexpected log output.")
} else {
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestSugarFatalLogging(t *testing.T) {
assert.True(t, stub.Exited, "Expected all calls to fatal logger methods to exit process.")
if tt.expectedMsg != "" {
assert.Equal(t, []observer.LoggedEntry{{
Context: []zapcore.Field{},
Context: []Field{},
Entry: zapcore.Entry{Message: tt.expectedMsg, Level: FatalLevel},
}}, logs.AllUntimed(), "Unexpected log output.")
} else {
Expand Down

0 comments on commit 7307fae

Please sign in to comment.