rope / ropetest / historytest.py

  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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import unittest

import rope.base.history
from rope.base import exceptions
from rope.base.change import *
from ropetest import testutils


class HistoryTest(unittest.TestCase):

    def setUp(self):
        super(HistoryTest, self).setUp()
        self.project = testutils.sample_project()
        self.history = self.project.history

    def tearDown(self):
        testutils.remove_project(self.project)
        super(HistoryTest, self).tearDown()

    def test_undoing_writes(self):
        my_file = self.project.root.create_file('my_file.txt')
        my_file.write('text1')
        self.history.undo()
        self.assertEquals('', my_file.read())

    def test_moving_files(self):
        my_file = self.project.root.create_file('my_file.txt')
        my_file.move('new_file.txt')
        self.history.undo()
        self.assertEquals('', my_file.read())

    def test_moving_files_to_folders(self):
        my_file = self.project.root.create_file('my_file.txt')
        my_folder = self.project.root.create_folder('my_folder')
        my_file.move(my_folder.path)
        self.history.undo()
        self.assertEquals('', my_file.read())

    def test_writing_files_that_does_not_change_contents(self):
        my_file = self.project.root.create_file('my_file.txt')
        my_file.write('')
        self.project.history.undo()
        self.assertFalse(my_file.exists())

class IsolatedHistoryTest(unittest.TestCase):

    def setUp(self):
        super(IsolatedHistoryTest, self).setUp()
        self.project = testutils.sample_project()
        self.history = rope.base.history.History(self.project)
        self.file1 = self.project.root.create_file('file1.txt')
        self.file2 = self.project.root.create_file('file2.txt')

    def tearDown(self):
        testutils.remove_project(self.project)
        super(IsolatedHistoryTest, self).tearDown()

    def test_simple_undo(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.assertEquals('1', self.file1.read())
        self.history.undo()
        self.assertEquals('', self.file1.read())

    def test_tobe_undone(self):
        change1 = ChangeContents(self.file1, '1')
        self.assertEquals(None, self.history.tobe_undone)
        self.history.do(change1)
        self.assertEquals(change1, self.history.tobe_undone)
        change2 = ChangeContents(self.file1, '2')
        self.history.do(change2)
        self.assertEquals(change2, self.history.tobe_undone)
        self.history.undo()
        self.assertEquals(change1, self.history.tobe_undone)

    def test_tobe_redone(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.assertEquals(None, self.history.tobe_redone)
        self.history.undo()
        self.assertEquals(change, self.history.tobe_redone)

    @testutils.assert_raises(exceptions.HistoryError)
    def test_undo_limit(self):
        history = rope.base.history.History(self.project, maxundos=1)
        history.do(ChangeContents(self.file1, '1'))
        history.do(ChangeContents(self.file1, '2'))
        try:
            history.undo()
            history.undo()
        finally:
            self.assertEquals('1', self.file1.read())

    def test_simple_redo(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.history.undo()
        self.history.redo()
        self.assertEquals('1', self.file1.read())

    def test_simple_re_undo(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.history.undo()
        self.history.redo()
        self.history.undo()
        self.assertEquals('', self.file1.read())

    def test_multiple_undos(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        change = ChangeContents(self.file1, '2')
        self.history.do(change)
        self.history.undo()
        self.assertEquals('1', self.file1.read())
        change = ChangeContents(self.file1, '3')
        self.history.do(change)
        self.history.undo()
        self.assertEquals('1', self.file1.read())
        self.history.redo()
        self.assertEquals('3', self.file1.read())

    @testutils.assert_raises(exceptions.HistoryError)
    def test_undo_list_underflow(self):
        self.history.undo()

    @testutils.assert_raises(exceptions.HistoryError)
    def test_redo_list_underflow(self):
        self.history.redo()

    @testutils.assert_raises(exceptions.HistoryError)
    def test_dropping_undone_changes(self):
        self.file1.write('1')
        self.history.undo(drop=True)
        self.history.redo()

    def test_undoing_choosen_changes(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.history.undo(change)
        self.assertEquals('', self.file1.read())
        self.assertFalse(self.history.undo_list)

    def test_undoing_choosen_changes2(self):
        change1 = ChangeContents(self.file1, '1')
        self.history.do(change1)
        self.history.do(ChangeContents(self.file1, '2'))
        self.history.undo(change1)
        self.assertEquals('', self.file1.read())
        self.assertFalse(self.history.undo_list)

    def test_undoing_choosen_changes_not_undoing_others(self):
        change1 = ChangeContents(self.file1, '1')
        self.history.do(change1)
        self.history.do(ChangeContents(self.file2, '2'))
        self.history.undo(change1)
        self.assertEquals('', self.file1.read())
        self.assertEquals('2', self.file2.read())

    def test_undoing_writing_after_moving(self):
        change1 = ChangeContents(self.file1, '1')
        self.history.do(change1)
        self.history.do(MoveResource(self.file1, 'file3.txt'))
        file3 = self.project.get_resource('file3.txt')
        self.history.undo(change1)
        self.assertEquals('', self.file1.read())
        self.assertFalse(file3.exists())

    def test_undoing_folder_movements_for_undoing_writes_inside_it(self):
        folder = self.project.root.create_folder('folder')
        file3 = folder.create_file('file3.txt')
        change1 = ChangeContents(file3, '1')
        self.history.do(change1)
        self.history.do(MoveResource(folder, 'new_folder'))
        new_folder = self.project.get_resource('new_folder')
        self.history.undo(change1)
        self.assertEquals('', file3.read())
        self.assertFalse(new_folder.exists())

    def test_undoing_changes_that_depend_on_a_dependant_change(self):
        change1 = ChangeContents(self.file1, '1')
        self.history.do(change1)
        changes = ChangeSet('2nd change')
        changes.add_change(ChangeContents(self.file1, '2'))
        changes.add_change(ChangeContents(self.file2, '2'))
        self.history.do(changes)
        self.history.do(MoveResource(self.file2, 'file3.txt'))
        file3 = self.project.get_resource('file3.txt')

        self.history.undo(change1)
        self.assertEquals('', self.file1.read())
        self.assertEquals('', self.file2.read())
        self.assertFalse(file3.exists())

    def test_undoing_writes_for_undoing_folder_movements_containing_it(self):
        folder = self.project.root.create_folder('folder')
        old_file = folder.create_file('file3.txt')
        change1 = MoveResource(folder, 'new_folder')
        self.history.do(change1)
        new_file = self.project.get_resource('new_folder/file3.txt')
        self.history.do(ChangeContents(new_file, '1'))
        self.history.undo(change1)
        self.assertEquals('', old_file.read())
        self.assertFalse(new_file.exists())

    @testutils.assert_raises(exceptions.HistoryError)
    def test_undoing_not_available_change(self):
        change = ChangeContents(self.file1, '1')
        self.history.undo(change)

    def test_ignoring_ignored_resources(self):
        self.project.set('ignored_resources', ['ignored*'])
        ignored = self.project.get_file('ignored.txt')
        change = CreateResource(ignored)
        self.history.do(change)
        self.assertTrue(ignored.exists())
        self.assertEquals(0, len(self.history.undo_list))

    def test_get_file_undo_list_simple(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.assertEquals(set([change]),
                          set(self.history.get_file_undo_list(self.file1)))

    def test_get_file_undo_list_for_moves(self):
        change = MoveResource(self.file1, 'file2.txt')
        self.history.do(change)
        self.assertEquals(set([change]),
                          set(self.history.get_file_undo_list(self.file1)))

    # XXX: What happens for moves before the file is created?
    def xxx_test_get_file_undo_list_and_moving_its_contining_folder(self):
        folder = self.project.root.create_folder('folder')
        old_file = folder.create_file('file3.txt')
        change1 = MoveResource(folder, 'new_folder')
        self.history.do(change1)
        self.assertEquals(set([change1]),
                          set(self.history.get_file_undo_list(old_file)))

    def test_clearing_redo_list_after_do(self):
        change = ChangeContents(self.file1, '1')
        self.history.do(change)
        self.history.undo()
        self.history.do(change)
        self.assertEquals(0, len(self.history.redo_list))

    @testutils.assert_raises(exceptions.HistoryError)
    def test_undoing_a_not_yet_performed_change(self):
        change = ChangeContents(self.file1, '1')
        str(change)
        change.undo()

    def test_clearing_up_the_history(self):
        change1 = ChangeContents(self.file1, '1')
        change2 = ChangeContents(self.file1, '2')
        self.history.do(change1)
        self.history.do(change2)
        self.history.undo()
        self.history.clear()
        self.assertEquals(0, len(self.history.undo_list))
        self.assertEquals(0, len(self.history.redo_list))

    def test_redoing_choosen_changes_not_undoing_others(self):
        change1 = ChangeContents(self.file1, '1')
        change2 = ChangeContents(self.file2, '2')
        self.history.do(change1)
        self.history.do(change2)
        self.history.undo()
        self.history.undo()
        redone = self.history.redo(change2)
        self.assertEquals([change2], redone)
        self.assertEquals('', self.file1.read())
        self.assertEquals('2', self.file2.read())


class SavingHistoryTest(unittest.TestCase):

    def setUp(self):
        super(SavingHistoryTest, self).setUp()
        self.project = testutils.sample_project()
        self.history = rope.base.history.History(self.project)
        self.to_data = ChangeToData()
        self.to_change = DataToChange(self.project)

    def tearDown(self):
        testutils.remove_project(self.project)
        super(SavingHistoryTest, self).tearDown()

    def test_simple_set_saving(self):
        data = self.to_data(ChangeSet('testing'))
        change = self.to_change(data)
        self.assertEquals('testing', str(change))

    def test_simple_change_content_saving(self):
        myfile = self.project.get_file('myfile.txt')
        myfile.create()
        myfile.write('1')
        data = self.to_data(ChangeContents(myfile, '2'))
        change = self.to_change(data)
        self.history.do(change)
        self.assertEquals('2', myfile.read())
        self.history.undo()
        self.assertEquals('1', change.old_contents)

    def test_move_resource_saving(self):
        myfile = self.project.root.create_file('myfile.txt')
        myfolder = self.project.root.create_folder('myfolder')
        data = self.to_data(MoveResource(myfile, 'myfolder'))
        change = self.to_change(data)
        self.history.do(change)
        self.assertFalse(myfile.exists())
        self.assertTrue(myfolder.has_child('myfile.txt'))
        self.history.undo()
        self.assertTrue(myfile.exists())
        self.assertFalse(myfolder.has_child('myfile.txt'))

    def test_move_resource_saving_for_folders(self):
        myfolder = self.project.root.create_folder('myfolder')
        newfolder = self.project.get_folder('newfolder')
        change = MoveResource(myfolder, 'newfolder')
        self.history.do(change)

        data = self.to_data(change)
        change = self.to_change(data)
        change.undo()
        self.assertTrue(myfolder.exists())
        self.assertFalse(newfolder.exists())

    def test_create_file_saving(self):
        myfile = self.project.get_file('myfile.txt')
        data = self.to_data(CreateFile(self.project.root, 'myfile.txt'))
        change = self.to_change(data)
        self.history.do(change)
        self.assertTrue(myfile.exists())
        self.history.undo()
        self.assertFalse(myfile.exists())

    def test_create_folder_saving(self):
        myfolder = self.project.get_folder('myfolder')
        data = self.to_data(CreateFolder(self.project.root, 'myfolder'))
        change = self.to_change(data)
        self.history.do(change)
        self.assertTrue(myfolder.exists())
        self.history.undo()
        self.assertFalse(myfolder.exists())

    def test_create_resource_saving(self):
        myfile = self.project.get_file('myfile.txt')
        data = self.to_data(CreateResource(myfile))
        change = self.to_change(data)
        self.history.do(change)
        self.assertTrue(myfile.exists())
        self.history.undo()
        self.assertFalse(myfile.exists())

    def test_remove_resource_saving(self):
        myfile = self.project.root.create_file('myfile.txt')
        data = self.to_data(RemoveResource(myfile))
        change = self.to_change(data)
        self.history.do(change)
        self.assertFalse(myfile.exists())

    def test_change_set_saving(self):
        change = ChangeSet('testing')
        myfile = self.project.get_file('myfile.txt')
        change.add_change(CreateResource(myfile))
        change.add_change(ChangeContents(myfile, '1'))

        data = self.to_data(change)
        change = self.to_change(data)
        self.history.do(change)
        self.assertEquals('1', myfile.read())
        self.history.undo()
        self.assertFalse(myfile.exists())

    def test_writing_and_reading_history(self):
        history_file = self.project.get_file('history.pickle')
        self.project.set('save_history', True)
        history = rope.base.history.History(self.project)
        myfile = self.project.get_file('myfile.txt')
        history.do(CreateResource(myfile))
        history.write()

        history = rope.base.history.History(self.project)
        history.undo()
        self.assertFalse(myfile.exists())

    def test_writing_and_reading_history2(self):
        history_file = self.project.get_file('history.pickle')
        self.project.set('save_history', True)
        history = rope.base.history.History(self.project)
        myfile = self.project.get_file('myfile.txt')
        history.do(CreateResource(myfile))
        history.undo()
        history.write()

        history = rope.base.history.History(self.project)
        history.redo()
        self.assertTrue(myfile.exists())

def suite():
    result = unittest.TestSuite()
    result.addTests(unittest.makeSuite(HistoryTest))
    result.addTests(unittest.makeSuite(IsolatedHistoryTest))
    result.addTests(unittest.makeSuite(SavingHistoryTest))
    return result

if __name__ == '__main__':
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.