-
Notifications
You must be signed in to change notification settings - Fork 1
/
func_test.go
101 lines (79 loc) · 2.13 KB
/
func_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package formatter_test
import (
"bytes"
"fmt"
"strings"
"testing"
"time"
formatter "github.com/helmwave/logrus-emoji-formatter"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
)
type FormatterTestSuite struct {
suite.Suite
}
func (s *FormatterTestSuite) createLogger() (*logrus.Logger, *bytes.Buffer) {
logger := logrus.New()
var buf bytes.Buffer
logger.SetFormatter(&formatter.Config{Color: false})
logger.SetOutput(&buf)
logger.SetLevel(logrus.TraceLevel)
return logger, &buf
}
func (s *FormatterTestSuite) TestDefaultFormatting() {
logger, buf := s.createLogger()
msg := "testblabla"
s.Run("trace", func() {
expected := fmt.Sprintf("[🤮 aka TRACE]: %s\n", msg)
logger.Trace(msg)
defer buf.Reset()
s.Require().Equal(expected, buf.String())
})
s.Run("debug", func() {
expected := fmt.Sprintf("[🤷 aka DEBUG]: %s\n", msg)
logger.Debug(msg)
defer buf.Reset()
s.Require().Equal(expected, buf.String())
})
s.Run("info", func() {
expected := fmt.Sprintf("[🙃 aka INFO]: %s\n", msg)
logger.Info(msg)
defer buf.Reset()
s.Require().Equal(expected, buf.String())
})
s.Run("warn", func() {
expected := fmt.Sprintf("[🙈 aka WARNING]: %s\n", msg)
logger.Warn(msg)
defer buf.Reset()
s.Require().Equal(expected, buf.String())
})
s.Run("error", func() {
expected := fmt.Sprintf("[💩 aka ERROR]: %s\n", msg)
logger.Error(msg)
defer buf.Reset()
s.Require().Equal(expected, buf.String())
})
}
func (s *FormatterTestSuite) TestCustomFormat() {
logger, buf := s.createLogger()
c, _ := logger.Formatter.(*formatter.Config)
c.LogFormat = " "
msg := "testblabla"
logger.Info(msg)
s.Require().Equal(" \n", buf.String())
}
func (s *FormatterTestSuite) TestTimeFormat() {
logger, buf := s.createLogger()
c, _ := logger.Formatter.(*formatter.Config)
c.LogFormat = "%time%"
msg := "testblabla"
expected := time.Now()
logger.Info(msg)
t, err := time.Parse(time.RFC3339, strings.TrimSpace(buf.String()))
s.Require().NoError(err)
s.Require().WithinDuration(expected, t, time.Second)
}
func TestFormatterTestSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(FormatterTestSuite))
}