def test_get_actions(self): h = TextHistory() h.insert('a') h.insert('bc') h.replace('B', pos=1) h.delete(pos=0, length=1) self.assertEqual('Bc', h.text) actions = h.get_actions(1) self.assertEqual(3, len(actions)) insert, replace, delete = actions self.assertIsInstance(insert, InsertAction) self.assertIsInstance(replace, ReplaceAction) self.assertIsInstance(delete, DeleteAction) # insert self.assertEqual(1, insert.from_version) self.assertEqual(2, insert.to_version) self.assertEqual('bc', insert.text) self.assertEqual(1, insert.pos) # replace self.assertEqual(2, replace.from_version) self.assertEqual(3, replace.to_version) self.assertEqual('B', replace.text) self.assertEqual(1, replace.pos) # delete self.assertEqual(3, delete.from_version) self.assertEqual(4, delete.to_version) self.assertEqual(0, delete.pos) self.assertEqual(1, delete.length)
def test_replace__bad(self): h = TextHistory() self.assertEqual(1, h.insert('abc')) with self.assertRaises(ValueError): h.replace('abc', pos=10) with self.assertRaises(ValueError): h.replace('abc', pos=-1)
def test_optimise_replace(self): h = TextHistory() h.insert('abcdefgh') h.replace('abc', 0) h.replace('woa', 0) actions = h.get_actions() self.assertEqual(2, len(actions)) th = TextHistory() for act in actions: th.action(act) self.assertEqual(h.text, th.text)
def test_add_long_replace(self): h = TextHistory() h.insert('abcdef') h.replace('xyz', pos=2) h.replace('QRP', pos=4) actions = h.get_actions() self.assertEqual(2, len(actions)) v2 = actions[1] self.assertEqual('xyQRP', v2.text) self.assertEqual(2, v2.pos) self.assertEqual(3, v2.to_version)
def test_trivial_replace(self): h = TextHistory() h.insert('abcdef') h.replace('A', pos=0) h.replace('B', pos=1) actions = h.get_actions() self.assertEqual(2, len(actions)) v2 = actions[1] self.assertEqual('AB', v2.text) self.assertEqual(0, v2.pos) self.assertEqual(3, v2.to_version)
def test_insert_example(self): h = TextHistory() h.insert('abc qwe') h.replace('A', pos=0) h.insert('xy', pos=2) h.insert('z', pos=4) actions = h.get_actions() self.assertEqual(3, len(actions)) v3 = actions[2] self.assertEqual('xyz', v3.text) self.assertEqual(2, v3.pos) self.assertEqual(4, v3.to_version)
def test_get_actions__optimyze_2(self): h = TextHistory() h.insert('a') h.insert('b', 0) h.replace("abc") actions = h.get_actions() self.assertEqual(2, len(actions)) insert, replace = actions self.assertIsInstance(insert, InsertAction) self.assertIsInstance(replace, ReplaceAction) self.assertEqual(0, insert.from_version) self.assertEqual(2, insert.to_version) self.assertEqual("b", insert.text)
def test_replace(self): h = TextHistory() self.assertEqual(1, h.replace('abc')) self.assertEqual('abc', h.text) self.assertEqual(1, h.version) self.assertEqual(2, h.replace('xyz', pos=2)) self.assertEqual('abxyz', h.text) self.assertEqual(2, h.version) self.assertEqual(3, h.replace('X', pos=2)) self.assertEqual('abXyz', h.text) self.assertEqual(3, h.version) self.assertEqual(4, h.replace('END')) self.assertEqual('abXyzEND', h.text) self.assertEqual(4, h.version)
def test_insert_trivial(self): h = TextHistory() h.insert('abcd qwe') h.replace('A', pos=0) h.insert('gg', pos=2) h.insert('pop', pos=7) #pdb.set_trace() actions = h.get_actions(1) self.assertEqual(3, len(actions)) v1, v2, v3 = actions self.assertEqual('A', v1.text) self.assertEqual(0, v1.pos) self.assertEqual(2, v1.to_version) self.assertEqual('gg', v2.text) self.assertEqual(2, v2.pos) self.assertEqual(3, v2.to_version) self.assertEqual('pop', v3.text) self.assertEqual(7, v3.pos) self.assertEqual(4, v3.to_version)
def test_get_actions__optimyze_1(self): h = TextHistory() h.insert('a') h.insert('b') h.delete(0, 1) h.delete(0, 1) h.replace("abc") actions = h.get_actions() self.assertEqual(3, len(actions)) insert, delete, replace = actions self.assertIsInstance(insert, InsertAction) self.assertIsInstance(delete, DeleteAction) self.assertIsInstance(replace, ReplaceAction) # insert self.assertEqual(0, insert.from_version) self.assertEqual(2, insert.to_version) self.assertEqual("ab", insert.text) #delete self.assertEqual(2, delete.from_version) self.assertEqual(4, delete.to_version) self.assertEqual(2, delete.length)
def test_get_optimized_actions__merge_replace(self): h = TextHistory() h.insert('01234567891011') h.replace('aaa', 6) h.replace('LEFT', 2) h.replace('RIGHT', 9) self.assertEqual('01LEFTaaaRIGHT', h.text) actions = h.get_actions(optimize=True) self.assertEqual(2, len(actions)) insert, replace = actions self.assertIsInstance(insert, InsertAction) self.assertIsInstance(replace, ReplaceAction) self.assertEqual(1, replace.from_version) self.assertEqual(4, replace.to_version) self.assertEqual(2, replace.pos)