cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/read_test.go
155lines · modecode
| 1 | package parser_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/cloudflare/pint/internal/parser" |
| 10 | "github.com/google/go-cmp/cmp" |
| 11 | ) |
| 12 | |
| 13 | func TestReadContent(t *testing.T) { |
| 14 | type testCaseT struct { |
| 15 | input []byte |
| 16 | output []byte |
| 17 | shouldError bool |
| 18 | } |
| 19 | |
| 20 | testCases := []testCaseT{ |
| 21 | { |
| 22 | input: []byte(""), |
| 23 | output: []byte(""), |
| 24 | }, |
| 25 | { |
| 26 | input: []byte("\n"), |
| 27 | output: []byte("\n"), |
| 28 | }, |
| 29 | { |
| 30 | input: []byte("\n \n"), |
| 31 | output: []byte("\n \n"), |
| 32 | }, |
| 33 | { |
| 34 | input: []byte("foo bar"), |
| 35 | output: []byte("foo bar"), |
| 36 | }, |
| 37 | { |
| 38 | input: []byte("foo bar\n"), |
| 39 | output: []byte("foo bar\n"), |
| 40 | }, |
| 41 | { |
| 42 | input: []byte("line1\nline2"), |
| 43 | output: []byte("line1\nline2"), |
| 44 | }, |
| 45 | { |
| 46 | input: []byte("line1\nline2\n"), |
| 47 | output: []byte("line1\nline2\n"), |
| 48 | }, |
| 49 | { |
| 50 | input: []byte("line1\n\nline2\n\n"), |
| 51 | output: []byte("line1\n\nline2\n\n"), |
| 52 | }, |
| 53 | { |
| 54 | input: []byte("# pint ignore/next-line\nfoo\n"), |
| 55 | output: []byte("# pint ignore/next-line\n \n"), |
| 56 | }, |
| 57 | { |
| 58 | input: []byte("# pint ignore/next-line\nfoo"), |
| 59 | output: []byte("# pint ignore/next-line\n "), |
| 60 | }, |
| 61 | { |
| 62 | input: []byte("# pint ignore/next-line\nfoo\n\n"), |
| 63 | output: []byte("# pint ignore/next-line\n \n\n"), |
| 64 | }, |
| 65 | { |
| 66 | input: []byte("# pint ignore/next-line\nfoo\nbar\n"), |
| 67 | output: []byte("# pint ignore/next-line\n \nbar\n"), |
| 68 | }, |
| 69 | { |
| 70 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 71 | output: []byte("# pint ignore/next-line \n \n"), |
| 72 | }, |
| 73 | { |
| 74 | input: []byte("# pint ignore/next-line \nfoo\n"), |
| 75 | output: []byte("# pint ignore/next-line \n \n"), |
| 76 | }, |
| 77 | { |
| 78 | input: []byte("#pint ignore/next-line \nfoo\n"), |
| 79 | output: []byte("#pint ignore/next-line \n \n"), |
| 80 | }, |
| 81 | { |
| 82 | input: []byte("#pintignore/next-line\nfoo\n"), |
| 83 | output: []byte("#pintignore/next-line\nfoo\n"), |
| 84 | }, |
| 85 | { |
| 86 | input: []byte("# pint ignore/next-linex\nfoo\n"), |
| 87 | output: []byte("# pint ignore/next-linex\nfoo\n"), |
| 88 | }, |
| 89 | { |
| 90 | input: []byte("# pint ignore/begin\nfoo\nbar\n"), |
| 91 | output: []byte("# pint ignore/begin\n \n \n"), |
| 92 | }, |
| 93 | { |
| 94 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin"), |
| 95 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin"), |
| 96 | }, |
| 97 | { |
| 98 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/begin\nfoo\n"), |
| 99 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/begin\n \n"), |
| 100 | }, |
| 101 | { |
| 102 | input: []byte("# pint ignore/begin\nfoo\nbar\n# pint ignore/end\nfoo\n"), |
| 103 | output: []byte("# pint ignore/begin\n \n \n# pint ignore/end\nfoo\n"), |
| 104 | }, |
| 105 | { |
| 106 | input: []byte("# pint ignore/begin\nfoo # pint ignore/line\nbar\n# pint ignore/begin"), |
| 107 | output: []byte("# pint ignore/begin\n # pint ignore/line\n \n# pint ignore/begin"), |
| 108 | }, |
| 109 | { |
| 110 | input: []byte("line1\nline2 # pint ignore/line\n"), |
| 111 | output: []byte("line1\n # pint ignore/line\n"), |
| 112 | }, |
| 113 | { |
| 114 | input: []byte("line1\nline2 # pint ignore/line\nline3\n"), |
| 115 | output: []byte("line1\n # pint ignore/line\nline3\n"), |
| 116 | }, |
| 117 | { |
| 118 | input: []byte("{#- comment #} # pint ignore/line\n"), |
| 119 | output: []byte(" #- comment #} # pint ignore/line\n"), |
| 120 | }, |
| 121 | { |
| 122 | input: []byte("# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 123 | output: []byte("# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 124 | }, |
| 125 | { |
| 126 | input: []byte("foo\n# pint ignore/file\nfoo\nbar\n# pint ignore/begin\nfoo\n# pint ignore/end\n"), |
| 127 | output: []byte("foo\n# pint ignore/file\n \n \n# pint ignore/begin\n \n# pint ignore/end\n"), |
| 128 | }, |
| 129 | } |
| 130 | |
| 131 | for i, tc := range testCases { |
| 132 | t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 133 | r := bytes.NewReader(tc.input) |
| 134 | output, err := parser.ReadContent(r) |
| 135 | |
| 136 | hadError := err != nil |
| 137 | if hadError != tc.shouldError { |
| 138 | t.Errorf("ReadContent() returned err=%v, expected=%v", err, tc.shouldError) |
| 139 | return |
| 140 | } |
| 141 | |
| 142 | outputLines := strings.Count(string(output), "\n") |
| 143 | inputLines := strings.Count(string(tc.input), "\n") |
| 144 | if outputLines != inputLines { |
| 145 | t.Errorf("ReadContent() returned %d line(s) while input had %d", outputLines, inputLines) |
| 146 | return |
| 147 | } |
| 148 | |
| 149 | if diff := cmp.Diff(string(tc.output), string(output)); diff != "" { |
| 150 | t.Errorf("ReadContent() returned wrong output (-want +got):\n%s", diff) |
| 151 | return |
| 152 | } |
| 153 | }) |
| 154 | } |
| 155 | } |
| 156 | |