Merge "Add INFO.yaml"
[iec.git] / src / foundation / api / revel / logger / init_test.go
1 // Copyright (c) 2012-2018 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package logger_test
6
7 import (
8         "github.com/revel/config"
9         "github.com/revel/revel/logger"
10         "github.com/stretchr/testify/assert"
11         "os"
12         "strings"
13         "testing"
14 )
15
16 type (
17         // A counter for the tester
18         testCounter struct {
19                 debug, info, warn, error, critical int
20         }
21         // The data to tes
22         testData struct {
23                 config []string
24                 result testResult
25                 tc     *testCounter
26         }
27         // The test result
28         testResult struct {
29                 debug, info, warn, error, critical int
30         }
31 )
32
33 // Single test cases
34 var singleCases = []testData{
35         {config: []string{"log.crit.output"},
36                 result: testResult{0, 0, 0, 0, 1}},
37         {config: []string{"log.error.output"},
38                 result: testResult{0, 0, 0, 1, 1}},
39         {config: []string{"log.warn.output"},
40                 result: testResult{0, 0, 1, 0, 0}},
41         {config: []string{"log.info.output"},
42                 result: testResult{0, 1, 0, 0, 0}},
43         {config: []string{"log.debug.output"},
44                 result: testResult{1, 0, 0, 0, 0}},
45 }
46
47 // Test singles
48 func TestSingleCases(t *testing.T) {
49         rootLog := logger.New()
50         for _, testCase := range singleCases {
51                 testCase.logTest(rootLog, t)
52                 testCase.validate(t)
53         }
54 }
55
56 // Filter test cases
57 var filterCases = []testData{
58         {config: []string{"log.crit.filter.module.app"},
59                 result: testResult{0, 0, 0, 0, 1}},
60         {config: []string{"log.crit.filter.module.appa"},
61                 result: testResult{0, 0, 0, 0, 0}},
62         {config: []string{"log.error.filter.module.app"},
63                 result: testResult{0, 0, 0, 1, 1}},
64         {config: []string{"log.error.filter.module.appa"},
65                 result: testResult{0, 0, 0, 0, 0}},
66         {config: []string{"log.warn.filter.module.app"},
67                 result: testResult{0, 0, 1, 0, 0}},
68         {config: []string{"log.warn.filter.module.appa"},
69                 result: testResult{0, 0, 0, 0, 0}},
70         {config: []string{"log.info.filter.module.app"},
71                 result: testResult{0, 1, 0, 0, 0}},
72         {config: []string{"log.info.filter.module.appa"},
73                 result: testResult{0, 0, 0, 0, 0}},
74         {config: []string{"log.debug.filter.module.app"},
75                 result: testResult{1, 0, 0, 0, 0}},
76         {config: []string{"log.debug.filter.module.appa"},
77                 result: testResult{0, 0, 0, 0, 0}},
78 }
79
80 // Filter test
81 func TestFilterCases(t *testing.T) {
82         rootLog := logger.New("module", "app")
83         for _, testCase := range filterCases {
84                 testCase.logTest(rootLog, t)
85                 testCase.validate(t)
86         }
87 }
88
89 // Inverse test cases
90 var nfilterCases = []testData{
91         {config: []string{"log.crit.nfilter.module.appa"},
92                 result: testResult{0, 0, 0, 0, 1}},
93         {config: []string{"log.crit.nfilter.modules.appa"},
94                 result: testResult{0, 0, 0, 0, 0}},
95         {config: []string{"log.crit.nfilter.module.app"},
96                 result: testResult{0, 0, 0, 0, 0}},
97         {config: []string{"log.error.nfilter.module.appa"}, // Special case, when error is not nill critical inherits from error
98                 result: testResult{0, 0, 0, 1, 1}},
99         {config: []string{"log.error.nfilter.module.app"},
100                 result: testResult{0, 0, 0, 0, 0}},
101         {config: []string{"log.warn.nfilter.module.appa"},
102                 result: testResult{0, 0, 1, 0, 0}},
103         {config: []string{"log.warn.nfilter.module.app"},
104                 result: testResult{0, 0, 0, 0, 0}},
105         {config: []string{"log.info.nfilter.module.appa"},
106                 result: testResult{0, 1, 0, 0, 0}},
107         {config: []string{"log.info.nfilter.module.app"},
108                 result: testResult{0, 0, 0, 0, 0}},
109         {config: []string{"log.debug.nfilter.module.appa"},
110                 result: testResult{1, 0, 0, 0, 0}},
111         {config: []string{"log.debug.nfilter.module.app"},
112                 result: testResult{0, 0, 0, 0, 0}},
113 }
114
115 // Inverse test
116 func TestNotFilterCases(t *testing.T) {
117         rootLog := logger.New("module", "app")
118         for _, testCase := range nfilterCases {
119                 testCase.logTest(rootLog, t)
120                 testCase.validate(t)
121         }
122 }
123
124 // off test cases
125 var offCases = []testData{
126         {config: []string{"log.all.output", "log.error.output=off"},
127                 result: testResult{1, 1, 1, 0, 1}},
128 }
129
130 // Off test
131 func TestOffCases(t *testing.T) {
132         rootLog := logger.New("module", "app")
133         for _, testCase := range offCases {
134                 testCase.logTest(rootLog, t)
135                 testCase.validate(t)
136         }
137 }
138
139 // Duplicate test cases
140 var duplicateCases = []testData{
141         {config: []string{"log.all.output", "log.error.output", "log.error.filter.module.app"},
142                 result: testResult{1, 1, 1, 2, 1}},
143 }
144
145 // test duplicate cases
146 func TestDuplicateCases(t *testing.T) {
147         rootLog := logger.New("module", "app")
148         for _, testCase := range duplicateCases {
149                 testCase.logTest(rootLog, t)
150                 testCase.validate(t)
151         }
152 }
153
154 // Contradicting cases
155 var contradictCases = []testData{
156         {config: []string{"log.all.output", "log.error.output=off", "log.all.output"},
157                 result: testResult{1, 1, 1, 0, 1}},
158         {config: []string{"log.all.output", "log.error.output=off", "log.debug.filter.module.app"},
159                 result: testResult{2, 1, 1, 0, 1}},
160         {config: []string{"log.all.filter.module.app", "log.info.output=off", "log.info.filter.module.app"},
161                 result: testResult{1, 2, 1, 1, 1}},
162         {config: []string{"log.all.output", "log.info.output=off", "log.info.filter.module.app"},
163                 result: testResult{1, 1, 1, 1, 1}},
164 }
165
166 // Contradiction test
167 func TestContradictCases(t *testing.T) {
168         rootLog := logger.New("module", "app")
169         for _, testCase := range contradictCases {
170                 testCase.logTest(rootLog, t)
171                 testCase.validate(t)
172         }
173 }
174
175 // All test cases
176 var allCases = []testData{
177         {config: []string{"log.all.filter.module.app"},
178                 result: testResult{1, 1, 1, 1, 1}},
179         {config: []string{"log.all.output"},
180                 result: testResult{2, 2, 2, 2, 2}},
181 }
182
183 // All tests
184 func TestAllCases(t *testing.T) {
185         rootLog := logger.New("module", "app")
186         for i, testCase := range allCases {
187                 testCase.logTest(rootLog, t)
188                 allCases[i] = testCase
189         }
190         rootLog = logger.New()
191         for i, testCase := range allCases {
192                 testCase.logTest(rootLog, t)
193                 allCases[i] = testCase
194         }
195         for _, testCase := range allCases {
196                 testCase.validate(t)
197         }
198
199 }
200
201 func (c *testCounter) Log(r *logger.Record) error {
202         switch r.Level {
203         case logger.LvlDebug:
204                 c.debug++
205         case logger.LvlInfo:
206                 c.info++
207         case logger.LvlWarn:
208                 c.warn++
209         case logger.LvlError:
210                 c.error++
211         case logger.LvlCrit:
212                 c.critical++
213         default:
214                 panic("Unknown log level")
215         }
216         return nil
217 }
218 func (td *testData) logTest(rootLog logger.MultiLogger, t *testing.T) {
219         if td.tc == nil {
220                 td.tc = &testCounter{}
221                 counterInit(td.tc)
222         }
223         newContext := config.NewContext()
224         for _, i := range td.config {
225                 iout := strings.Split(i, "=")
226                 if len(iout) > 1 {
227                         newContext.SetOption(iout[0], iout[1])
228                 } else {
229                         newContext.SetOption(i, "test")
230                 }
231         }
232
233         newContext.SetOption("specialUseFlag", "true")
234
235         handler := logger.InitializeFromConfig("test", newContext)
236
237         rootLog.SetHandler(handler)
238
239         td.runLogTest(rootLog)
240 }
241
242 func (td *testData) runLogTest(log logger.MultiLogger) {
243         log.Debug("test")
244         log.Info("test")
245         log.Warn("test")
246         log.Error("test")
247         log.Crit("test")
248 }
249
250 func (td *testData) validate(t *testing.T) {
251         t.Logf("Test %#v expected %#v", td.tc, td.result)
252         assert.Equal(t, td.result.debug, td.tc.debug, "Debug failed "+strings.Join(td.config, " "))
253         assert.Equal(t, td.result.info, td.tc.info, "Info failed "+strings.Join(td.config, " "))
254         assert.Equal(t, td.result.warn, td.tc.warn, "Warn failed "+strings.Join(td.config, " "))
255         assert.Equal(t, td.result.error, td.tc.error, "Error failed "+strings.Join(td.config, " "))
256         assert.Equal(t, td.result.critical, td.tc.critical, "Critical failed "+strings.Join(td.config, " "))
257 }
258
259 // Add test to the function map
260 func counterInit(tc *testCounter) {
261         logger.LogFunctionMap["test"] = func(c *logger.CompositeMultiHandler, logOptions *logger.LogOptions) {
262                 // Output to the test log and the stdout
263                 outHandler := logger.LogHandler(
264                         logger.NewListLogHandler(tc,
265                                 logger.StreamHandler(os.Stdout, logger.TerminalFormatHandler(false, true))),
266                 )
267                 if logOptions.HandlerWrap != nil {
268                         outHandler = logOptions.HandlerWrap.SetChild(outHandler)
269                 }
270
271                 c.SetHandlers(outHandler, logOptions)
272         }
273 }