1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#!/usr/bin/env python
# encoding: utf-8
# pylint: skip-file
import unittest
from diff import diff, guess_edit
from position import Position
def transform(a, cmds):
buf = a.split("\n")
for cmd in cmds:
ctype, line, col, char = cmd
if ctype == "D":
if char != "\n":
buf[line] = buf[line][:col] + buf[line][col + len(char) :]
else:
buf[line] = buf[line] + buf[line + 1]
del buf[line + 1]
elif ctype == "I":
buf[line] = buf[line][:col] + char + buf[line][col:]
buf = "\n".join(buf).split("\n")
return "\n".join(buf)
import unittest
class _BaseGuessing(object):
def runTest(self):
rv, es = guess_edit(
self.initial_line, self.a, self.b, Position(*self.ppos), Position(*self.pos)
)
self.assertEqual(rv, True)
self.assertEqual(self.wanted, es)
class TestGuessing_Noop0(_BaseGuessing, unittest.TestCase):
a, b = [], []
initial_line = 0
ppos, pos = (0, 6), (0, 7)
wanted = ()
class TestGuessing_InsertOneChar(_BaseGuessing, unittest.TestCase):
a, b = ["Hello World"], ["Hello World"]
initial_line = 0
ppos, pos = (0, 6), (0, 7)
wanted = (("I", 0, 6, " "),)
class TestGuessing_InsertOneChar1(_BaseGuessing, unittest.TestCase):
a, b = ["Hello World"], ["Hello World"]
initial_line = 0
ppos, pos = (0, 7), (0, 8)
wanted = (("I", 0, 7, " "),)
class TestGuessing_BackspaceOneChar(_BaseGuessing, unittest.TestCase):
a, b = ["Hello World"], ["Hello World"]
initial_line = 0
ppos, pos = (0, 7), (0, 6)
wanted = (("D", 0, 6, " "),)
class TestGuessing_DeleteOneChar(_BaseGuessing, unittest.TestCase):
a, b = ["Hello World"], ["Hello World"]
initial_line = 0
ppos, pos = (0, 5), (0, 5)
wanted = (("D", 0, 5, " "),)
class _Base(object):
def runTest(self):
es = diff(self.a, self.b)
tr = transform(self.a, es)
self.assertEqual(self.b, tr)
self.assertEqual(self.wanted, es)
class TestEmptyString(_Base, unittest.TestCase):
a, b = "", ""
wanted = ()
class TestAllMatch(_Base, unittest.TestCase):
a, b = "abcdef", "abcdef"
wanted = ()
class TestLotsaNewlines(_Base, unittest.TestCase):
a, b = "Hello", "Hello\nWorld\nWorld\nWorld"
wanted = (
("I", 0, 5, "\n"),
("I", 1, 0, "World"),
("I", 1, 5, "\n"),
("I", 2, 0, "World"),
("I", 2, 5, "\n"),
("I", 3, 0, "World"),
)
class TestCrash(_Base, unittest.TestCase):
a = "hallo Blah mitte=sdfdsfsd\nhallo kjsdhfjksdhfkjhsdfkh mittekjshdkfhkhsdfdsf"
b = "hallo Blah mitte=sdfdsfsd\nhallo b mittekjshdkfhkhsdfdsf"
wanted = (("D", 1, 6, "kjsdhfjksdhfkjhsdfkh"), ("I", 1, 6, "b"))
class TestRealLife(_Base, unittest.TestCase):
a = "hallo End Beginning"
b = "hallo End t"
wanted = (("D", 0, 10, "Beginning"), ("I", 0, 10, "t"))
class TestRealLife1(_Base, unittest.TestCase):
a = "Vorne hallo Hinten"
b = "Vorne hallo Hinten"
wanted = (("I", 0, 11, " "),)
class TestWithNewline(_Base, unittest.TestCase):
a = "First Line\nSecond Line"
b = "n"
wanted = (
("D", 0, 0, "First Line"),
("D", 0, 0, "\n"),
("D", 0, 0, "Second Line"),
("I", 0, 0, "n"),
)
class TestCheapDelete(_Base, unittest.TestCase):
a = "Vorne hallo Hinten"
b = "Vorne Hinten"
wanted = (("D", 0, 5, " hallo"),)
class TestNoSubstring(_Base, unittest.TestCase):
a, b = "abc", "def"
wanted = (("D", 0, 0, "abc"), ("I", 0, 0, "def"))
class TestCommonCharacters(_Base, unittest.TestCase):
a, b = "hasomelongertextbl", "hol"
wanted = (("D", 0, 1, "asomelongertextb"), ("I", 0, 1, "o"))
class TestUltiSnipsProblem(_Base, unittest.TestCase):
a = "this is it this is it this is it"
b = "this is it a this is it"
wanted = (("D", 0, 11, "this is it"), ("I", 0, 11, "a"))
class MatchIsTooCheap(_Base, unittest.TestCase):
a = "stdin.h"
b = "s"
wanted = (("D", 0, 1, "tdin.h"),)
class MultiLine(_Base, unittest.TestCase):
a = "hi first line\nsecond line first line\nsecond line world"
b = "hi first line\nsecond line k world"
wanted = (
("D", 1, 12, "first line"),
("D", 1, 12, "\n"),
("D", 1, 12, "second line"),
("I", 1, 12, "k"),
)
if __name__ == "__main__":
unittest.main()
# k = TestEditScript()
# unittest.TextTestRunner().run(k)
|