cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/read_test.go
156lines · 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 | |
| 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 | if diff := cmp.Diff(string(tc.output), string(output)); diff != "" { |
| 151 | t.Errorf("ReadContent() returned wrong output (-want +got):\n%s", diff) |
| 152 | return |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | } |
| 157 | |