-
Notifications
You must be signed in to change notification settings - Fork 0
/
fieldparser_test.go
123 lines (116 loc) · 3.44 KB
/
fieldparser_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package sqllogging
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)
func TestParseIntValue(t *testing.T) {
tests := []struct {
input string
number int
found bool
}{
{input: "+123", number: 123, found: true},
{input: "+123 asdf", number: 123, found: true},
{input: "-1", number: -1, found: true},
{input: "123 asdf", number: 123, found: true},
{input: "123asdf", number: 0, found: false},
{input: " 123", number: 0, found: false},
{input: "x123", number: 0, found: false},
{input: "", number: 0, found: false},
}
for i, tc := range tests {
testname := fmt.Sprintf("Test %d", i)
s := scanner{input: "abcd" + tc.input, pos: 4}
number, found := parseIntValue(&s)
assert.Equal(t, tc.number, number, testname)
assert.Equal(t, tc.found, found, testname)
}
}
func TestParseStringValue(t *testing.T) {
tests := []struct {
input, value, remainder string
found bool
}{
{input: "hello world] remainder", value: "hello world", remainder: " remainder", found: true},
{input: "hello world]] part of value]", value: "hello world] part of value", remainder: "", found: true},
{input: "]]]", value: "]", remainder: "", found: true},
{input: "]]]]]", value: "]]", remainder: "", found: true},
{input: "", value: "", remainder: "", found: false},
{input: "]", value: "", remainder: "", found: true},
{input: "no bracket at the end", found: false},
}
for _, tc := range tests {
tc.input = "abcd" + tc.input
s := scanner{input: tc.input, pos: 4}
value, found := parseStringValue(&s)
require.Equal(t, tc.found, found)
if found {
require.Equal(t, tc.value, value)
require.Equal(t, tc.remainder, tc.input[s.pos:])
}
}
}
func TestParseKeyValuePair(t *testing.T) {
tests := []struct {
input string
kv keyValue
found bool
}{
{input: "abc=", kv: keyValue{"abc", nil}, found: true},
{input: "abc= ", kv: keyValue{"abc", nil}, found: true},
{input: "a=1", kv: keyValue{"a", 1}, found: true},
{input: "abc=[1]", kv: keyValue{"abc", "1"}, found: true},
{input: "a=1 ", kv: keyValue{"a", 1}, found: true},
{input: "abc=[1] ", kv: keyValue{"abc", "1"}, found: true},
{input: " a=1", found: false},
}
for i, tc := range tests {
testname := fmt.Sprintf("Test %d", i)
s := scanner{input: "abcd" + tc.input, pos: 4}
kv, found := parseKeyValue(&s)
assert.Equal(t, tc.found, found, testname)
if found {
assert.Equal(t, tc.kv, kv, testname)
} else {
assert.Equal(t, 4, s.pos)
}
}
}
func TestParseFields(t *testing.T) {
tests := []struct {
input string
fields logrus.Fields
msg string
}{
{
input: "one=[aaa]two=[bbb]three=3 four=",
fields: logrus.Fields{"one": "aaa", "two": "bbb", "three": 3, "four": nil},
msg: "",
},
{
input: "a=[1]b=[2 ]] \" /* asdf */ lots of junk ]]] msg with [] ]] c=[3]",
fields: logrus.Fields{"a": "1", "b": "2 ] \" /* asdf */ lots of junk ]"},
msg: "msg with [] ]] c=[3]",
},
{
// space before =; syntax error
input: "a =[1]b=[2]c=[3]",
fields: nil,
msg: "a =[1]b=[2]c=[3]",
},
{
// allow whitespace between entries
input: "nil= a=[1] \t \n b=[2] \t \t \t \n c=[3] \t \n msg",
fields: logrus.Fields{"a": "1", "b": "2", "c": "3", "nil": nil},
msg: "msg",
},
}
for _, tc := range tests {
fields, msg := parseFields(tc.input)
require.Equal(t, tc.fields, fields)
require.Equal(t, tc.msg, msg)
}
}