JOJ3/internal/parser/diff/diff_test.go
张泊明518370910136 b8c233b1bb
All checks were successful
submodules sync / sync (push) Successful in 43s
build / build (push) Successful in 1m32s
build / trigger-build-image (push) Successful in 8s
test(parser/diff): add myers diff test
2024-11-16 03:07:12 -05:00

70 lines
1.7 KiB
Go

package diff
import (
"reflect"
"testing"
)
func TestMyersDiff(t *testing.T) {
tests := []struct {
name string
src []string
dst []string
compareSpace bool
expected []operation
}{
{
name: "Insert operation",
src: []string{"a", "b"},
dst: []string{"a", "b", "c"},
compareSpace: true,
expected: []operation{MOVE, MOVE, INSERT},
},
{
name: "Delete operation",
src: []string{"a", "b", "c"},
dst: []string{"a", "b"},
compareSpace: true,
expected: []operation{MOVE, MOVE, DELETE},
},
{
name: "No changes",
src: []string{"a", "b", "c"},
dst: []string{"a", "b", "c"},
compareSpace: true,
expected: []operation{MOVE, MOVE, MOVE},
},
{
name: "Move operation",
src: []string{"a", "b", "c"},
dst: []string{"c", "a", "b"},
compareSpace: true,
expected: []operation{INSERT, MOVE, MOVE, DELETE},
},
{
name: "Ignore whitespace differences",
src: []string{"a ", "b"},
dst: []string{"a", "b"},
compareSpace: false,
expected: []operation{MOVE, MOVE},
},
{
name: "Consider whitespace differences",
src: []string{"a ", "b"},
dst: []string{"a", "b"},
compareSpace: true,
expected: []operation{DELETE, INSERT, MOVE},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := myersDiff(test.src, test.dst, test.compareSpace)
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("myersDiff(%v, %v, %v) = %v; want %v",
test.src, test.dst, test.compareSpace, result, test.expected)
}
})
}
}