cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/read_test.go
202lines · modecode
| 1 | package parser_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/google/go-cmp/cmp" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | |
| 12 | "github.com/cloudflare/pint/internal/comments" |
| 13 | "github.com/cloudflare/pint/internal/parser" |
| 14 | ) |
| 15 | |
| 16 | func TestReadContent(t *testing.T) { |
| 17 | type testCaseT struct { |
| 18 | input []byte |
| 19 | output []byte |
| 20 | comments []comments.Comment |
| 21 | ignored bool |
| 22 | shouldError bool |
| 23 | } |
| 24 | |
| 25 | testCases := []testCaseT{ |
| 26 | { |
| 27 | input: []byte(""), |
| 28 | output: []byte(""), |
| 29 | }, |
| 30 | { |
| 31 | input: []byte("\n"), |
| 32 | output: []byte("\n"), |
| 33 | }, |
| 34 | { |
| 35 | input: []byte("\n \n"), |
| 36 | output: []byte("\n \n"), |
| 37 | }, |
| 38 | { |
| 39 | input: []byte("foo bar"), |
| 40 | output: []byte("foo bar"), |
| 41 | }, |
| 42 | { |
| 43 | input: []byte("foo bar\n"), |
| 44 | output: []byte("foo bar\n"), |
| 45 | }, |
| 46 | { |
| 47 | input: []byte("line1\nline2"), |
| 48 | output: []byte("line1\nline2"), |
| 49 | }, |
| 50 | { |
| 51 | input: []byte("line1\nline2\n"), |
| 52 | output: []byte("line1\nline2\n"), |
| 53 | }, |
| 54 | { |
| 55 | input: []byte("line1\n\nline2\n\n"), |
| 56 | output: []byte("line1\n\nline2\n\n"), |
| 57 | }, |
| 58 | { |
| 59 | input: []byte("# pint ignore/next-line\nfoo\n"), |
| 60 | output: []byte("# pint ignore/next-line\n \n"), |
| 61 | }, |
| 62 | { |
| 63 | input: []byte("# pint ignore/next-line\nfoo"), |
| 64 | output: []byte("# pint ignore/next-line\n "), |
| 65 | }, |
| 66 | { |
| 67 | input: []byte("# pint ignore/next-line\nfoo\n\n"), |
| 68 | output: []byte("# pint ignore/next-line\n \n\n"), |
| 69 | }, |
| 70 | { |
| 71 | input: []byte("# pint ignore/next-line\nfoo\nbar\n"), |
| 72 | output: []byte("# pint ignore/next-line\n \nbar\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("# pint ignore/next-line \nfoo\n"), |
| 84 | output: []byte("# pint ignore/next-line \n \n"), |
| 85 | }, |
| 86 | { |
| 87 | input: []byte("# pintignore/next-line\nfoo\n"), |
| 88 | output: []byte("# pintignore/next-line\nfoo\n"), |
| 89 | }, |
| 90 | { |
| 91 | input: []byte("# pint ignore/next-linex\nfoo\n"), |
| 92 | output: []byte("# pint ignore/next-linex\nfoo\n"), |
| 93 | }, |
| 94 | { |
| 95 | input: []byte("# pint ignore/begin\nfoo\nbar\n"), |
| 96 | output: []byte("# pint ignore/begin\n \n \n"), |
| 97 | }, |
| 98 | { |
| 99 | input: []byte("prefix # pint ignore/begin\nfoo\nbar\n"), |
| 100 | output: []byte("prefix # pint ignore/begin\n \n \n"), |
| 101 | }, |
| 102 | { |
| 103 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin"), |
| 104 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin"), |
| 105 | }, |
| 106 | { |
| 107 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin\nfoo\n"), |
| 108 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin\n \n"), |
| 109 | }, |
| 110 | { |
| 111 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/end\nfoo\n"), |
| 112 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/end\nfoo\n"), |
| 113 | }, |
| 114 | { |
| 115 | input: []byte("# pint ignore/begin\nfoo # pint ignore/line\nbar\n# pint ignore/begin"), |
| 116 | output: []byte("# pint ignore/begin\n # pint ignore/line\n \n# pint ignore/begin"), |
| 117 | }, |
| 118 | { |
| 119 | input: []byte("line1\nline2 # pint ignore/line\n"), |
| 120 | output: []byte("line1\n # pint ignore/line\n"), |
| 121 | }, |
| 122 | { |
| 123 | input: []byte("line1\nline2 # pint ignore/line\nline3\n"), |
| 124 | output: []byte("line1\n # pint ignore/line\nline3\n"), |
| 125 | }, |
| 126 | { |
| 127 | input: []byte("{#- comment #} # pint ignore/line\n"), |
| 128 | output: []byte(" # pint ignore/line\n"), |
| 129 | }, |
| 130 | { |
| 131 | input: []byte("# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 132 | output: []byte("# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 133 | ignored: true, |
| 134 | }, |
| 135 | { |
| 136 | input: []byte("foo\n# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 137 | output: []byte("foo\n# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 138 | ignored: true, |
| 139 | }, |
| 140 | { |
| 141 | input: []byte(" {% raw %} # pint ignore/line\n"), |
| 142 | output: []byte(" # pint ignore/line\n"), |
| 143 | }, |
| 144 | { |
| 145 | input: []byte("{# comment #} # pint ignore/line\n"), |
| 146 | output: []byte(" # pint ignore/line\n"), |
| 147 | }, |
| 148 | { |
| 149 | input: []byte("# pint file/owner bob\n# pint rule/set xxx\n# pint bamboozle xxx\n"), |
| 150 | output: []byte("# pint file/owner bob\n# pint rule/set xxx\n# pint bamboozle xxx\n"), |
| 151 | comments: []comments.Comment{ |
| 152 | { |
| 153 | Type: comments.FileOwnerType, |
| 154 | Value: comments.Owner{Name: "bob"}, |
| 155 | }, |
| 156 | }, |
| 157 | }, |
| 158 | { |
| 159 | input: []byte("{#- hide this comment -#} # pint ignore/line\n"), |
| 160 | output: []byte(" # pint ignore/line\n"), |
| 161 | }, |
| 162 | } |
| 163 | |
| 164 | cmpErrorText := cmp.Comparer(func(x, y interface{}) bool { |
| 165 | xe := x.(error) |
| 166 | ye := y.(error) |
| 167 | return xe.Error() == ye.Error() |
| 168 | }) |
| 169 | sameErrorText := cmp.FilterValues(func(x, y interface{}) bool { |
| 170 | _, xe := x.(error) |
| 171 | _, ye := y.(error) |
| 172 | return xe && ye |
| 173 | }, cmpErrorText) |
| 174 | |
| 175 | for i, tc := range testCases { |
| 176 | t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 177 | r := bytes.NewReader(tc.input) |
| 178 | output, comments, err := parser.ReadContent(r) |
| 179 | |
| 180 | hadError := err != nil |
| 181 | if hadError != tc.shouldError { |
| 182 | t.Errorf("ReadContent() returned err=%v, expected=%v", err, tc.shouldError) |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | outputLines := strings.Count(string(output.Body), "\n") |
| 187 | inputLines := strings.Count(string(tc.input), "\n") |
| 188 | if outputLines != inputLines { |
| 189 | t.Errorf("ReadContent() returned %d line(s) while input had %d", outputLines, inputLines) |
| 190 | return |
| 191 | } |
| 192 | |
| 193 | if diff := cmp.Diff(tc.comments, comments, sameErrorText); diff != "" { |
| 194 | t.Errorf("ReadContent() returned wrong comments (-want +got):\n%s", diff) |
| 195 | return |
| 196 | } |
| 197 | |
| 198 | require.Equal(t, string(tc.output), string(output.Body), "ReadContent() returned wrong output") |
| 199 | require.Equal(t, tc.ignored, output.Ignored, "ReadContent() returned wrong Ignored value") |
| 200 | }) |
| 201 | } |
| 202 | } |
| 203 | |