| 302 | } |
| 303 | |
| 304 | func TestDecodeError_PositionAfterComments(t *testing.T) { |
| 305 | examples := []struct { |
| 306 | name string |
| 307 | doc string |
| 308 | expectedRow int |
| 309 | expectedCol int |
| 310 | }{ |
| 311 | { |
| 312 | name: "comment then invalid key start", |
| 313 | doc: "# comment\n= \"value\"", |
| 314 | expectedRow: 2, |
| 315 | expectedCol: 1, |
| 316 | }, |
| 317 | { |
| 318 | name: "no comment invalid key start", |
| 319 | doc: "= \"value\"", |
| 320 | expectedRow: 1, |
| 321 | expectedCol: 1, |
| 322 | }, |
| 323 | { |
| 324 | name: "multiple comments then error", |
| 325 | doc: "# c1\n# c2\n= \"val\"", |
| 326 | expectedRow: 3, |
| 327 | expectedCol: 1, |
| 328 | }, |
| 329 | { |
| 330 | name: "valid line then invalid key start", |
| 331 | doc: "a = 1\n= \"val\"", |
| 332 | expectedRow: 2, |
| 333 | expectedCol: 1, |
| 334 | }, |
| 335 | { |
| 336 | name: "blank lines then error", |
| 337 | doc: "\n\n= \"val\"", |
| 338 | expectedRow: 3, |
| 339 | expectedCol: 1, |
| 340 | }, |
| 341 | { |
| 342 | name: "expected newline but got invalid char", |
| 343 | doc: "a = 1 b = 2", |
| 344 | expectedRow: 1, |
| 345 | expectedCol: 7, |
| 346 | }, |
| 347 | } |
| 348 | |
| 349 | for _, e := range examples { |
| 350 | t.Run(e.name, func(t *testing.T) { |
| 351 | var v interface{} |
| 352 | err := Unmarshal([]byte(e.doc), &v) |
| 353 | |
| 354 | var derr *DecodeError |
| 355 | if !errors.As(err, &derr) { |
| 356 | t.Fatal("error not in expected format") |
| 357 | } |
| 358 | |
| 359 | row, col := derr.Position() |
| 360 | if row != e.expectedRow { |
| 361 | t.Errorf("row: got %d, want %d", row, e.expectedRow) |