logrus/entry_test.go

93 lines
1.7 KiB
Go
Raw Normal View History

package logrus
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
2015-05-19 21:20:59 +03:00
var err = fmt.Errorf("kaboom at layer %d", 4711)
func TestToError(t *testing.T) {
assert := assert.New(t)
ctx := WithField("foo", "bar")
assert.Equal(nil, ctx.Debug("Hello").ToError())
ctx.Data[ErrorKey] = "error"
assert.Equal(nil, ctx.Debug("Hello").ToError())
ctx = ctx.WithError(err)
assert.Equal(err, ctx.Debug("Hello").ToError())
}
2015-05-13 14:35:03 +03:00
func TestEntryWithError(t *testing.T) {
assert := assert.New(t)
defer func() {
ErrorKey = "error"
}()
assert.Equal(err, WithError(err).Data["error"])
2015-05-13 14:35:03 +03:00
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
assert.Equal(err, entry.WithError(err).Data["error"])
2015-05-13 14:35:03 +03:00
ErrorKey = "err"
assert.Equal(err, entry.WithError(err).Data["err"])
2015-05-13 14:35:03 +03:00
}
func TestEntryPanicln(t *testing.T) {
errBoom := fmt.Errorf("boom time")
defer func() {
p := recover()
assert.NotNil(t, p)
switch pVal := p.(type) {
case *Entry:
assert.Equal(t, "kaboom", pVal.Message)
assert.Equal(t, errBoom, pVal.Data["err"])
default:
t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
}
}()
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
entry.WithField("err", errBoom).Panicln("kaboom")
}
func TestEntryPanicf(t *testing.T) {
errBoom := fmt.Errorf("boom again")
defer func() {
p := recover()
assert.NotNil(t, p)
switch pVal := p.(type) {
case *Entry:
assert.Equal(t, "kaboom true", pVal.Message)
assert.Equal(t, errBoom, pVal.Data["err"])
default:
t.Fatalf("want type *Entry, got %T: %#v", pVal, pVal)
}
}()
logger := New()
logger.Out = &bytes.Buffer{}
entry := NewEntry(logger)
entry.WithField("err", errBoom).Panicf("kaboom %v", true)
}