1package diff_test
2
3import (
4 "testing"
5
6 "gopkg.in/src-d/go-git.v4/utils/diff"
7
8 "github.com/sergi/go-diff/diffmatchpatch"
9 . "gopkg.in/check.v1"
10)
11
12func Test(t *testing.T) { TestingT(t) }
13
14type suiteCommon struct{}
15
16var _ = Suite(&suiteCommon{})
17
18var diffTests = [...]struct {
19 src string // the src string to diff
20 dst string // the dst string to diff
21}{
22 // equal inputs
23 {"", ""},
24 {"a", "a"},
25 {"a\n", "a\n"},
26 {"a\nb", "a\nb"},
27 {"a\nb\n", "a\nb\n"},
28 {"a\nb\nc", "a\nb\nc"},
29 {"a\nb\nc\n", "a\nb\nc\n"},
30 // missing '\n'
31 {"", "\n"},
32 {"\n", ""},
33 {"a", "a\n"},
34 {"a\n", "a"},
35 {"a\nb", "a\nb"},
36 {"a\nb\n", "a\nb\n"},
37 {"a\nb\nc", "a\nb\nc"},
38 {"a\nb\nc\n", "a\nb\nc\n"},
39 // generic
40 {"a\nbbbbb\n\tccc\ndd\n\tfffffffff\n", "bbbbb\n\tccc\n\tDD\n\tffff\n"},
41}
42
43func (s *suiteCommon) TestAll(c *C) {
44 for i, t := range diffTests {
45 diffs := diff.Do(t.src, t.dst)
46 src := diff.Src(diffs)
47 dst := diff.Dst(diffs)
48 c.Assert(src, Equals, t.src, Commentf("subtest %d, src=%q, dst=%q, bad calculated src", i, t.src, t.dst))
49 c.Assert(dst, Equals, t.dst, Commentf("subtest %d, src=%q, dst=%q, bad calculated dst", i, t.src, t.dst))
50 }
51}
52
53var doTests = [...]struct {
54 src, dst string
55 exp []diffmatchpatch.Diff
56}{
57 {
58 src: "",
59 dst: "",
60 exp: []diffmatchpatch.Diff{},
61 },
62 {
63 src: "a",
64 dst: "a",
65 exp: []diffmatchpatch.Diff{
66 {
67 Type: 0,
68 Text: "a",
69 },
70 },
71 },
72 {
73 src: "",
74 dst: "abc\ncba",
75 exp: []diffmatchpatch.Diff{
76 {
77 Type: 1,
78 Text: "abc\ncba",
79 },
80 },
81 },
82 {
83 src: "abc\ncba",
84 dst: "",
85 exp: []diffmatchpatch.Diff{
86 {
87 Type: -1,
88 Text: "abc\ncba",
89 },
90 },
91 },
92 {
93 src: "abc\nbcd\ncde",
94 dst: "000\nabc\n111\nBCD\n",
95 exp: []diffmatchpatch.Diff{
96 {Type: 1, Text: "000\n"},
97 {Type: 0, Text: "abc\n"},
98 {Type: -1, Text: "bcd\ncde"},
99 {Type: 1, Text: "111\nBCD\n"},
100 },
101 },
102}
103
104func (s *suiteCommon) TestDo(c *C) {
105 for i, t := range doTests {
106 diffs := diff.Do(t.src, t.dst)
107 c.Assert(diffs, DeepEquals, t.exp, Commentf("subtest %d", i))
108 }
109}