1// Copyright (c) 2017 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
21package zap
22
23import (
24	"errors"
25	"testing"
26
27	"go.uber.org/zap/zapcore"
28
29	richErrors "github.com/pkg/errors"
30	"github.com/stretchr/testify/assert"
31	"github.com/stretchr/testify/require"
32)
33
34func TestErrorConstructors(t *testing.T) {
35	fail := errors.New("fail")
36
37	tests := []struct {
38		name   string
39		field  zapcore.Field
40		expect zapcore.Field
41	}{
42		{"Error", Skip(), Error(nil)},
43		{"Error", zapcore.Field{Key: "error", Type: zapcore.ErrorType, Interface: fail}, Error(fail)},
44		{"NamedError", Skip(), NamedError("foo", nil)},
45		{"NamedError", zapcore.Field{Key: "foo", Type: zapcore.ErrorType, Interface: fail}, NamedError("foo", fail)},
46		{"Any:Error", Any("k", errors.New("v")), NamedError("k", errors.New("v"))},
47		{"Any:Errors", Any("k", []error{errors.New("v")}), Errors("k", []error{errors.New("v")})},
48	}
49
50	for _, tt := range tests {
51		if !assert.Equal(t, tt.expect, tt.field, "Unexpected output from convenience field constructor %s.", tt.name) {
52			t.Logf("type expected: %T\nGot: %T", tt.expect.Interface, tt.field.Interface)
53		}
54		assertCanBeReused(t, tt.field)
55	}
56}
57
58func TestErrorArrayConstructor(t *testing.T) {
59	tests := []struct {
60		desc     string
61		field    zapcore.Field
62		expected []interface{}
63	}{
64		{"empty errors", Errors("", []error{}), []interface{}(nil)},
65		{
66			"errors",
67			Errors("", []error{nil, errors.New("foo"), nil, errors.New("bar")}),
68			[]interface{}{map[string]interface{}{"error": "foo"}, map[string]interface{}{"error": "bar"}},
69		},
70	}
71
72	for _, tt := range tests {
73		enc := zapcore.NewMapObjectEncoder()
74		tt.field.Key = "k"
75		tt.field.AddTo(enc)
76		assert.Equal(t, tt.expected, enc.Fields["k"], "%s: unexpected map contents.", tt.desc)
77		assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields)
78	}
79}
80
81func TestErrorsArraysHandleRichErrors(t *testing.T) {
82	errs := []error{richErrors.New("egad")}
83
84	enc := zapcore.NewMapObjectEncoder()
85	Errors("k", errs).AddTo(enc)
86	assert.Equal(t, 1, len(enc.Fields), "Expected only top-level field.")
87
88	val := enc.Fields["k"]
89	arr, ok := val.([]interface{})
90	require.True(t, ok, "Expected top-level field to be an array.")
91	require.Equal(t, 1, len(arr), "Expected only one error object in array.")
92
93	serialized := arr[0]
94	errMap, ok := serialized.(map[string]interface{})
95	require.True(t, ok, "Expected serialized error to be a map, got %T.", serialized)
96	assert.Equal(t, "egad", errMap["error"], "Unexpected standard error string.")
97	assert.Contains(t, errMap["errorVerbose"], "egad", "Verbose error string should be a superset of standard error.")
98	assert.Contains(t, errMap["errorVerbose"], "TestErrorsArraysHandleRichErrors", "Verbose error string should contain a stacktrace.")
99}
100