1// Copyright (c) 2012 - Cloud Instruments Co., Ltd.
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this
9//    list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11//    this list of conditions and the following disclaimer in the documentation
12//    and/or other materials provided with the distribution.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25package seelog
26
27import (
28	"testing"
29)
30
31func TestInvalidminMaxConstraints(t *testing.T) {
32	constr, err := NewMinMaxConstraints(CriticalLvl, WarnLvl)
33
34	if err == nil || constr != nil {
35		t.Errorf("expected an error and a nil value for minmax constraints: min = %d, max = %d. Got: %v, %v",
36			CriticalLvl, WarnLvl, err, constr)
37		return
38	}
39}
40
41func TestInvalidLogLevels(t *testing.T) {
42	var invalidMin uint8 = 123
43	var invalidMax uint8 = 124
44	minMaxConstr, errMinMax := NewMinMaxConstraints(LogLevel(invalidMin), LogLevel(invalidMax))
45
46	if errMinMax == nil || minMaxConstr != nil {
47		t.Errorf("expected an error and a nil value for minmax constraints: min = %d, max = %d. Got: %v, %v",
48			invalidMin, invalidMax, errMinMax, minMaxConstr)
49		return
50	}
51
52	invalidList := []LogLevel{145}
53
54	listConstr, errList := NewListConstraints(invalidList)
55
56	if errList == nil || listConstr != nil {
57		t.Errorf("expected an error and a nil value for constraints list: %v. Got: %v, %v",
58			invalidList, errList, listConstr)
59		return
60	}
61}
62
63func TestlistConstraintsWithDuplicates(t *testing.T) {
64	duplicateList := []LogLevel{TraceLvl, DebugLvl, InfoLvl,
65		WarnLvl, ErrorLvl, CriticalLvl, CriticalLvl, CriticalLvl}
66
67	listConstr, errList := NewListConstraints(duplicateList)
68
69	if errList != nil || listConstr == nil {
70		t.Errorf("expected a valid constraints list struct for: %v, got error: %v, value: %v",
71			duplicateList, errList, listConstr)
72		return
73	}
74
75	listLevels := listConstr.AllowedLevels()
76
77	if listLevels == nil {
78		t.Fatalf("listConstr.AllowedLevels() == nil")
79		return
80	}
81
82	if len(listLevels) != 6 {
83		t.Errorf("expected: listConstr.AllowedLevels() length == 6. Got: %d", len(listLevels))
84		return
85	}
86}
87
88func TestlistConstraintsWithOffInList(t *testing.T) {
89	offList := []LogLevel{TraceLvl, DebugLvl, Off}
90
91	listConstr, errList := NewListConstraints(offList)
92
93	if errList == nil || listConstr != nil {
94		t.Errorf("expected an error and a nil value for constraints list with 'Off':  %v. Got: %v, %v",
95			offList, errList, listConstr)
96		return
97	}
98}
99
100type logLevelTestCase struct {
101	level   LogLevel
102	allowed bool
103}
104
105var minMaxTests = []logLevelTestCase{
106	{TraceLvl, false},
107	{DebugLvl, false},
108	{InfoLvl, true},
109	{WarnLvl, true},
110	{ErrorLvl, false},
111	{CriticalLvl, false},
112	{123, false},
113	{6, false},
114}
115
116func TestValidminMaxConstraints(t *testing.T) {
117
118	constr, err := NewMinMaxConstraints(InfoLvl, WarnLvl)
119
120	if err != nil || constr == nil {
121		t.Errorf("expected a valid constraints struct for minmax constraints: min = %d, max = %d. Got: %v, %v",
122			InfoLvl, WarnLvl, err, constr)
123		return
124	}
125
126	for _, minMaxTest := range minMaxTests {
127		allowed := constr.IsAllowed(minMaxTest.level)
128		if allowed != minMaxTest.allowed {
129			t.Errorf("expected IsAllowed() = %t for level = %d. Got: %t",
130				minMaxTest.allowed, minMaxTest.level, allowed)
131			return
132		}
133	}
134}
135
136var listTests = []logLevelTestCase{
137	{TraceLvl, true},
138	{DebugLvl, false},
139	{InfoLvl, true},
140	{WarnLvl, true},
141	{ErrorLvl, false},
142	{CriticalLvl, true},
143	{123, false},
144	{6, false},
145}
146
147func TestValidlistConstraints(t *testing.T) {
148	validList := []LogLevel{TraceLvl, InfoLvl, WarnLvl, CriticalLvl}
149	constr, err := NewListConstraints(validList)
150
151	if err != nil || constr == nil {
152		t.Errorf("expected a valid constraints list struct for: %v. Got error: %v, value: %v",
153			validList, err, constr)
154		return
155	}
156
157	for _, minMaxTest := range listTests {
158		allowed := constr.IsAllowed(minMaxTest.level)
159		if allowed != minMaxTest.allowed {
160			t.Errorf("expected IsAllowed() = %t for level = %d. Got: %t",
161				minMaxTest.allowed, minMaxTest.level, allowed)
162			return
163		}
164	}
165}
166
167var offTests = []logLevelTestCase{
168	{TraceLvl, false},
169	{DebugLvl, false},
170	{InfoLvl, false},
171	{WarnLvl, false},
172	{ErrorLvl, false},
173	{CriticalLvl, false},
174	{123, false},
175	{6, false},
176}
177
178func TestValidListoffConstraints(t *testing.T) {
179	validList := []LogLevel{Off}
180	constr, err := NewListConstraints(validList)
181
182	if err != nil || constr == nil {
183		t.Errorf("expected a valid constraints list struct for: %v. Got error: %v, value: %v",
184			validList, err, constr)
185		return
186	}
187
188	for _, minMaxTest := range offTests {
189		allowed := constr.IsAllowed(minMaxTest.level)
190		if allowed != minMaxTest.allowed {
191			t.Errorf("expected IsAllowed() = %t for level = %d. Got: %t",
192				minMaxTest.allowed, minMaxTest.level, allowed)
193			return
194		}
195	}
196}
197