TestDecodeErrorRedefinition checks that errors raised by the duplicate-key tracker are reported as DecodeError, carrying the key path and a position pointing at the offending key (see issue #668).
(t *testing.T)
| 422 | // tracker are reported as DecodeError, carrying the key path and a position |
| 423 | // pointing at the offending key (see issue #668). |
| 424 | func TestDecodeErrorRedefinition(t *testing.T) { |
| 425 | examples := []struct { |
| 426 | desc string |
| 427 | doc string |
| 428 | msg string |
| 429 | key Key |
| 430 | row int |
| 431 | col int |
| 432 | human string |
| 433 | }{ |
| 434 | { |
| 435 | desc: "duplicate key", |
| 436 | doc: "a = 1\nb = 2\nb = 3\n", |
| 437 | msg: "toml: key b is already defined", |
| 438 | key: Key{"b"}, |
| 439 | row: 3, |
| 440 | col: 1, |
| 441 | human: ` |
| 442 | 1| a = 1 |
| 443 | 2| b = 2 |
| 444 | 3| b = 3 |
| 445 | | ~ key b is already defined`, |
| 446 | }, |
| 447 | { |
| 448 | desc: "duplicate dotted key", |
| 449 | doc: "foo.bar = 1\nfoo.bar = 2\n", |
| 450 | msg: "toml: key bar is already defined", |
| 451 | key: Key{"foo", "bar"}, |
| 452 | row: 2, |
| 453 | col: 1, |
| 454 | human: ` |
| 455 | 1| foo.bar = 1 |
| 456 | 2| foo.bar = 2 |
| 457 | | ~~~~~~~ key bar is already defined`, |
| 458 | }, |
| 459 | { |
| 460 | desc: "redefined table", |
| 461 | doc: "[a]\nx = 1\n[a]\ny = 2\n", |
| 462 | msg: "toml: table a already exists", |
| 463 | key: Key{"a"}, |
| 464 | row: 3, |
| 465 | col: 2, |
| 466 | }, |
| 467 | { |
| 468 | desc: "duplicate key in table body", |
| 469 | doc: "[a]\nx = 1\nx = 2\n", |
| 470 | msg: "toml: key x is already defined", |
| 471 | key: Key{"x"}, |
| 472 | row: 3, |
| 473 | col: 1, |
| 474 | }, |
| 475 | { |
| 476 | desc: "redefined nested table", |
| 477 | doc: "[a.b]\n[a.b]\n", |
| 478 | msg: "toml: table b already exists", |
| 479 | key: Key{"a", "b"}, |
| 480 | row: 2, |
| 481 | col: 2, |