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.
13 var readTests = []struct {
17 UseFieldsPerRecord bool // false (default) means FieldsPerRecord is -1
19 // These fields are copied into the Reader
28 Line int // Expected error line if != 0
29 Column int // Expected error column if line != 0
34 Output: [][]string{{"a", "b", "c"}},
38 Input: "a,b\r\nc,d\r\n",
39 Output: [][]string{{"a", "b"}, {"c", "d"}},
43 Input: "a,b\rc,d\r\n",
44 Output: [][]string{{"a", "b\rc", "d"}},
48 UseFieldsPerRecord: true,
49 Input: `#field1,field2,field3
56 {"#field1", "field2", "field3"},
57 {"aaa", "bb\nb", "ccc"},
58 {"a,a", `b"bb`, "ccc"},
59 {"zzz", "yyy", "xxx"},
65 Output: [][]string{{"a", "b", "c"}},
71 Output: [][]string{{"a", "b", "c"}},
76 line","one line","three
79 Output: [][]string{{"two\nline", "one line", "three\nline\nfield"}},
83 Input: "a,b,c\n\nd,e,f\n\n",
92 TrimLeadingSpace: true,
93 Output: [][]string{{"a", "b", "c"}},
98 Output: [][]string{{" a", " b", " c"}},
103 Input: "#1,2,3\na,b,c\n#comment",
104 Output: [][]string{{"a", "b", "c"}},
108 Input: "#1,2,3\na,b,c",
109 Output: [][]string{{"#1", "2", "3"}, {"a", "b", "c"}},
114 Input: `a "word","1"2",a","b`,
115 Output: [][]string{{`a "word"`, `1"2`, `a"`, `b`}},
120 Input: `a "word","1"2",a"`,
121 Output: [][]string{{`a "word"`, `1"2`, `a"`}},
124 Name: "BareDoubleQuotes",
127 Output: [][]string{{`a""b`, `c`}},
130 Name: "BadDoubleQuotes",
132 Error: `bare " in non-quoted-field`, Line: 1, Column: 1,
136 Input: ` "a"," b",c`,
137 TrimLeadingSpace: true,
138 Output: [][]string{{"a", " b", "c"}},
141 Name: "BadBareQuote",
142 Input: `a "word","b"`,
143 Error: `bare " in non-quoted-field`, Line: 1, Column: 2,
146 Name: "BadTrailingQuote",
147 Input: `"a word",b"`,
148 Error: `bare " in non-quoted-field`, Line: 1, Column: 10,
151 Name: "ExtraneousQuote",
152 Input: `"a "word","b"`,
153 Error: `extraneous " in field`, Line: 1, Column: 3,
156 Name: "BadFieldCount",
157 UseFieldsPerRecord: true,
159 Error: "wrong number of fields", Line: 2,
162 Name: "BadFieldCount1",
163 UseFieldsPerRecord: true,
166 Error: "wrong number of fields", Line: 1,
171 Output: [][]string{{"a", "b", "c"}, {"d", "e"}},
174 Name: "BadTrailingCommaEOF",
176 Error: "extra delimiter at end of line", Line: 1, Column: 5,
179 Name: "BadTrailingCommaEOL",
181 Error: "extra delimiter at end of line", Line: 1, Column: 5,
184 Name: "BadTrailingCommaSpaceEOF",
185 TrimLeadingSpace: true,
187 Error: "extra delimiter at end of line", Line: 1, Column: 5,
190 Name: "BadTrailingCommaSpaceEOL",
191 TrimLeadingSpace: true,
193 Error: "extra delimiter at end of line", Line: 1, Column: 5,
196 Name: "BadTrailingCommaLine3",
197 TrimLeadingSpace: true,
198 Input: "a,b,c\nd,e,f\ng,hi,",
199 Error: "extra delimiter at end of line", Line: 3, Column: 4,
202 Name: "NotTrailingComma3",
204 Output: [][]string{{"a", "b", "c", " "}},
207 Name: "CommaFieldTest",
221 {"x", "y", "z", "w"},
226 {"x", "y", "z", "w"},
236 TrimLeadingSpace: true,
237 Input: "a,b,\nc,d,e",
245 TrailingComma: false,
246 TrimLeadingSpace: true,
247 Input: "a,b,\nc,d,e",
248 Error: "extra delimiter at end of line",
252 func TestRead(t *testing.T) {
253 for _, tt := range readTests {
254 r := NewReader(strings.NewReader(tt.Input))
255 r.Comment = tt.Comment
256 if tt.UseFieldsPerRecord {
257 r.FieldsPerRecord = tt.FieldsPerRecord
259 r.FieldsPerRecord = -1
261 r.LazyQuotes = tt.LazyQuotes
262 r.TrailingComma = tt.TrailingComma
263 r.TrimLeadingSpace = tt.TrimLeadingSpace
267 out, err := r.ReadAll()
268 perr, _ := err.(*ParseError)
270 if err == nil || !strings.Contains(err.String(), tt.Error) {
271 t.Errorf("%s: error %v, want error %q", tt.Name, err, tt.Error)
272 } else if tt.Line != 0 && (tt.Line != perr.Line || tt.Column != perr.Column) {
273 t.Errorf("%s: error at %d:%d expected %d:%d", tt.Name, perr.Line, perr.Column, tt.Line, tt.Column)
275 } else if err != nil {
276 t.Errorf("%s: unexpected error %v", tt.Name, err)
277 } else if !reflect.DeepEqual(out, tt.Output) {
278 t.Errorf("%s: out=%q want %q", tt.Name, out, tt.Output)