level_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 zapcore
  21. import (
  22. "bytes"
  23. "flag"
  24. "strings"
  25. "testing"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. func TestLevelString(t *testing.T) {
  29. tests := map[Level]string{
  30. DebugLevel: "debug",
  31. InfoLevel: "info",
  32. WarnLevel: "warn",
  33. ErrorLevel: "error",
  34. DPanicLevel: "dpanic",
  35. PanicLevel: "panic",
  36. FatalLevel: "fatal",
  37. Level(-42): "Level(-42)",
  38. }
  39. for lvl, stringLevel := range tests {
  40. assert.Equal(t, stringLevel, lvl.String(), "Unexpected lowercase level string.")
  41. assert.Equal(t, strings.ToUpper(stringLevel), lvl.CapitalString(), "Unexpected all-caps level string.")
  42. }
  43. }
  44. func TestLevelText(t *testing.T) {
  45. tests := []struct {
  46. text string
  47. level Level
  48. }{
  49. {"debug", DebugLevel},
  50. {"info", InfoLevel},
  51. {"", InfoLevel}, // make the zero value useful
  52. {"warn", WarnLevel},
  53. {"error", ErrorLevel},
  54. {"dpanic", DPanicLevel},
  55. {"panic", PanicLevel},
  56. {"fatal", FatalLevel},
  57. }
  58. for _, tt := range tests {
  59. if tt.text != "" {
  60. lvl := tt.level
  61. marshaled, err := lvl.MarshalText()
  62. assert.NoError(t, err, "Unexpected error marshaling level %v to text.", &lvl)
  63. assert.Equal(t, tt.text, string(marshaled), "Marshaling level %v to text yielded unexpected result.", &lvl)
  64. }
  65. var unmarshaled Level
  66. err := unmarshaled.UnmarshalText([]byte(tt.text))
  67. assert.NoError(t, err, `Unexpected error unmarshaling text %q to level.`, tt.text)
  68. assert.Equal(t, tt.level, unmarshaled, `Text %q unmarshaled to an unexpected level.`, tt.text)
  69. }
  70. }
  71. func TestCapitalLevelsParse(t *testing.T) {
  72. tests := []struct {
  73. text string
  74. level Level
  75. }{
  76. {"DEBUG", DebugLevel},
  77. {"INFO", InfoLevel},
  78. {"WARN", WarnLevel},
  79. {"ERROR", ErrorLevel},
  80. {"DPANIC", DPanicLevel},
  81. {"PANIC", PanicLevel},
  82. {"FATAL", FatalLevel},
  83. }
  84. for _, tt := range tests {
  85. var unmarshaled Level
  86. err := unmarshaled.UnmarshalText([]byte(tt.text))
  87. assert.NoError(t, err, `Unexpected error unmarshaling text %q to level.`, tt.text)
  88. assert.Equal(t, tt.level, unmarshaled, `Text %q unmarshaled to an unexpected level.`, tt.text)
  89. }
  90. }
  91. func TestWeirdLevelsParse(t *testing.T) {
  92. tests := []struct {
  93. text string
  94. level Level
  95. }{
  96. // I guess...
  97. {"Debug", DebugLevel},
  98. {"Info", InfoLevel},
  99. {"Warn", WarnLevel},
  100. {"Error", ErrorLevel},
  101. {"Dpanic", DPanicLevel},
  102. {"Panic", PanicLevel},
  103. {"Fatal", FatalLevel},
  104. // What even is...
  105. {"DeBuG", DebugLevel},
  106. {"InFo", InfoLevel},
  107. {"WaRn", WarnLevel},
  108. {"ErRor", ErrorLevel},
  109. {"DpAnIc", DPanicLevel},
  110. {"PaNiC", PanicLevel},
  111. {"FaTaL", FatalLevel},
  112. }
  113. for _, tt := range tests {
  114. var unmarshaled Level
  115. err := unmarshaled.UnmarshalText([]byte(tt.text))
  116. assert.NoError(t, err, `Unexpected error unmarshaling text %q to level.`, tt.text)
  117. assert.Equal(t, tt.level, unmarshaled, `Text %q unmarshaled to an unexpected level.`, tt.text)
  118. }
  119. }
  120. func TestLevelNils(t *testing.T) {
  121. var l *Level
  122. // The String() method will not handle nil level properly.
  123. assert.Panics(t, func() {
  124. assert.Equal(t, "Level(nil)", l.String(), "Unexpected result stringifying nil *Level.")
  125. }, "Level(nil).String() should panic")
  126. assert.Panics(t, func() {
  127. l.MarshalText()
  128. }, "Expected to panic when marshalling a nil level.")
  129. err := l.UnmarshalText([]byte("debug"))
  130. assert.Equal(t, errUnmarshalNilLevel, err, "Expected to error unmarshalling into a nil Level.")
  131. }
  132. func TestLevelUnmarshalUnknownText(t *testing.T) {
  133. var l Level
  134. err := l.UnmarshalText([]byte("foo"))
  135. assert.Contains(t, err.Error(), "unrecognized level", "Expected unmarshaling arbitrary text to fail.")
  136. }
  137. func TestLevelAsFlagValue(t *testing.T) {
  138. var (
  139. buf bytes.Buffer
  140. lvl Level
  141. )
  142. fs := flag.NewFlagSet("levelTest", flag.ContinueOnError)
  143. fs.SetOutput(&buf)
  144. fs.Var(&lvl, "level", "log level")
  145. for _, expected := range []Level{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, DPanicLevel, PanicLevel, FatalLevel} {
  146. assert.NoError(t, fs.Parse([]string{"-level", expected.String()}))
  147. assert.Equal(t, expected, lvl, "Unexpected level after parsing flag.")
  148. assert.Equal(t, expected, lvl.Get(), "Unexpected output using flag.Getter API.")
  149. assert.Empty(t, buf.String(), "Unexpected error output parsing level flag.")
  150. buf.Reset()
  151. }
  152. assert.Error(t, fs.Parse([]string{"-level", "nope"}))
  153. assert.Equal(
  154. t,
  155. `invalid value "nope" for flag -level: unrecognized level: "nope"`,
  156. strings.Split(buf.String(), "\n")[0], // second line is help message
  157. "Unexpected error output from invalid flag input.",
  158. )
  159. }