cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.29.3

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/parser/read_test.go

266lines · modecode

1package parser_test
2
3import (
4 "bytes"
5 "fmt"
6 "strconv"
7 "strings"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11
12 "github.com/cloudflare/pint/internal/parser"
13)
14
15func TestReadContent(t *testing.T) {
16 type testCaseT struct {
17 input []byte
18 output []byte
19 shouldError bool
20 }
21
22 testCases := []testCaseT{
23 {
24 input: []byte(""),
25 output: []byte(""),
26 },
27 {
28 input: []byte("\n"),
29 output: []byte("\n"),
30 },
31 {
32 input: []byte("\n \n"),
33 output: []byte("\n \n"),
34 },
35 {
36 input: []byte("foo bar"),
37 output: []byte("foo bar"),
38 },
39 {
40 input: []byte("foo bar\n"),
41 output: []byte("foo bar\n"),
42 },
43 {
44 input: []byte("line1\nline2"),
45 output: []byte("line1\nline2"),
46 },
47 {
48 input: []byte("line1\nline2\n"),
49 output: []byte("line1\nline2\n"),
50 },
51 {
52 input: []byte("line1\n\nline2\n\n"),
53 output: []byte("line1\n\nline2\n\n"),
54 },
55 {
56 input: []byte("# pint ignore/next-line\nfoo\n"),
57 output: []byte("# pint ignore/next-line\n \n"),
58 },
59 {
60 input: []byte("# pint ignore/next-line\nfoo"),
61 output: []byte("# pint ignore/next-line\n "),
62 },
63 {
64 input: []byte("# pint ignore/next-line\nfoo\n\n"),
65 output: []byte("# pint ignore/next-line\n \n\n"),
66 },
67 {
68 input: []byte("# pint ignore/next-line\nfoo\nbar\n"),
69 output: []byte("# pint ignore/next-line\n \nbar\n"),
70 },
71 {
72 input: []byte("# pint ignore/next-line \nfoo\n"),
73 output: []byte("# pint ignore/next-line \n \n"),
74 },
75 {
76 input: []byte("# pint ignore/next-line \nfoo\n"),
77 output: []byte("# pint ignore/next-line \n \n"),
78 },
79 {
80 input: []byte("#pint ignore/next-line \nfoo\n"),
81 output: []byte("#pint ignore/next-line \n \n"),
82 },
83 {
84 input: []byte("#pintignore/next-line\nfoo\n"),
85 output: []byte("#pintignore/next-line\nfoo\n"),
86 },
87 {
88 input: []byte("# pint ignore/next-linex\nfoo\n"),
89 output: []byte("# pint ignore/next-linex\nfoo\n"),
90 },
91 {
92 input: []byte("# pint ignore/begin\nfoo\nbar\n"),
93 output: []byte("# pint ignore/begin\n \n \n"),
94 },
95 {
96 input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin"),
97 output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin"),
98 },
99 {
100 input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin\nfoo\n"),
101 output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin\n \n"),
102 },
103 {
104 input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/end\nfoo\n"),
105 output: []byte("# pint ignore/begin\n \n \n# pint ignore/end\nfoo\n"),
106 },
107 {
108 input: []byte("# pint ignore/begin\nfoo # pint ignore/line\nbar\n# pint ignore/begin"),
109 output: []byte("# pint ignore/begin\n # pint ignore/line\n \n# pint ignore/begin"),
110 },
111 {
112 input: []byte("line1\nline2 # pint ignore/line\n"),
113 output: []byte("line1\n # pint ignore/line\n"),
114 },
115 {
116 input: []byte("line1\nline2 # pint ignore/line\nline3\n"),
117 output: []byte("line1\n # pint ignore/line\nline3\n"),
118 },
119 {
120 input: []byte("{#- comment #} # pint ignore/line\n"),
121 output: []byte(" #- comment #} # pint ignore/line\n"),
122 },
123 {
124 input: []byte("# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"),
125 output: []byte("# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"),
126 },
127 {
128 input: []byte("foo\n# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"),
129 output: []byte("foo\n# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"),
130 },
131 {
132 input: []byte(" {% raw %} # pint ignore/line\n"),
133 output: []byte(" # pint ignore/line\n"),
134 },
135 }
136
137 for i, tc := range testCases {
138 t.Run(strconv.Itoa(i+1), func(t *testing.T) {
139 r := bytes.NewReader(tc.input)
140 output, err := parser.ReadContent(r)
141
142 hadError := err != nil
143 if hadError != tc.shouldError {
144 t.Errorf("ReadContent() returned err=%v, expected=%v", err, tc.shouldError)
145 return
146 }
147
148 outputLines := strings.Count(string(output), "\n")
149 inputLines := strings.Count(string(tc.input), "\n")
150 if outputLines != inputLines {
151 t.Errorf("ReadContent() returned %d line(s) while input had %d", outputLines, inputLines)
152 return
153 }
154
155 require.Equal(t, string(tc.output), string(output), "ReadContent() returned wrong output")
156 })
157 }
158}
159
160func TestGetComment(t *testing.T) {
161 type testCaseT struct {
162 input string
163 comment []string
164 output parser.Comment
165 ok bool
166 }
167
168 testCases := []testCaseT{
169 {
170 input: "",
171 comment: []string{"rule/owner"},
172 },
173 {
174 input: "\n",
175 comment: []string{"rule/owner"},
176 },
177 {
178 input: "\n \n",
179 comment: []string{"rule/owner"},
180 },
181 {
182 input: "foo bar",
183 comment: []string{"rule/owner"},
184 },
185 {
186 input: "foo bar\n",
187 comment: []string{"rule/owner"},
188 },
189 {
190 input: "line1\nline2",
191 comment: []string{"rule/owner"},
192 },
193 {
194 input: "line1\nline2\n",
195 comment: []string{"rule/owner"},
196 },
197 {
198 input: "line1\n\nline2\n\n",
199 comment: []string{"rule/owner"},
200 },
201 {
202 input: "# pint rule/owner",
203 comment: []string{"rule/owner"},
204 ok: true,
205 output: parser.Comment{Key: "rule/owner"},
206 },
207 {
208 input: "# pint rule/owner foo",
209 comment: []string{"rule/owner"},
210 ok: true,
211 output: parser.Comment{Key: "rule/owner", Value: "foo"},
212 },
213 {
214 input: "# pint rule/owner foo bar bob/alice",
215 comment: []string{"rule/owner"},
216 ok: true,
217 output: parser.Comment{Key: "rule/owner", Value: "foo bar bob/alice"},
218 },
219 {
220 input: "line1\n # pint rule/owner foo bar bob/alice\n line2\n\n",
221 comment: []string{"rule/owner"},
222 ok: true,
223 output: parser.Comment{Key: "rule/owner", Value: "foo bar bob/alice"},
224 },
225 {
226 input: "line1\n #### pint rule/owner foo bar bob/alice\n line2\n\n",
227 comment: []string{"rule/owner"},
228 ok: true,
229 output: parser.Comment{Key: "rule/owner", Value: "foo bar bob/alice"},
230 },
231 {
232 input: "# pint set promql/series min-age 1w",
233 comment: []string{"set promql/series min-age"},
234 },
235 {
236 input: "# pint set promql/series min-age 1w",
237 comment: []string{"set", "promql/series", "min-age"},
238 ok: true,
239 output: parser.Comment{Key: "set promql/series min-age", Value: "1w"},
240 },
241 {
242 input: "# pint set promql/series min-age 1w ",
243 comment: []string{"set", "promql/series", "min-age"},
244 ok: true,
245 output: parser.Comment{Key: "set promql/series min-age", Value: "1w"},
246 },
247 {
248 input: "# pint set",
249 comment: []string{"set", "promql/series", "min-age"},
250 },
251 {
252 input: "# pint rule/set promql/series ignore/label-value error",
253 comment: []string{"rule/set", "promql/series", "ignore/label-value"},
254 ok: true,
255 output: parser.Comment{Key: "rule/set promql/series ignore/label-value", Value: "error"},
256 },
257 }
258
259 for i, tc := range testCases {
260 t.Run(fmt.Sprintf("%d/%s", i, tc.input), func(t *testing.T) {
261 output, ok := parser.GetComment(tc.input, tc.comment...)
262 require.Equal(t, tc.ok, ok)
263 require.Equal(t, tc.output, output)
264 })
265 }
266}
267