LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2011 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 package csv |
| 6 |
| 7 import ( |
| 8 "reflect" |
| 9 "strings" |
| 10 "testing" |
| 11 ) |
| 12 |
| 13 var readTests = []struct { |
| 14 Name string |
| 15 Input string |
| 16 Output [][]string |
| 17 UseFieldsPerRecord bool // false (default) means FieldsPerRecord is -1 |
| 18 |
| 19 // These fields are copied into the Reader |
| 20 Comma int |
| 21 Comment int |
| 22 FieldsPerRecord int |
| 23 LazyQuotes bool |
| 24 TrailingComma bool |
| 25 TrimLeadingSpace bool |
| 26 |
| 27 Error string |
| 28 Line int // Expected error line if != 0 |
| 29 Column int // Expected error column if line != 0 |
| 30 }{ |
| 31 { |
| 32 Name: "Simple", |
| 33 Input: "a,b,c\n", |
| 34 Output: [][]string{{"a", "b", "c"}}, |
| 35 }, |
| 36 { |
| 37 Name: "CRLF", |
| 38 Input: "a,b\r\nc,d\r\n", |
| 39 Output: [][]string{{"a", "b"}, {"c", "d"}}, |
| 40 }, |
| 41 { |
| 42 Name: "BareCR", |
| 43 Input: "a,b\rc,d\r\n", |
| 44 Output: [][]string{{"a", "b\rc", "d"}}, |
| 45 }, |
| 46 { |
| 47 Name: "RFC4180test", |
| 48 UseFieldsPerRecord: true, |
| 49 Input: `#field1,field2,field3 |
| 50 "aaa","bb |
| 51 b","ccc" |
| 52 "a,a","b""bb","ccc" |
| 53 zzz,yyy,xxx |
| 54 `, |
| 55 Output: [][]string{ |
| 56 {"#field1", "field2", "field3"}, |
| 57 {"aaa", "bb\nb", "ccc"}, |
| 58 {"a,a", `b"bb`, "ccc"}, |
| 59 {"zzz", "yyy", "xxx"}, |
| 60 }, |
| 61 }, |
| 62 { |
| 63 Name: "NoEOLTest", |
| 64 Input: "a,b,c", |
| 65 Output: [][]string{{"a", "b", "c"}}, |
| 66 }, |
| 67 { |
| 68 Name: "Semicolon", |
| 69 Comma: ';', |
| 70 Input: "a;b;c\n", |
| 71 Output: [][]string{{"a", "b", "c"}}, |
| 72 }, |
| 73 { |
| 74 Name: "MultiLine", |
| 75 Input: `"two |
| 76 line","one line","three |
| 77 line |
| 78 field"`, |
| 79 Output: [][]string{{"two\nline", "one line", "three\nline\nfield
"}}, |
| 80 }, |
| 81 { |
| 82 Name: "BlankLine", |
| 83 Input: "a,b,c\n\nd,e,f\n\n", |
| 84 Output: [][]string{ |
| 85 {"a", "b", "c"}, |
| 86 {"d", "e", "f"}, |
| 87 }, |
| 88 }, |
| 89 { |
| 90 Name: "TrimSpace", |
| 91 Input: " a, b, c\n", |
| 92 TrimLeadingSpace: true, |
| 93 Output: [][]string{{"a", "b", "c"}}, |
| 94 }, |
| 95 { |
| 96 Name: "LeadingSpace", |
| 97 Input: " a, b, c\n", |
| 98 Output: [][]string{{" a", " b", " c"}}, |
| 99 }, |
| 100 { |
| 101 Name: "Comment", |
| 102 Comment: '#', |
| 103 Input: "#1,2,3\na,b,c\n#comment", |
| 104 Output: [][]string{{"a", "b", "c"}}, |
| 105 }, |
| 106 { |
| 107 Name: "NoComment", |
| 108 Input: "#1,2,3\na,b,c", |
| 109 Output: [][]string{{"#1", "2", "3"}, {"a", "b", "c"}}, |
| 110 }, |
| 111 { |
| 112 Name: "LazyQuotes", |
| 113 LazyQuotes: true, |
| 114 Input: `a "word","1"2",a","b`, |
| 115 Output: [][]string{{`a "word"`, `1"2`, `a"`, `b`}}, |
| 116 }, |
| 117 { |
| 118 Name: "BareQuotes", |
| 119 LazyQuotes: true, |
| 120 Input: `a "word","1"2",a"`, |
| 121 Output: [][]string{{`a "word"`, `1"2`, `a"`}}, |
| 122 }, |
| 123 { |
| 124 Name: "BareDoubleQuotes", |
| 125 LazyQuotes: true, |
| 126 Input: `a""b,c`, |
| 127 Output: [][]string{{`a""b`, `c`}}, |
| 128 }, |
| 129 { |
| 130 Name: "BadDoubleQuotes", |
| 131 Input: `a""b,c`, |
| 132 Output: [][]string{{`a""b`, `c`}}, |
| 133 Error: `bare " in non-quoted-field`, Line: 1, Column: 1, |
| 134 }, |
| 135 { |
| 136 Name: "TrimQuote", |
| 137 Input: ` "a"," b",c`, |
| 138 TrimLeadingSpace: true, |
| 139 Output: [][]string{{"a", " b", "c"}}, |
| 140 }, |
| 141 { |
| 142 Name: "BadBareQuote", |
| 143 Input: `a "word","b"`, |
| 144 Error: `bare " in non-quoted-field`, Line: 1, Column: 2, |
| 145 }, |
| 146 { |
| 147 Name: "BadTrailingQuote", |
| 148 Input: `"a word",b"`, |
| 149 Error: `bare " in non-quoted-field`, Line: 1, Column: 10, |
| 150 }, |
| 151 { |
| 152 Name: "ExtraneousQuote", |
| 153 Input: `"a "word","b"`, |
| 154 Error: `extraneous " in field`, Line: 1, Column: 3, |
| 155 }, |
| 156 { |
| 157 Name: "BadFieldCount", |
| 158 UseFieldsPerRecord: true, |
| 159 Input: "a,b,c\nd,e", |
| 160 Error: "wrong number of fields", Line: 2, |
| 161 }, |
| 162 { |
| 163 Name: "BadFieldCount1", |
| 164 UseFieldsPerRecord: true, |
| 165 FieldsPerRecord: 2, |
| 166 Input: `a,b,c`, |
| 167 Error: "wrong number of fields", Line: 1, |
| 168 }, |
| 169 { |
| 170 Name: "FieldCount", |
| 171 Input: "a,b,c\nd,e", |
| 172 Output: [][]string{{"a", "b", "c"}, {"d", "e"}}, |
| 173 }, |
| 174 { |
| 175 Name: "BadTrailingCommaEOF", |
| 176 Input: "a,b,c,", |
| 177 Error: "extra delimiter at end of line", Line: 1, Column: 5, |
| 178 }, |
| 179 { |
| 180 Name: "BadTrailingCommaEOL", |
| 181 Input: "a,b,c,\n", |
| 182 Error: "extra delimiter at end of line", Line: 1, Column: 5, |
| 183 }, |
| 184 { |
| 185 Name: "BadTrailingCommaSpaceEOF", |
| 186 TrimLeadingSpace: true, |
| 187 Input: "a,b,c, ", |
| 188 Error: "extra delimiter at end of line", Line: 1, Col
umn: 5, |
| 189 }, |
| 190 { |
| 191 Name: "BadTrailingCommaSpaceEOL", |
| 192 TrimLeadingSpace: true, |
| 193 Input: "a,b,c, \n", |
| 194 Error: "extra delimiter at end of line", Line: 1, Col
umn: 5, |
| 195 }, |
| 196 { |
| 197 Name: "BadTrailingCommaLine3", |
| 198 TrimLeadingSpace: true, |
| 199 Input: "a,b,c\nd,e,f\ng,hi,", |
| 200 Error: "extra delimiter at end of line", Line: 3, Col
umn: 4, |
| 201 }, |
| 202 { |
| 203 Name: "NotTrailingComma3", |
| 204 Input: "a,b,c, \n", |
| 205 Output: [][]string{{"a", "b", "c", " "}}, |
| 206 }, |
| 207 { |
| 208 Name: "CommaFieldTest", |
| 209 TrailingComma: true, |
| 210 Input: `x,y,z,w |
| 211 x,y,z, |
| 212 x,y,, |
| 213 x,,, |
| 214 ,,, |
| 215 "x","y","z","w" |
| 216 "x","y","z","" |
| 217 "x","y","","" |
| 218 "x","","","" |
| 219 "","","","" |
| 220 `, |
| 221 Output: [][]string{ |
| 222 {"x", "y", "z", "w"}, |
| 223 {"x", "y", "z", ""}, |
| 224 {"x", "y", "", ""}, |
| 225 {"x", "", "", ""}, |
| 226 {"", "", "", ""}, |
| 227 {"x", "y", "z", "w"}, |
| 228 {"x", "y", "z", ""}, |
| 229 {"x", "y", "", ""}, |
| 230 {"x", "", "", ""}, |
| 231 {"", "", "", ""}, |
| 232 }, |
| 233 }, |
| 234 } |
| 235 |
| 236 func TestRead(t *testing.T) { |
| 237 for _, tt := range readTests { |
| 238 r := NewReader(strings.NewReader(tt.Input)) |
| 239 r.Comment = tt.Comment |
| 240 if tt.UseFieldsPerRecord { |
| 241 r.FieldsPerRecord = tt.FieldsPerRecord |
| 242 } else { |
| 243 r.FieldsPerRecord = -1 |
| 244 } |
| 245 r.LazyQuotes = tt.LazyQuotes |
| 246 r.TrailingComma = tt.TrailingComma |
| 247 r.TrimLeadingSpace = tt.TrimLeadingSpace |
| 248 if tt.Comma != 0 { |
| 249 r.Comma = tt.Comma |
| 250 } |
| 251 out, err := r.ReadAll() |
| 252 perr, _ := err.(*ParseError) |
| 253 if tt.Error != "" { |
| 254 if err == nil || !strings.Contains(err.String(), tt.Erro
r) { |
| 255 t.Errorf("%s: error %v, want error %q", tt.Name,
err, tt.Error) |
| 256 } else if tt.Line != 0 && (tt.Line != perr.Line || tt.Co
lumn != perr.Column) { |
| 257 t.Errorf("%s: error at %d:%d expected %d:%d", tt
.Name, perr.Line, perr.Column, tt.Line, tt.Column) |
| 258 } |
| 259 } else if err != nil { |
| 260 t.Errorf("%s: unexpected error %v", tt.Name, err) |
| 261 } else if !reflect.DeepEqual(out, tt.Output) { |
| 262 t.Errorf("%s: out=%q want %q", tt.Name, out, tt.Output) |
| 263 } |
| 264 } |
| 265 } |
LEFT | RIGHT |