encoder_test.go 3.3 KB

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