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