fork of go-git with some jj specific features
at v5.8.0 3.2 kB view raw
1package diff_test 2 3import ( 4 "testing" 5 6 "github.com/go-git/go-git/v5/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 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", 104 dst: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ", 105 exp: []diffmatchpatch.Diff{ 106 {Type: -1, Text: "A\n"}, 107 {Type: 0, Text: "B\nC\nD\nE\nF\nG\n"}, 108 {Type: -1, Text: "H\n"}, 109 {Type: 0, Text: "I\nJ\nK\nL\nM\nN\n"}, 110 {Type: -1, Text: "Ñ\n"}, 111 {Type: 0, Text: "O\nP\nQ\nR\nS\nT\n"}, 112 {Type: -1, Text: "U\n"}, 113 {Type: 0, Text: "V\nW\nX\nY\nZ"}, 114 }, 115 }, 116 { 117 src: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ", 118 dst: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\n", 119 exp: []diffmatchpatch.Diff{ 120 {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"}, 121 {Type: -1, Text: "Z"}, 122 }, 123 }, 124 { 125 src: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY\nZ", 126 dst: "B\nC\nD\nE\nF\nG\nI\nJ\nK\nL\nM\nN\nO\nP\nQ\nR\nS\nT\nV\nW\nX\nY", 127 exp: []diffmatchpatch.Diff{ 128 {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"}, 129 {Type: -1, Text: "Y\nZ"}, 130 {Type: 1, Text: "Y"}, 131 }, 132 }, 133} 134 135func (s *suiteCommon) TestDo(c *C) { 136 for i, t := range doTests { 137 diffs := diff.Do(t.src, t.dst) 138 c.Assert(diffs, DeepEquals, t.exp, Commentf("subtest %d", i)) 139 } 140}