json_encoder_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2018 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 zapcore_test
  21. import (
  22. "testing"
  23. "time"
  24. "github.com/stretchr/testify/assert"
  25. "go.uber.org/zap"
  26. "go.uber.org/zap/zapcore"
  27. )
  28. // TestJSONEncodeEntry is an more "integrated" test that makes it easier to get
  29. // coverage on the json encoder (e.g. putJSONEncoder, let alone EncodeEntry
  30. // itself) than the tests in json_encoder_impl_test.go; it needs to be in the
  31. // zapcore_test package, so that it can import the toplevel zap package for
  32. // field constructor convenience.
  33. func TestJSONEncodeEntry(t *testing.T) {
  34. type bar struct {
  35. Key string `json:"key"`
  36. Val float64 `json:"val"`
  37. }
  38. type foo struct {
  39. A string `json:"aee"`
  40. B int `json:"bee"`
  41. C float64 `json:"cee"`
  42. D []bar `json:"dee"`
  43. }
  44. tests := []struct {
  45. desc string
  46. expected string
  47. ent zapcore.Entry
  48. fields []zapcore.Field
  49. }{
  50. {
  51. desc: "info entry with some fields",
  52. expected: `{
  53. "L": "info",
  54. "T": "2018-06-19T16:33:42.000Z",
  55. "N": "bob",
  56. "M": "lob law",
  57. "so": "passes",
  58. "answer": 42,
  59. "common_pie": 3.14,
  60. "null_value": null,
  61. "array_with_null_elements": [{}, null, null, 2],
  62. "such": {
  63. "aee": "lol",
  64. "bee": 123,
  65. "cee": 0.9999,
  66. "dee": [
  67. {"key": "pi", "val": 3.141592653589793},
  68. {"key": "tau", "val": 6.283185307179586}
  69. ]
  70. }
  71. }`,
  72. ent: zapcore.Entry{
  73. Level: zapcore.InfoLevel,
  74. Time: time.Date(2018, 6, 19, 16, 33, 42, 99, time.UTC),
  75. LoggerName: "bob",
  76. Message: "lob law",
  77. },
  78. fields: []zapcore.Field{
  79. zap.String("so", "passes"),
  80. zap.Int("answer", 42),
  81. zap.Float64("common_pie", 3.14),
  82. // Cover special-cased handling of nil in AddReflect() and
  83. // AppendReflect(). Note that for the latter, we explicitly test
  84. // correct results for both the nil static interface{} value
  85. // (`nil`), as well as the non-nil interface value with a
  86. // dynamic type and nil value (`(*struct{})(nil)`).
  87. zap.Reflect("null_value", nil),
  88. zap.Reflect("array_with_null_elements", []interface{}{&struct{}{}, nil, (*struct{})(nil), 2}),
  89. zap.Reflect("such", foo{
  90. A: "lol",
  91. B: 123,
  92. C: 0.9999,
  93. D: []bar{
  94. {"pi", 3.141592653589793},
  95. {"tau", 6.283185307179586},
  96. },
  97. }),
  98. },
  99. },
  100. }
  101. enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{
  102. MessageKey: "M",
  103. LevelKey: "L",
  104. TimeKey: "T",
  105. NameKey: "N",
  106. CallerKey: "C",
  107. StacktraceKey: "S",
  108. EncodeLevel: zapcore.LowercaseLevelEncoder,
  109. EncodeTime: zapcore.ISO8601TimeEncoder,
  110. EncodeDuration: zapcore.SecondsDurationEncoder,
  111. EncodeCaller: zapcore.ShortCallerEncoder,
  112. })
  113. for _, tt := range tests {
  114. t.Run(tt.desc, func(t *testing.T) {
  115. buf, err := enc.EncodeEntry(tt.ent, tt.fields)
  116. if assert.NoError(t, err, "Unexpected JSON encoding error.") {
  117. assert.JSONEq(t, tt.expected, buf.String(), "Incorrect encoded JSON entry.")
  118. }
  119. buf.Free()
  120. })
  121. }
  122. }