1package diff_test
2
3import (
4 "fmt"
5 "testing"
6
7 "github.com/go-git/go-git/v5/utils/diff"
8 "github.com/stretchr/testify/suite"
9
10 "github.com/sergi/go-diff/diffmatchpatch"
11)
12
13type suiteCommon struct {
14 suite.Suite
15}
16
17func TestSuiteCommon(t *testing.T) {
18 suite.Run(t, new(suiteCommon))
19}
20
21var diffTests = [...]struct {
22 src string // the src string to diff
23 dst string // the dst string to diff
24}{
25 // equal inputs
26 {"", ""},
27 {"a", "a"},
28 {"a\n", "a\n"},
29 {"a\nb", "a\nb"},
30 {"a\nb\n", "a\nb\n"},
31 {"a\nb\nc", "a\nb\nc"},
32 {"a\nb\nc\n", "a\nb\nc\n"},
33 // missing '\n'
34 {"", "\n"},
35 {"\n", ""},
36 {"a", "a\n"},
37 {"a\n", "a"},
38 {"a\nb", "a\nb"},
39 {"a\nb\n", "a\nb\n"},
40 {"a\nb\nc", "a\nb\nc"},
41 {"a\nb\nc\n", "a\nb\nc\n"},
42 // generic
43 {"a\nbbbbb\n\tccc\ndd\n\tfffffffff\n", "bbbbb\n\tccc\n\tDD\n\tffff\n"},
44}
45
46func (s *suiteCommon) TestAll() {
47 for i, t := range diffTests {
48 diffs := diff.Do(t.src, t.dst)
49 src := diff.Src(diffs)
50 dst := diff.Dst(diffs)
51 s.Equal(t.src, src, fmt.Sprintf("subtest %d, src=%q, dst=%q, bad calculated src", i, t.src, t.dst))
52 s.Equal(t.dst, dst, fmt.Sprintf("subtest %d, src=%q, dst=%q, bad calculated dst", i, t.src, t.dst))
53 }
54}
55
56var doTests = [...]struct {
57 src, dst string
58 exp []diffmatchpatch.Diff
59}{
60 {
61 src: "",
62 dst: "",
63 exp: []diffmatchpatch.Diff{},
64 },
65 {
66 src: "a",
67 dst: "a",
68 exp: []diffmatchpatch.Diff{
69 {
70 Type: 0,
71 Text: "a",
72 },
73 },
74 },
75 {
76 src: "",
77 dst: "abc\ncba",
78 exp: []diffmatchpatch.Diff{
79 {
80 Type: 1,
81 Text: "abc\ncba",
82 },
83 },
84 },
85 {
86 src: "abc\ncba",
87 dst: "",
88 exp: []diffmatchpatch.Diff{
89 {
90 Type: -1,
91 Text: "abc\ncba",
92 },
93 },
94 },
95 {
96 src: "abc\nbcd\ncde",
97 dst: "000\nabc\n111\nBCD\n",
98 exp: []diffmatchpatch.Diff{
99 {Type: 1, Text: "000\n"},
100 {Type: 0, Text: "abc\n"},
101 {Type: -1, Text: "bcd\ncde"},
102 {Type: 1, Text: "111\nBCD\n"},
103 },
104 },
105 {
106 src: "A\nB\nC\nD\nE\nF\nG\nH\nI\nJ\nK\nL\nM\nN\nÑ\nO\nP\nQ\nR\nS\nT\nU\nV\nW\nX\nY\nZ",
107 dst: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
108 exp: []diffmatchpatch.Diff{
109 {Type: -1, Text: "A\n"},
110 {Type: 0, Text: "B\nC\nD\nE\nF\nG\n"},
111 {Type: -1, Text: "H\n"},
112 {Type: 0, Text: "I\nJ\nK\nL\nM\nN\n"},
113 {Type: -1, Text: "Ñ\n"},
114 {Type: 0, Text: "O\nP\nQ\nR\nS\nT\n"},
115 {Type: -1, Text: "U\n"},
116 {Type: 0, Text: "V\nW\nX\nY\nZ"},
117 },
118 },
119 {
120 src: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
121 dst: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\n",
122 exp: []diffmatchpatch.Diff{
123 {Type: 0, Text: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\n"},
124 {Type: -1, Text: "Z"},
125 },
126 },
127 {
128 src: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ",
129 dst: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY",
130 exp: []diffmatchpatch.Diff{
131 {Type: 0, Text: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\n"},
132 {Type: -1, Text: "Y\nZ"},
133 {Type: 1, Text: "Y"},
134 },
135 },
136}
137
138func (s *suiteCommon) TestDo() {
139 for i, t := range doTests {
140 diffs := diff.Do(t.src, t.dst)
141 s.Equal(t.exp, diffs, fmt.Sprintf("subtest %d", i))
142 }
143}