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
31type exceptionTestCase struct {
32	funcPattern string
33	filePattern string
34	funcName    string
35	fileName    string
36	match       bool
37}
38
39var exceptionTestCases = []exceptionTestCase{
40	{"*", "*", "func", "file", true},
41	{"func*", "*", "func", "file", true},
42	{"*func", "*", "func", "file", true},
43	{"*func", "*", "1func", "file", true},
44	{"func*", "*", "func1", "file", true},
45	{"fu*nc", "*", "func", "file", true},
46	{"fu*nc", "*", "fu1nc", "file", true},
47	{"fu*nc", "*", "func1nc", "file", true},
48	{"*fu*nc*", "*", "somefuntonc", "file", true},
49	{"fu*nc", "*", "f1nc", "file", false},
50	{"func*", "*", "fun", "file", false},
51	{"fu*nc", "*", "func1n", "file", false},
52	{"**f**u**n**c**", "*", "func1n", "file", true},
53}
54
55func TestMatchingCorrectness(t *testing.T) {
56	constraints, err := NewListConstraints([]LogLevel{TraceLvl})
57	if err != nil {
58		t.Error(err)
59		return
60	}
61
62	for _, testCase := range exceptionTestCases {
63		rule, ruleError := NewLogLevelException(testCase.funcPattern, testCase.filePattern, constraints)
64		if ruleError != nil {
65			t.Fatalf("Unexpected error on rule creation: [ %v, %v ]. %v",
66				testCase.funcPattern, testCase.filePattern, ruleError)
67		}
68
69		match := rule.match(testCase.funcName, testCase.fileName)
70		if match != testCase.match {
71			t.Errorf("incorrect matching for [ %v, %v ] [ %v, %v ] Expected: %t. Got: %t",
72				testCase.funcPattern, testCase.filePattern, testCase.funcName, testCase.fileName, testCase.match, match)
73		}
74	}
75}
76
77func TestAsterisksReducing(t *testing.T) {
78	constraints, err := NewListConstraints([]LogLevel{TraceLvl})
79	if err != nil {
80		t.Error(err)
81		return
82	}
83
84	rule, err := NewLogLevelException("***func**", "fi*****le", constraints)
85	if err != nil {
86		t.Error(err)
87		return
88	}
89	expectFunc := "*func*"
90	if rule.FuncPattern() != expectFunc {
91		t.Errorf("asterisks must be reduced. Expect:%v, Got:%v", expectFunc, rule.FuncPattern())
92	}
93
94	expectFile := "fi*le"
95	if rule.FilePattern() != expectFile {
96		t.Errorf("asterisks must be reduced. Expect:%v, Got:%v", expectFile, rule.FilePattern())
97	}
98}
99