Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 80189ba449 |
+32
-67
@@ -202,6 +202,38 @@ func TestDecodeError_Accessors(t *testing.T) {
|
||||
assert.Equal(t, "bar", e.String())
|
||||
}
|
||||
|
||||
func TestDecodeError_InvalidKeyStartAfterComment(t *testing.T) {
|
||||
// Regression for https://github.com/pelletier/go-toml/issues/1047: the "="
|
||||
// that starts an invalid keyval must be reported on line 2, column 1, with
|
||||
// the human-readable context pointing at that byte (not the document end).
|
||||
doc := "# comment\n= \"value\""
|
||||
|
||||
var v map[string]any
|
||||
err := Unmarshal([]byte(doc), &v)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
|
||||
var derr *DecodeError
|
||||
if !errors.As(err, &derr) {
|
||||
t.Fatalf("expected *DecodeError, got %T", err)
|
||||
}
|
||||
|
||||
row, col := derr.Position()
|
||||
if row != 2 || col != 1 {
|
||||
t.Errorf("Position(): got row %d col %d, want row 2 col 1", row, col)
|
||||
}
|
||||
|
||||
human := derr.String()
|
||||
if !strings.Contains(human, `2| = "value"`) {
|
||||
t.Errorf("human output should show the error line; got:\n%s", human)
|
||||
}
|
||||
// Caret line uses line-number column width padding; only the "| ~" part is stable here.
|
||||
if !strings.Contains(human, "| ~ invalid character at start of key") {
|
||||
t.Errorf("human output should underline '=' and include the parser message; got:\n%s", human)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeError_DuplicateContent(t *testing.T) {
|
||||
// This test verifies that when the same content appears multiple times
|
||||
// in the document, the error correctly points to the actual location
|
||||
@@ -301,73 +333,6 @@ OtherMissing = 1
|
||||
assert.Equal(t, 2, len(strictErr.Unwrap()))
|
||||
}
|
||||
|
||||
func TestDecodeError_PositionAfterComment(t *testing.T) {
|
||||
// Regression test for https://github.com/pelletier/go-toml/issues/1047
|
||||
// Error positions must be correct when the error occurs after comments or
|
||||
// other content that was already scanned past.
|
||||
examples := []struct {
|
||||
desc string
|
||||
doc string
|
||||
expectedRow int
|
||||
expectedCol int
|
||||
expectedStr string
|
||||
}{
|
||||
{
|
||||
desc: "invalid key after comment",
|
||||
doc: "# comment\n= \"value\"",
|
||||
expectedRow: 2,
|
||||
expectedCol: 1,
|
||||
expectedStr: "1| # comment\n2| = \"value\"\n | ~ invalid character at start of key: =",
|
||||
},
|
||||
{
|
||||
desc: "invalid key after two comments",
|
||||
doc: "# one\n# two\n= \"value\"",
|
||||
expectedRow: 3,
|
||||
expectedCol: 1,
|
||||
expectedStr: "1| # one\n2| # two\n3| = \"value\"\n | ~ invalid character at start of key: =",
|
||||
},
|
||||
{
|
||||
desc: "invalid key after key-value pair",
|
||||
doc: "a = 1\n= 2",
|
||||
expectedRow: 2,
|
||||
expectedCol: 1,
|
||||
expectedStr: "1| a = 1\n2| = 2\n | ~ invalid character at start of key: =",
|
||||
},
|
||||
{
|
||||
desc: "invalid key after blank line",
|
||||
doc: "a = 1\n\n= 2",
|
||||
expectedRow: 3,
|
||||
expectedCol: 1,
|
||||
expectedStr: "1| a = 1\n2|\n3| = 2\n | ~ invalid character at start of key: =",
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
var v interface{}
|
||||
err := Unmarshal([]byte(e.doc), &v)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
|
||||
var derr *DecodeError
|
||||
if !errors.As(err, &derr) {
|
||||
t.Fatalf("error not a *DecodeError: %T: %v", err, err)
|
||||
}
|
||||
|
||||
row, col := derr.Position()
|
||||
if row != e.expectedRow {
|
||||
t.Errorf("row: got %d, want %d (error: %s)", row, e.expectedRow, derr.String())
|
||||
}
|
||||
if col != e.expectedCol {
|
||||
t.Errorf("col: got %d, want %d (error: %s)", col, e.expectedCol, derr.String())
|
||||
}
|
||||
|
||||
assert.Equal(t, e.expectedStr, derr.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleDecodeError() {
|
||||
doc := `name = 123__456`
|
||||
|
||||
|
||||
+14
-8
@@ -70,8 +70,8 @@ func (p *Parser) Data() []byte {
|
||||
// panics.
|
||||
func (p *Parser) Range(b []byte) Range {
|
||||
return Range{
|
||||
Offset: uint32(p.subsliceOffset(b)), //nolint:gosec // TOML documents are small
|
||||
Length: uint32(len(b)), //nolint:gosec // TOML documents are small
|
||||
Offset: uint32(subsliceOffset(p.data, b)), //nolint:gosec // TOML documents are small
|
||||
Length: uint32(len(b)), //nolint:gosec // TOML documents are small
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,15 +83,21 @@ func (p *Parser) rangeOfToken(token, rest []byte) Range {
|
||||
return Range{Offset: uint32(offset), Length: uint32(len(token))} //nolint:gosec // TOML documents are small
|
||||
}
|
||||
|
||||
// subsliceOffset returns the byte offset of subslice b within p.data.
|
||||
// b must be a subslice of p.data (sharing the same backing array).
|
||||
func (p *Parser) subsliceOffset(b []byte) int {
|
||||
// subsliceOffset returns the byte offset of subslice b within data.
|
||||
// b must share the same backing array as data (any subslice of data).
|
||||
func subsliceOffset(data, b []byte) int {
|
||||
if len(b) == 0 {
|
||||
return 0
|
||||
}
|
||||
dataPtr := reflect.ValueOf(p.data).Pointer()
|
||||
subPtr := reflect.ValueOf(b).Pointer()
|
||||
return int(subPtr - dataPtr)
|
||||
|
||||
dataPtr := reflect.ValueOf(data).Pointer()
|
||||
bPtr := reflect.ValueOf(b).Pointer()
|
||||
|
||||
offset := int(bPtr - dataPtr)
|
||||
if offset < 0 || offset > len(data) {
|
||||
panic("subslice is not within data")
|
||||
}
|
||||
return offset
|
||||
}
|
||||
|
||||
// Raw returns the slice corresponding to the bytes in the given range.
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package unstable
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Regression test for https://github.com/pelletier/go-toml/issues/1047:
|
||||
// Parser.Range must use the real slice offset, not len(data)-len(slice).
|
||||
func TestParser_Range_HighlightAfterComment(t *testing.T) {
|
||||
input := []byte("# comment\n= \"value\"")
|
||||
|
||||
var p Parser
|
||||
p.Reset(input)
|
||||
for p.NextExpression() {
|
||||
}
|
||||
err := p.Error()
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
|
||||
var perr *ParserError
|
||||
if !errors.As(err, &perr) {
|
||||
t.Fatalf("expected *ParserError, got %T", err)
|
||||
}
|
||||
|
||||
r := p.Range(perr.Highlight)
|
||||
shape := p.Shape(r)
|
||||
|
||||
if r.Offset != 10 {
|
||||
t.Errorf("Range offset: got %d, want 10", r.Offset)
|
||||
}
|
||||
if shape.Start.Line != 2 || shape.Start.Column != 1 {
|
||||
t.Errorf("position: got %d:%d, want 2:1", shape.Start.Line, shape.Start.Column)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user