File: init_test.go

package info (click to toggle)
golang-github-revel-revel 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,240 kB
  • sloc: xml: 7; makefile: 7; javascript: 1
file content (273 lines) | stat: -rw-r--r-- 7,858 bytes parent folder | download | duplicates (2)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2012-2018 The Revel Framework Authors, All rights reserved.
// Revel Framework source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.

package logger_test

import (
	"github.com/revel/config"
	"github.com/revel/revel/logger"
	"github.com/stretchr/testify/assert"
	"os"
	"strings"
	"testing"
)

type (
	// A counter for the tester
	testCounter struct {
		debug, info, warn, error, critical int
	}
	// The data to tes
	testData struct {
		config []string
		result testResult
		tc     *testCounter
	}
	// The test result
	testResult struct {
		debug, info, warn, error, critical int
	}
)

// Single test cases
var singleCases = []testData{
	{config: []string{"log.crit.output"},
		result: testResult{0, 0, 0, 0, 1}},
	{config: []string{"log.error.output"},
		result: testResult{0, 0, 0, 1, 1}},
	{config: []string{"log.warn.output"},
		result: testResult{0, 0, 1, 0, 0}},
	{config: []string{"log.info.output"},
		result: testResult{0, 1, 0, 0, 0}},
	{config: []string{"log.debug.output"},
		result: testResult{1, 0, 0, 0, 0}},
}

// Test singles
func TestSingleCases(t *testing.T) {
	rootLog := logger.New()
	for _, testCase := range singleCases {
		testCase.logTest(rootLog, t)
		testCase.validate(t)
	}
}

// Filter test cases
var filterCases = []testData{
	{config: []string{"log.crit.filter.module.app"},
		result: testResult{0, 0, 0, 0, 1}},
	{config: []string{"log.crit.filter.module.appa"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.error.filter.module.app"},
		result: testResult{0, 0, 0, 1, 1}},
	{config: []string{"log.error.filter.module.appa"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.warn.filter.module.app"},
		result: testResult{0, 0, 1, 0, 0}},
	{config: []string{"log.warn.filter.module.appa"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.info.filter.module.app"},
		result: testResult{0, 1, 0, 0, 0}},
	{config: []string{"log.info.filter.module.appa"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.debug.filter.module.app"},
		result: testResult{1, 0, 0, 0, 0}},
	{config: []string{"log.debug.filter.module.appa"},
		result: testResult{0, 0, 0, 0, 0}},
}

// Filter test
func TestFilterCases(t *testing.T) {
	rootLog := logger.New("module", "app")
	for _, testCase := range filterCases {
		testCase.logTest(rootLog, t)
		testCase.validate(t)
	}
}

// Inverse test cases
var nfilterCases = []testData{
	{config: []string{"log.crit.nfilter.module.appa"},
		result: testResult{0, 0, 0, 0, 1}},
	{config: []string{"log.crit.nfilter.modules.appa"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.crit.nfilter.module.app"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.error.nfilter.module.appa"}, // Special case, when error is not nill critical inherits from error
		result: testResult{0, 0, 0, 1, 1}},
	{config: []string{"log.error.nfilter.module.app"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.warn.nfilter.module.appa"},
		result: testResult{0, 0, 1, 0, 0}},
	{config: []string{"log.warn.nfilter.module.app"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.info.nfilter.module.appa"},
		result: testResult{0, 1, 0, 0, 0}},
	{config: []string{"log.info.nfilter.module.app"},
		result: testResult{0, 0, 0, 0, 0}},
	{config: []string{"log.debug.nfilter.module.appa"},
		result: testResult{1, 0, 0, 0, 0}},
	{config: []string{"log.debug.nfilter.module.app"},
		result: testResult{0, 0, 0, 0, 0}},
}

// Inverse test
func TestNotFilterCases(t *testing.T) {
	rootLog := logger.New("module", "app")
	for _, testCase := range nfilterCases {
		testCase.logTest(rootLog, t)
		testCase.validate(t)
	}
}

// off test cases
var offCases = []testData{
	{config: []string{"log.all.output", "log.error.output=off"},
		result: testResult{1, 1, 1, 0, 1}},
}

// Off test
func TestOffCases(t *testing.T) {
	rootLog := logger.New("module", "app")
	for _, testCase := range offCases {
		testCase.logTest(rootLog, t)
		testCase.validate(t)
	}
}

// Duplicate test cases
var duplicateCases = []testData{
	{config: []string{"log.all.output", "log.error.output", "log.error.filter.module.app"},
		result: testResult{1, 1, 1, 2, 1}},
}

// test duplicate cases
func TestDuplicateCases(t *testing.T) {
	rootLog := logger.New("module", "app")
	for _, testCase := range duplicateCases {
		testCase.logTest(rootLog, t)
		testCase.validate(t)
	}
}

// Contradicting cases
var contradictCases = []testData{
	{config: []string{"log.all.output", "log.error.output=off", "log.all.output"},
		result: testResult{1, 1, 1, 0, 1}},
	{config: []string{"log.all.output", "log.error.output=off", "log.debug.filter.module.app"},
		result: testResult{2, 1, 1, 0, 1}},
	{config: []string{"log.all.filter.module.app", "log.info.output=off", "log.info.filter.module.app"},
		result: testResult{1, 2, 1, 1, 1}},
	{config: []string{"log.all.output", "log.info.output=off", "log.info.filter.module.app"},
		result: testResult{1, 1, 1, 1, 1}},
}

// Contradiction test
func TestContradictCases(t *testing.T) {
	rootLog := logger.New("module", "app")
	for _, testCase := range contradictCases {
		testCase.logTest(rootLog, t)
		testCase.validate(t)
	}
}

// All test cases
var allCases = []testData{
	{config: []string{"log.all.filter.module.app"},
		result: testResult{1, 1, 1, 1, 1}},
	{config: []string{"log.all.output"},
		result: testResult{2, 2, 2, 2, 2}},
}

// All tests
func TestAllCases(t *testing.T) {
	rootLog := logger.New("module", "app")
	for i, testCase := range allCases {
		testCase.logTest(rootLog, t)
		allCases[i] = testCase
	}
	rootLog = logger.New()
	for i, testCase := range allCases {
		testCase.logTest(rootLog, t)
		allCases[i] = testCase
	}
	for _, testCase := range allCases {
		testCase.validate(t)
	}

}

func (c *testCounter) Log(r *logger.Record) error {
	switch r.Level {
	case logger.LvlDebug:
		c.debug++
	case logger.LvlInfo:
		c.info++
	case logger.LvlWarn:
		c.warn++
	case logger.LvlError:
		c.error++
	case logger.LvlCrit:
		c.critical++
	default:
		panic("Unknown log level")
	}
	return nil
}
func (td *testData) logTest(rootLog logger.MultiLogger, t *testing.T) {
	if td.tc == nil {
		td.tc = &testCounter{}
		counterInit(td.tc)
	}
	newContext := config.NewContext()
	for _, i := range td.config {
		iout := strings.Split(i, "=")
		if len(iout) > 1 {
			newContext.SetOption(iout[0], iout[1])
		} else {
			newContext.SetOption(i, "test")
		}
	}

	newContext.SetOption("specialUseFlag", "true")

	handler := logger.InitializeFromConfig("test", newContext)

	rootLog.SetHandler(handler)

	td.runLogTest(rootLog)
}

func (td *testData) runLogTest(log logger.MultiLogger) {
	log.Debug("test")
	log.Info("test")
	log.Warn("test")
	log.Error("test")
	log.Crit("test")
}

func (td *testData) validate(t *testing.T) {
	t.Logf("Test %#v expected %#v", td.tc, td.result)
	assert.Equal(t, td.result.debug, td.tc.debug, "Debug failed "+strings.Join(td.config, " "))
	assert.Equal(t, td.result.info, td.tc.info, "Info failed "+strings.Join(td.config, " "))
	assert.Equal(t, td.result.warn, td.tc.warn, "Warn failed "+strings.Join(td.config, " "))
	assert.Equal(t, td.result.error, td.tc.error, "Error failed "+strings.Join(td.config, " "))
	assert.Equal(t, td.result.critical, td.tc.critical, "Critical failed "+strings.Join(td.config, " "))
}

// Add test to the function map
func counterInit(tc *testCounter) {
	logger.LogFunctionMap["test"] = func(c *logger.CompositeMultiHandler, logOptions *logger.LogOptions) {
		// Output to the test log and the stdout
		outHandler := logger.LogHandler(
			logger.NewListLogHandler(tc,
				logger.StreamHandler(os.Stdout, logger.TerminalFormatHandler(false, true))),
		)
		if logOptions.HandlerWrap != nil {
			outHandler = logOptions.HandlerWrap.SetChild(outHandler)
		}

		c.SetHandlers(outHandler, logOptions)
	}
}