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
21package zap
22
23import (
24	"testing"
25
26	"go.uber.org/zap/zapcore"
27
28	"github.com/stretchr/testify/assert"
29)
30
31func TestRegisterDefaultEncoders(t *testing.T) {
32	testEncodersRegistered(t, "console", "json")
33}
34
35func TestRegisterEncoder(t *testing.T) {
36	testEncoders(func() {
37		assert.NoError(t, RegisterEncoder("foo", newNilEncoder), "expected to be able to register the encoder foo")
38		testEncodersRegistered(t, "foo")
39	})
40}
41
42func TestDuplicateRegisterEncoder(t *testing.T) {
43	testEncoders(func() {
44		RegisterEncoder("foo", newNilEncoder)
45		assert.Error(t, RegisterEncoder("foo", newNilEncoder), "expected an error when registering an encoder with the same name twice")
46	})
47}
48
49func TestRegisterEncoderNoName(t *testing.T) {
50	assert.Equal(t, errNoEncoderNameSpecified, RegisterEncoder("", newNilEncoder), "expected an error when registering an encoder with no name")
51}
52
53func TestNewEncoder(t *testing.T) {
54	testEncoders(func() {
55		RegisterEncoder("foo", newNilEncoder)
56		encoder, err := newEncoder("foo", zapcore.EncoderConfig{})
57		assert.NoError(t, err, "could not create an encoder for the registered name foo")
58		assert.Nil(t, encoder, "the encoder from newNilEncoder is not nil")
59	})
60}
61
62func TestNewEncoderNotRegistered(t *testing.T) {
63	_, err := newEncoder("foo", zapcore.EncoderConfig{})
64	assert.Error(t, err, "expected an error when trying to create an encoder of an unregistered name")
65}
66
67func TestNewEncoderNoName(t *testing.T) {
68	_, err := newEncoder("", zapcore.EncoderConfig{})
69	assert.Equal(t, errNoEncoderNameSpecified, err, "expected an error when creating an encoder with no name")
70}
71
72func testEncoders(f func()) {
73	existing := _encoderNameToConstructor
74	_encoderNameToConstructor = make(map[string]func(zapcore.EncoderConfig) (zapcore.Encoder, error))
75	defer func() { _encoderNameToConstructor = existing }()
76	f()
77}
78
79func testEncodersRegistered(t *testing.T, names ...string) {
80	assert.Len(t, _encoderNameToConstructor, len(names), "the expected number of registered encoders does not match the actual number")
81	for _, name := range names {
82		assert.NotNil(t, _encoderNameToConstructor[name], "no encoder is registered for name %s", name)
83	}
84}
85
86func newNilEncoder(_ zapcore.EncoderConfig) (zapcore.Encoder, error) {
87	return nil, nil
88}
89