JOJ3/internal/parser/diff/parser_test.go
张泊明518370910136 43bfbba327
All checks were successful
submodules sync / sync (push) Successful in 47s
build / build (push) Successful in 2m41s
build / trigger-build-image (push) Successful in 12s
feat(parser/diff): limit max diff output in the end
2025-07-16 23:03:17 -07:00

68 lines
2.1 KiB
Go

package diff
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestGenerateDiffComment(t *testing.T) {
d := &Diff{}
t.Run("HideCommonPrefix with common prefix", func(t *testing.T) {
answerStr := "line1\nline2\nline3\nline4"
resultStr := "line1\nline2\nlineA\nlineB"
output := Output{
HideCommonPrefix: true,
}
comment := d.generateDiffComment(answerStr, resultStr, output)
expected := "```diff\n(2 line(s) of common prefix hidden)\n\n- line3\n- line4\n+ lineA\n+ lineB\n```\n"
assert.Equal(t, expected, comment)
})
t.Run("HideCommonPrefix with no common prefix", func(t *testing.T) {
answerStr := "line1\nline2"
resultStr := "lineA\nlineB"
output := Output{
HideCommonPrefix: true,
}
comment := d.generateDiffComment(answerStr, resultStr, output)
expected := "```diff\n- line1\n- line2\n+ lineA\n+ lineB\n```\n"
assert.Equal(t, expected, comment)
})
t.Run("HideCommonPrefix with identical content", func(t *testing.T) {
answerStr := "line1\nline2"
resultStr := "line1\nline2"
output := Output{
HideCommonPrefix: true,
}
comment := d.generateDiffComment(answerStr, resultStr, output)
expected := "```diff\n(2 line(s) of common prefix hidden)\n\n\n```\n"
assert.Equal(t, expected, comment)
})
t.Run("HideCommonPrefix with only common prefix", func(t *testing.T) {
answerStr := "line1\nline2"
resultStr := "line1\nline2\nlineA"
output := Output{
HideCommonPrefix: true,
}
comment := d.generateDiffComment(answerStr, resultStr, output)
expected := "```diff\n(2 line(s) of common prefix hidden)\n\n+ lineA\n```\n"
assert.Equal(t, expected, comment)
})
t.Run("MaxDiffLines truncation", func(t *testing.T) {
answerStr := "line1\nline2\nline3\nline4\nline5\nline6\nline7\nline8"
resultStr := "line1\nline2\nline3\nlineA\nlineB\nlineC\nlineD\nlineE"
output := Output{
MaxDiffLines: 3,
HideCommonPrefix: true,
}
comment := d.generateDiffComment(answerStr, resultStr, output)
expected := "```diff\n(3 line(s) of common prefix hidden)\n\n- line4\n- line5\n- line6\n+ lineA\n+ lineB\n+ lineC\n\n(truncated)\n```\n"
assert.Equal(t, expected, comment)
})
}