You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

level_test.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package logrus_test
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "testing"
  6. "github.com/sirupsen/logrus"
  7. "github.com/stretchr/testify/require"
  8. )
  9. func TestLevelJsonEncoding(t *testing.T) {
  10. type X struct {
  11. Level logrus.Level
  12. }
  13. var x X
  14. x.Level = logrus.WarnLevel
  15. var buf bytes.Buffer
  16. enc := json.NewEncoder(&buf)
  17. require.NoError(t, enc.Encode(x))
  18. dec := json.NewDecoder(&buf)
  19. var y X
  20. require.NoError(t, dec.Decode(&y))
  21. }
  22. func TestLevelUnmarshalText(t *testing.T) {
  23. var u logrus.Level
  24. for _, level := range logrus.AllLevels {
  25. t.Run(level.String(), func(t *testing.T) {
  26. require.NoError(t, u.UnmarshalText([]byte(level.String())))
  27. require.Equal(t, level, u)
  28. })
  29. }
  30. t.Run("invalid", func(t *testing.T) {
  31. require.Error(t, u.UnmarshalText([]byte("invalid")))
  32. })
  33. }
  34. func TestLevelMarshalText(t *testing.T) {
  35. levelStrings := []string{
  36. "panic",
  37. "fatal",
  38. "error",
  39. "warning",
  40. "info",
  41. "debug",
  42. "trace",
  43. }
  44. for idx, val := range logrus.AllLevels {
  45. level := val
  46. t.Run(level.String(), func(t *testing.T) {
  47. var cmp logrus.Level
  48. b, err := level.MarshalText()
  49. require.NoError(t, err)
  50. require.Equal(t, levelStrings[idx], string(b))
  51. err = cmp.UnmarshalText(b)
  52. require.NoError(t, err)
  53. require.Equal(t, level, cmp)
  54. })
  55. }
  56. }