Example #1
0
    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)
Example #2
0
 def test_optimized_delete(self):
     h = TextHistory()
     h.insert('ABCCDEEE')
     h.delete(2, 2)
     h.delete(2, 3)
     self.assertEqual('ABE', h.text)
     self.assertEqual(2, len(h.get_actions()))
Example #3
0
    def test_insert__bad(self):
        h = TextHistory()
        self.assertEqual(1, h.insert('abc'))

        with self.assertRaises(ValueError):
            h.insert('abc', pos=10)

        with self.assertRaises(ValueError):
            h.insert('abc', pos=-1)
Example #4
0
    def test_optimize_insert(self):
        h = TextHistory()
        h.insert('a')
        h.insert('b')
        h.insert('c')
        actions = h.get_actions()

        self.assertEqual(1, len(actions))
        action, = actions
        self.assertIsInstance(action, InsertAction)
Example #5
0
    def test_optimize_delete(self):
        h = TextHistory()
        h.insert('abcdefg')
        h.delete(2, 2)
        h.delete(2, 1)
        actions = h.get_actions()

        self.assertEqual(2, len(h.get_actions()))
        insert, delete = actions
        self.assertIsInstance(insert, InsertAction)
        self.assertIsInstance(delete, DeleteAction)
Example #6
0
    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)
Example #7
0
    def test_optimise_del(self):
        h = TextHistory()
        h.insert('abcdefgh')
        h.delete(2, 2)
        h.delete(2, 3)

        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_insert_mix_merge(self):
        h = TextHistory()
        h.insert('abcd qwe')
        h.insert('gg', pos=2)
        
        actions = h.get_actions()
        self.assertEqual(1, len(actions))

        v1 = actions[0]
        self.assertEqual('abggcd qwe', v1.text)
        self.assertEqual(0, v1.pos)
        self.assertEqual(2, v1.to_version)
Example #9
0
 def test_get_actions__bad(self):
     h = TextHistory()
     h.insert('a')
     h.insert('b')
     h.insert('c')
     with self.assertRaises(ValueError):
         h.get_actions(0, 10)
     with self.assertRaises(ValueError):
         h.get_actions(10, 10)
     with self.assertRaises(ValueError):
         h.get_actions(2, 1)
     with self.assertRaises(ValueError):
         h.get_actions(-1, 1)
    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_delete_opt(self):
     h = TextHistory()
     h.insert('a1b2c3d4e5f6g7h8')
     self.assertEqual('a1b2c3d4e5f6g7h8', h.text)
     h.delete(pos=3, length=2)
     self.assertEqual('a1b3d4e5f6g7h8', h.text)
     h.delete(pos=3, length=6)
     self.assertEqual('a1b6g7h8', h.text)
     actions = h.get_actions(1)
     self.assertEqual(1, len(actions))
     action = actions[0]
     self.assertIsInstance(action, DeleteAction)
     opt_value = DeleteAction(3, 8, 1, 3)
     self.assertEqual(action.__dict__, opt_value.__dict__)
Example #13
0
    def test_get_actions_insert_delete_optimization(self):
        h = TextHistory()
        h.insert(text="abdc")
        h.delete(pos=1, length=2)

        self.assertEqual(h.text, "ac")

        all_actions = h.get_actions()

        self.assertEqual(len(all_actions), 1)

        insert = all_actions[0]

        self.assertIsInstance(insert, InsertAction)
        self.assertEqual(insert.text, "ac")
    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)
Example #15
0
    def test_insert(self):
        h = TextHistory()

        self.assertEqual(1, h.insert('abc'))
        self.assertEqual('abc', h.text)
        self.assertEqual(1, h.version)

        self.assertEqual(2, h.insert('xyz', pos=2))
        self.assertEqual('abxyzc', h.text)
        self.assertEqual(2, h.version)

        self.assertEqual(3, h.insert('END'))
        self.assertEqual('abxyzcEND', h.text)
        self.assertEqual(3, h.version)

        self.assertEqual(4, h.insert('BEGIN', pos=0))
        self.assertEqual('BEGINabxyzcEND', h.text)
        self.assertEqual(4, h.version)
Example #16
0
    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)
Example #17
0
    def test_get_optimized_actions__merge_delete(self):
        h = TextHistory()
        h.insert('0123456789')
        h.delete(3, 2)
        h.delete(3, 3)
        h.delete(2, 1)

        self.assertEqual('0189', h.text)

        actions = h.get_actions(optimize=True)

        self.assertEqual(2, len(actions))
        insert, delete = actions
        self.assertIsInstance(insert, InsertAction)
        self.assertIsInstance(delete, DeleteAction)
        self.assertEqual(4, delete.to_version)
        self.assertEqual(1, delete.from_version)
        self.assertEqual(2, delete.pos)
        self.assertEqual(6, delete.length)
Example #18
0
    def test_delete(self):
        h = TextHistory()
        self.assertEqual(1, h.insert('abc xyz'))

        self.assertEqual(2, h.delete(pos=1, length=2))
        self.assertEqual('a xyz', h.text)
        self.assertEqual(2, h.version)

        self.assertEqual(3, h.delete(pos=3, length=0))
        self.assertEqual('a xyz', h.text)
        self.assertEqual(3, h.version)
Example #19
0
    def test_delete__bad(self):
        h = TextHistory()
        self.assertEqual(1, h.insert('abc'))

        with self.assertRaises(ValueError):
            h.delete(pos=10, length=2)

        with self.assertRaises(ValueError):
            h.delete(pos=1, length=3)

        with self.assertRaises(ValueError):
            h.delete(pos=-1, length=2)
    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_insert_opt(self):
     h = TextHistory()
     h.insert('a' * 10)
     h.insert('x', pos=3)
     h.insert('yz', pos=4)
     self.assertEqual('aaaxyzaaaaaaa', h.text)
     actions = h.get_actions(1)
     self.assertEqual(1, len(actions))
     action = actions[0]
     self.assertIsInstance(action, InsertAction)
     opt_value = InsertAction(3, 'xyz', 1, 3)
     self.assertEqual(action.__dict__, opt_value.__dict__)
    def test_insert_merge(self):
        h = TextHistory()
        h.insert('abcd qwe')
        h.insert('gg')
        h.insert('pop')
        
        actions = h.get_actions()
        self.assertEqual(1, len(actions))

        v1 = actions[0]
        self.assertEqual('abcd qweggpop', v1.text)
        self.assertEqual(0, v1.pos)
        self.assertEqual(3, v1.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)
Example #24
0
    def test_optimized_insert(self):
        h = TextHistory()
        h.insert('AB')
        self.assertEqual('AB', h.text)
        self.assertEqual(1, h.version)

        h.insert('CC', pos=2)
        self.assertEqual('ABCC', h.text)
        self.assertEqual(2, h.version)

        h.insert('D', pos=4)
        self.assertEqual('ABCCD', h.text)
        self.assertEqual(3, h.version)
        self.assertEqual(1, len(h.get_actions()))
Example #25
0
    def test_get_optimized_actions__merge_insert(self):
        h = TextHistory()
        h.insert('abc')
        h.insert('BEGIN', 0)
        h.insert('END')

        self.assertEqual('BEGINabcEND', h.text)

        actions = h.get_actions(optimize=True)

        self.assertEqual(1, len(actions))
        insert = actions[0]
        self.assertIsInstance(insert, InsertAction)
        self.assertEqual(3, insert.to_version)
        self.assertEqual(0, insert.from_version)
        self.assertEqual(0, insert.pos)
Example #26
0
    def test_get_actions_insert_optimization(self):
        h = TextHistory()
        h.insert(text="abc")
        h.insert(text="d")
        h.delete(pos=0, length=2)
        h.insert(text="abdc")
        h.insert(text="d")

        all_actions = h.get_actions()

        self.assertEqual(3, len(all_actions))

        insert1, delete, insert2 = all_actions

        self.assertIsInstance(insert1, InsertAction)
        self.assertIsInstance(delete, DeleteAction)
        self.assertIsInstance(insert2, InsertAction)

        self.assertEqual(insert1.text, "abcd")
        self.assertEqual(insert2.text, "abdcd")
        self.assertEqual(insert1.pos, 0)
        self.assertEqual(insert2.pos, 2)

        self.assertEqual(h.text, "cdabdcd")
    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_delete(self):
     h = TextHistory()
     h.insert('abcdefgh')
     h.delete(pos=1, length=2)
     h.delete(pos=1, length=1)
Example #29
0
    def test_get_actions__empty(self):
        h = TextHistory()
        self.assertEqual([], h.get_actions())

        h.insert('a')
        self.assertEqual([], h.get_actions(0, 0))