Add test for string escape bug and implement fix

Co-authored-by: mikefarah <1151925+mikefarah@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-20 04:41:44 +00:00 committed by Mike Farah
parent b7cbe59fd7
commit 7f60daad20
2 changed files with 74 additions and 3 deletions

View File

@ -614,9 +614,14 @@ func (te *tomlEncoder) colorizeToml(input []byte) []byte {
if ch == '"' || ch == '\'' { if ch == '"' || ch == '\'' {
quote := ch quote := ch
end := i + 1 end := i + 1
for end < len(toml) && toml[end] != quote { for end < len(toml) {
if toml[end] == '\\' { if toml[end] == quote {
end++ // skip escaped char break
}
if toml[end] == '\\' && end+1 < len(toml) {
// Skip the backslash and the escaped character
end += 2
continue
} }
end++ end++
} }

View File

@ -796,3 +796,69 @@ func TestTomlColorisationNumberBug(t *testing.T) {
}) })
} }
} }
// TestTomlStringEscapeColorization tests that string colorization correctly
// handles escape sequences, particularly escaped quotes at the end of strings
func TestTomlStringEscapeColorization(t *testing.T) {
// Save and restore color state
oldNoColor := color.NoColor
color.NoColor = false
defer func() { color.NoColor = oldNoColor }()
encoder := NewTomlEncoder()
tomlEncoder := encoder.(*tomlEncoder)
testCases := []struct {
name string
input string
description string
}{
{
name: "escaped quote at end",
input: `A = "test\""` + "\n",
description: "String ending with escaped quote should be colorized correctly",
},
{
name: "escaped backslash then quote",
input: `A = "test\\\""` + "\n",
description: "String with escaped backslash followed by escaped quote",
},
{
name: "escaped quote in middle",
input: `A = "test\"middle"` + "\n",
description: "String with escaped quote in the middle should be colorized correctly",
},
{
name: "multiple escaped quotes",
input: `A = "\"test\""` + "\n",
description: "String with escaped quotes at start and end",
},
{
name: "escaped newline",
input: `A = "test\n"` + "\n",
description: "String with escaped newline should be colorized correctly",
},
{
name: "single quote with escaped single quote",
input: `A = 'test\''` + "\n",
description: "Single-quoted string with escaped single quote",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
// The test should not panic and should return some output
result := tomlEncoder.colorizeToml([]byte(tt.input))
if len(result) == 0 {
t.Error("Expected non-empty colorized output")
}
// Check that the result contains the input string (with color codes)
// At minimum, it should contain "A" and "="
resultStr := string(result)
if !strings.Contains(resultStr, "A") || !strings.Contains(resultStr, "=") {
t.Errorf("Expected output to contain 'A' and '=', got: %q", resultStr)
}
})
}
}