2021-05-20 18:51:26 +02:00
|
|
|
package mergeconflicts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindConflicts(t *testing.T) {
|
|
|
|
type scenario struct {
|
|
|
|
name string
|
|
|
|
content string
|
|
|
|
expected []*mergeConflict
|
|
|
|
}
|
|
|
|
|
|
|
|
scenarios := []scenario{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
content: "",
|
|
|
|
expected: []*mergeConflict{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "various conflicts",
|
|
|
|
content: `++<<<<<<< HEAD
|
|
|
|
foo
|
|
|
|
++=======
|
|
|
|
bar
|
|
|
|
++>>>>>>> branch
|
|
|
|
|
|
|
|
<<<<<<< HEAD: foo/bar/baz.go
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
=======
|
|
|
|
baz
|
|
|
|
>>>>>>> branch
|
|
|
|
|
|
|
|
++<<<<<<< MERGE_HEAD
|
|
|
|
foo
|
|
|
|
++=======
|
|
|
|
bar
|
|
|
|
++>>>>>>> branch
|
|
|
|
|
|
|
|
++<<<<<<< Updated upstream
|
|
|
|
foo
|
|
|
|
++=======
|
|
|
|
bar
|
|
|
|
++>>>>>>> branch
|
|
|
|
|
|
|
|
++<<<<<<< ours
|
|
|
|
foo
|
|
|
|
++=======
|
|
|
|
bar
|
|
|
|
++>>>>>>> branch
|
2021-05-30 12:42:06 +02:00
|
|
|
|
|
|
|
<<<<<<< Updated upstream: foo/bar/baz.go
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
=======
|
|
|
|
baz
|
|
|
|
>>>>>>> branch
|
2021-08-21 18:03:05 +02:00
|
|
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
foo
|
|
|
|
||||||| fffffff
|
|
|
|
bar
|
|
|
|
=======
|
|
|
|
baz
|
|
|
|
>>>>>>> branch
|
2021-05-20 18:51:26 +02:00
|
|
|
`,
|
|
|
|
expected: []*mergeConflict{
|
|
|
|
{
|
2021-08-21 18:03:05 +02:00
|
|
|
start: 0,
|
|
|
|
ancestor: -1,
|
|
|
|
target: 2,
|
|
|
|
end: 4,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
start: 6,
|
|
|
|
ancestor: -1,
|
|
|
|
target: 9,
|
|
|
|
end: 11,
|
2021-05-20 18:51:26 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-21 18:03:05 +02:00
|
|
|
start: 13,
|
|
|
|
ancestor: -1,
|
|
|
|
target: 15,
|
|
|
|
end: 17,
|
2021-05-20 18:51:26 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-21 18:03:05 +02:00
|
|
|
start: 19,
|
|
|
|
ancestor: -1,
|
|
|
|
target: 21,
|
|
|
|
end: 23,
|
2021-05-20 18:51:26 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-21 18:03:05 +02:00
|
|
|
start: 25,
|
|
|
|
ancestor: -1,
|
|
|
|
target: 27,
|
|
|
|
end: 29,
|
2021-05-20 18:51:26 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-21 18:03:05 +02:00
|
|
|
start: 31,
|
|
|
|
ancestor: -1,
|
|
|
|
target: 34,
|
|
|
|
end: 36,
|
2021-05-20 18:51:26 +02:00
|
|
|
},
|
2021-05-30 12:42:06 +02:00
|
|
|
{
|
2021-08-21 18:03:05 +02:00
|
|
|
start: 38,
|
|
|
|
ancestor: 40,
|
|
|
|
target: 42,
|
|
|
|
end: 44,
|
2021-05-30 12:42:06 +02:00
|
|
|
},
|
2021-05-20 18:51:26 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range scenarios {
|
2021-06-05 05:17:44 +02:00
|
|
|
s := s
|
2021-05-20 18:51:26 +02:00
|
|
|
t.Run(s.name, func(t *testing.T) {
|
|
|
|
assert.EqualValues(t, s.expected, findConflicts(s.content))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|