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.

77 lines
1.4 KiB

  1. package logrus
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestEntryWithError(t *testing.T) {
  9. assert := assert.New(t)
  10. defer func() {
  11. ErrorKey = "error"
  12. }()
  13. err := fmt.Errorf("kaboom at layer %d", 4711)
  14. assert.Equal(err, WithError(err).Data["error"])
  15. logger := New()
  16. logger.Out = &bytes.Buffer{}
  17. entry := NewEntry(logger)
  18. assert.Equal(err, entry.WithError(err).Data["error"])
  19. ErrorKey = "err"
  20. assert.Equal(err, entry.WithError(err).Data["err"])
  21. }
  22. func TestEntryPanicln(t *testing.T) {
  23. errBoom := fmt.Errorf("boom time")
  24. defer func() {
  25. p := recover()
  26. assert.NotNil(t, p)
  27. switch pVal := p.(type) {
  28. case *Entry:
  29. assert.Equal(t, "kaboom", pVal.Message)
  30. assert.Equal(t, errBoom, pVal.Data["err"])
  31. default:
  32. t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
  33. }
  34. }()
  35. logger := New()
  36. logger.Out = &bytes.Buffer{}
  37. entry := NewEntry(logger)
  38. entry.WithField("err", errBoom).Panicln("kaboom")
  39. }
  40. func TestEntryPanicf(t *testing.T) {
  41. errBoom := fmt.Errorf("boom again")
  42. defer func() {
  43. p := recover()
  44. assert.NotNil(t, p)
  45. switch pVal := p.(type) {
  46. case *Entry:
  47. assert.Equal(t, "kaboom true", pVal.Message)
  48. assert.Equal(t, errBoom, pVal.Data["err"])
  49. default:
  50. t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
  51. }
  52. }()
  53. logger := New()
  54. logger.Out = &bytes.Buffer{}
  55. entry := NewEntry(logger)
  56. entry.WithField("err", errBoom).Panicf("kaboom %v", true)
  57. }