Debian Codegraph @

parsercommon/parsercommon_test.go

raw symbols SCIP
1 package parsercommon
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/require"
7 )
8
9 func TestParsePriority(t *testing.T) {
10 testCases := []struct {
11 description string
12 input []byte
13 expectedPri *Priority
14 expectedCursorPos int
15 expectedErr error
16 }{
17 {
18 description: "empty priority",
19 input: []byte(""),
20 expectedPri: nil,
21 expectedCursorPos: 0,
22 expectedErr: ErrPriorityEmpty,
23 },
24 {
25 description: "no start",
26 input: []byte("7>"),
27 expectedPri: nil,
28 expectedCursorPos: 0,
29 expectedErr: ErrPriorityNoStart,
30 },
31 {
32 description: "no end",
33 input: []byte("<77"),
34 expectedPri: nil,
35 expectedCursorPos: 0,
36 expectedErr: ErrPriorityNoEnd,
37 },
38 {
39 description: "too short",
40 input: []byte("<>"),
41 expectedPri: nil,
42 expectedCursorPos: 0,
43 expectedErr: ErrPriorityTooShort,
44 },
45 {
46 description: "too long",
47 input: []byte("<1233>"),
48 expectedPri: nil,
49 expectedCursorPos: 0,
50 expectedErr: ErrPriorityTooLong,
51 },
52 {
53 description: "no digits",
54 input: []byte("<7a8>"),
55 expectedPri: nil,
56 expectedCursorPos: 0,
57 expectedErr: ErrPriorityNonDigit,
58 },
59 {
60 description: "all good",
61 input: []byte("<190>"),
62 expectedPri: NewPriority(190),
63 expectedCursorPos: 5,
64 expectedErr: nil,
65 },
66 }
67
68 for _, tc := range testCases {
69 cursor := 0
70
71 obtained, err := ParsePriority(
72 tc.input, &cursor, len(tc.input),
73 )
74
75 require.Equal(
76 t, tc.expectedPri, obtained, tc.description,
77 )
78
79 require.Equal(
80 t, tc.expectedCursorPos, cursor, tc.description,
81 )
82
83 require.Equal(
84 t, tc.expectedErr, err, tc.description,
85 )
86 }
87 }
88
89 func TestNewPriority(t *testing.T) {
90 require.Equal(
91 t,
92 &Priority{
93 P: 165,
94 F: Facility{Value: 20},
95 S: Severity{Value: 5},
96 },
97 NewPriority(165),
98 )
99 }
100
101 func TestParseVersion(t *testing.T) {
102 testCases := []struct {
103 description string
104 input []byte
105 expectedVersion int
106 expectedCursorPos int
107 expectedErr error
108 }{
109 {
110 description: "not found",
111 input: []byte("<123>"),
112 expectedVersion: NO_VERSION,
113 expectedCursorPos: 5,
114 expectedErr: ErrVersionNotFound,
115 },
116 {
117 description: "non digit",
118 input: []byte("<123>a"),
119 expectedVersion: NO_VERSION,
120 expectedCursorPos: 6,
121 expectedErr: nil,
122 },
123 {
124 description: "all good",
125 input: []byte("<123>1"),
126 expectedVersion: 1,
127 expectedCursorPos: 6,
128 expectedErr: nil,
129 },
130 }
131
132 for _, tc := range testCases {
133 cursor := 5
134
135 obtained, err := ParseVersion(
136 tc.input, &cursor, len(tc.input),
137 )
138
139 require.Equal(
140 t, tc.expectedVersion, obtained, tc.description,
141 )
142
143 require.Equal(
144 t, tc.expectedCursorPos, cursor, tc.description,
145 )
146
147 require.Equal(
148 t, tc.expectedErr, err, tc.description,
149 )
150 }
151 }
152
153 func TestParseHostname(t *testing.T) {
154 testCases := []struct {
155 description string
156 input []byte
157 expectedHostname string
158 expectedCursorPos int
159 }{
160 {
161 description: "invalid",
162 input: []byte("foo name"),
163 expectedHostname: "foo",
164 expectedCursorPos: 3,
165 },
166 {
167 description: "valid",
168 input: []byte("ubuntu11.somehost.com" + " "),
169 expectedHostname: "ubuntu11.somehost.com",
170 expectedCursorPos: len("ubuntu11.somehost.com"),
171 },
172 }
173
174 for _, tc := range testCases {
175 cursor := 0
176
177 obtained, err := ParseHostname(
178 tc.input, &cursor, len(tc.input),
179 )
180
181 require.Equal(
182 t, tc.expectedHostname, obtained, tc.description,
183 )
184
185 require.Equal(
186 t, tc.expectedCursorPos, cursor, tc.description,
187 )
188
189 require.Nil(
190 t, err,
191 )
192 }
193 }
194
195 func TestFindNextSpace(t *testing.T) {
196 testCases := []struct {
197 description string
198 input []byte
199 expectedCursorPos int
200 expectedErr error
201 }{
202 {
203 description: "no space",
204 input: []byte("aaaaaa"),
205 expectedCursorPos: 0,
206 expectedErr: ErrNoSpace,
207 },
208 {
209 description: "space found",
210 input: []byte("foo bar baz"),
211 expectedCursorPos: 4,
212 expectedErr: nil,
213 },
214 }
215
216 for _, tc := range testCases {
217 obtained, err := FindNextSpace(
218 tc.input, 0, len(tc.input),
219 )
220
221 require.Equal(
222 t, tc.expectedCursorPos, obtained, tc.description,
223 )
224
225 require.Equal(
226 t, tc.expectedErr, err, tc.description,
227 )
228 }
229 }
230
231 func BenchmarkParsePriority(b *testing.B) {
232 buff := []byte("<190>")
233 var start int
234 l := len(buff)
235
236 for i := 0; i < b.N; i++ {
237 start = 0
238 _, err := ParsePriority(buff, &start, l)
239 if err != nil {
240 panic(err)
241 }
242 }
243 }
244
245 func BenchmarkParseVersion(b *testing.B) {
246 buff := []byte("<123>1")
247 start := 5
248 l := len(buff)
249
250 for i := 0; i < b.N; i++ {
251 start = 0
252 _, err := ParseVersion(buff, &start, l)
253 if err != nil {
254 panic(err)
255 }
256 }
257 }