pattern_test.go 963 B

1234567891011121314151617181920212223242526272829303132333435
  1. package log
  2. import (
  3. "bytes"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestPatternDefault(t *testing.T) {
  10. buf := &bytes.Buffer{}
  11. p := newPatternRender("%L %T %f %M")
  12. p.Render(buf, map[string]interface{}{_level: _infoLevel.String(), _log: "hello", _time: time.Now().Format(_timeFormat), _source: "xxx:123"})
  13. fields := strings.Fields(buf.String())
  14. assert.Equal(t, 4, len(fields))
  15. assert.Equal(t, "INFO", fields[0])
  16. assert.Equal(t, "hello", fields[3])
  17. }
  18. func TestKV(t *testing.T) {
  19. buf := &bytes.Buffer{}
  20. p := newPatternRender("%M")
  21. p.Render(buf, map[string]interface{}{_level: _infoLevel.String(), _log: "2233", "hello": "test"})
  22. assert.Equal(t, "hello=test 2233", buf.String())
  23. }
  24. func TestBadSymbol(t *testing.T) {
  25. buf := &bytes.Buffer{}
  26. p := newPatternRender("%12 %% %xd %M")
  27. p.Render(buf, map[string]interface{}{_level: _infoLevel.String(), _log: "2233"})
  28. assert.Equal(t, "%12 %% %xd 2233", buf.String())
  29. }