Move regression tests to public Unmarshal API with full error string assertions
Remove the unstable package test and consolidate all test cases into TestDecodeError_PositionAfterComment, which exercises toml.Unmarshal and validates the complete human-readable error output including context lines and tilde markers. https://claude.ai/code/session_01EXYfFXc3DDGpQ27sWdXTKq
This commit is contained in:
+6
-8
@@ -310,35 +310,35 @@ func TestDecodeError_PositionAfterComment(t *testing.T) {
|
|||||||
doc string
|
doc string
|
||||||
expectedRow int
|
expectedRow int
|
||||||
expectedCol int
|
expectedCol int
|
||||||
errContains string
|
expectedStr string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "invalid key after comment",
|
desc: "invalid key after comment",
|
||||||
doc: "# comment\n= \"value\"",
|
doc: "# comment\n= \"value\"",
|
||||||
expectedRow: 2,
|
expectedRow: 2,
|
||||||
expectedCol: 1,
|
expectedCol: 1,
|
||||||
errContains: "invalid character",
|
expectedStr: "1| # comment\n2| = \"value\"\n | ~ invalid character at start of key: =",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "invalid key after two comments",
|
desc: "invalid key after two comments",
|
||||||
doc: "# one\n# two\n= \"value\"",
|
doc: "# one\n# two\n= \"value\"",
|
||||||
expectedRow: 3,
|
expectedRow: 3,
|
||||||
expectedCol: 1,
|
expectedCol: 1,
|
||||||
errContains: "invalid character",
|
expectedStr: "1| # one\n2| # two\n3| = \"value\"\n | ~ invalid character at start of key: =",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "invalid key after key-value pair",
|
desc: "invalid key after key-value pair",
|
||||||
doc: "a = 1\n= 2",
|
doc: "a = 1\n= 2",
|
||||||
expectedRow: 2,
|
expectedRow: 2,
|
||||||
expectedCol: 1,
|
expectedCol: 1,
|
||||||
errContains: "invalid character",
|
expectedStr: "1| a = 1\n2| = 2\n | ~ invalid character at start of key: =",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "invalid key after blank line",
|
desc: "invalid key after blank line",
|
||||||
doc: "a = 1\n\n= 2",
|
doc: "a = 1\n\n= 2",
|
||||||
expectedRow: 3,
|
expectedRow: 3,
|
||||||
expectedCol: 1,
|
expectedCol: 1,
|
||||||
errContains: "invalid character",
|
expectedStr: "1| a = 1\n2|\n3| = 2\n | ~ invalid character at start of key: =",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,9 +363,7 @@ func TestDecodeError_PositionAfterComment(t *testing.T) {
|
|||||||
t.Errorf("col: got %d, want %d (error: %s)", col, e.expectedCol, derr.String())
|
t.Errorf("col: got %d, want %d (error: %s)", col, e.expectedCol, derr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.Contains(derr.Error(), e.errContains) {
|
assert.Equal(t, e.expectedStr, derr.String())
|
||||||
t.Errorf("error %q does not contain %q", derr.Error(), e.errContains)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -695,81 +695,3 @@ func ExampleParser() {
|
|||||||
// Expression: KeyValue
|
// Expression: KeyValue
|
||||||
// value -> (Integer) 42
|
// value -> (Integer) 42
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParserError_RangeOffset(t *testing.T) {
|
|
||||||
// Regression test for https://github.com/pelletier/go-toml/issues/1047
|
|
||||||
// Parser.Range must return the correct byte offset for error highlights,
|
|
||||||
// not just for suffix slices.
|
|
||||||
examples := []struct {
|
|
||||||
desc string
|
|
||||||
input string
|
|
||||||
wantOffset int
|
|
||||||
wantLine int
|
|
||||||
wantColumn int
|
|
||||||
wantMessage string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
desc: "invalid key start after comment",
|
|
||||||
input: "# comment\n= \"value\"",
|
|
||||||
wantOffset: 10,
|
|
||||||
wantLine: 2,
|
|
||||||
wantColumn: 1,
|
|
||||||
wantMessage: "invalid character at start of key: =",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "invalid key start after two comments",
|
|
||||||
input: "# one\n# two\n= \"value\"",
|
|
||||||
wantOffset: 12,
|
|
||||||
wantLine: 3,
|
|
||||||
wantColumn: 1,
|
|
||||||
wantMessage: "invalid character at start of key: =",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "invalid key start after blank line",
|
|
||||||
input: "a = 1\n\n= 2",
|
|
||||||
wantOffset: 7,
|
|
||||||
wantLine: 3,
|
|
||||||
wantColumn: 1,
|
|
||||||
wantMessage: "invalid character at start of key: =",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
desc: "invalid key start after valid key-value",
|
|
||||||
input: "a = 1\n= 2",
|
|
||||||
wantOffset: 6,
|
|
||||||
wantLine: 2,
|
|
||||||
wantColumn: 1,
|
|
||||||
wantMessage: "invalid character at start of key: =",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, e := range examples {
|
|
||||||
t.Run(e.desc, func(t *testing.T) {
|
|
||||||
p := Parser{}
|
|
||||||
p.Reset([]byte(e.input))
|
|
||||||
for p.NextExpression() {
|
|
||||||
}
|
|
||||||
err := p.Error()
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("expected an error")
|
|
||||||
}
|
|
||||||
perr, ok := err.(*ParserError)
|
|
||||||
if !ok {
|
|
||||||
t.Fatalf("expected *ParserError, got %T", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, e.wantMessage, perr.Message)
|
|
||||||
|
|
||||||
r := p.Range(perr.Highlight)
|
|
||||||
if int(r.Offset) != e.wantOffset {
|
|
||||||
t.Errorf("Range offset: got %d, want %d", r.Offset, e.wantOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
shape := p.Shape(r)
|
|
||||||
if shape.Start.Line != e.wantLine || shape.Start.Column != e.wantColumn {
|
|
||||||
t.Errorf("position: got %d:%d, want %d:%d",
|
|
||||||
shape.Start.Line, shape.Start.Column,
|
|
||||||
e.wantLine, e.wantColumn)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user