rope_py3k / ropeidetest / editortest.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import unittest
import Tkinter

from ropeidetest.mockeditortest import (GraphicalEditorFactory,
                                        get_sample_editingcontext)
from ropeide.indenter import PythonCodeIndenter
from ropeide import editor


class GraphicalEditorTest(unittest.TestCase):
    """This class only tests features that are specific to GraphicalEditor;
    see mockeditortest
    """

    __factory = GraphicalEditorFactory(Tkinter.Frame())

    def setUp(self):
        unittest.TestCase.setUp(self)
        self.editor = self.__factory.create(get_sample_editingcontext())
        self.editor.set_text('sample text')

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def test_move_next_word(self):
        self.editor.next_word()
        self.assertEquals(' ', self.editor.get(), 'Expected <%c> but was <%c>' % (' ', self.editor.get()))

    def test_move_next_word_on_spaces(self):
        self.editor.next_word()
        self.editor.insert(self.editor.get_end(), ' and\n')
        self.editor.next_word()
        self.assertEquals(' ', self.editor.get())
        self.editor.next_word()
        self.assertEquals('\n', self.editor.get())

    def test_next_word_on_end(self):
        self.editor.set_insert(self.editor.get_end())
        self.editor.next_word()
        self.assertEquals(self.editor.get_end(), self.editor.get_insert())

    def test_next_word_on_new_line(self):
        self.editor.set_insert(self.editor.get_relative(self.editor.get_end(), -1))
        self.editor.insert(self.editor.get_end(), '\non a new line')
        self.editor.next_word()
        self.assertEquals('\n', self.editor.get())

    def test_next_word_on_new_line(self):
        self.editor.set_text('hello \n world\n')
        self.editor.next_word()
        self.editor.next_word()
        self.assertEquals('\n', self.editor.get(), self.editor.get())

    def test_next_one_character_word(self):
        self.editor.set_text('1 2\n')
        self.editor.next_word()
        self.assertEquals(' ', self.editor.get())
        self.editor.next_word()
        self.assertEquals('\n', self.editor.get())

    def test_prev_word_on_the_begining(self):
        self.editor.prev_word()
        self.assertEquals('s', self.editor.get())

    def test_prev_word(self):
        self.editor.set_insert(self.editor.get_end())
        self.editor.prev_word()
        self.assertEquals('t', self.editor.get())
        self.editor.prev_word()
        self.assertEquals('s', self.editor.get())

    def test_prev_word_on_the_middle_of_a_word(self):
        self.editor.set_insert(self.editor.get_relative(self.editor.get_end(), -2))
        self.editor.prev_word()
        self.assertEquals('t', self.editor.get())

    def test_prev_one_character_word(self):
        self.editor.set_text('1 2 3')
        self.editor.set_insert(self.editor.get_end())
        self.editor.prev_word()
        self.assertEquals('3', self.editor.get())
        self.editor.prev_word()
        self.assertEquals('2', self.editor.get())
        self.editor.prev_word()
        self.assertEquals('1', self.editor.get())

    def test_deleting_next_word(self):
        self.editor.delete_next_word()
        self.assertEquals(' text', self.editor.get_text())

    def test_deleting_next_word_in_the_middle(self):
        self.editor.set_insert(self.editor.get_index(2))
        self.editor.delete_next_word()
        self.assertEquals('sa text', self.editor.get_text())

    def test_deleting_prev_word(self):
        self.editor.set_insert(self.editor.get_end())
        self.editor.delete_prev_word()
        self.assertEquals('sample ', self.editor.get_text(), self.editor.get_text())

    def test_deleting_prev_word_in_the_middle(self):
        self.editor.set_insert(self.editor.get_relative(self.editor.get_end(), -2))
        self.editor.delete_prev_word()
        self.assertEquals('sample xt', self.editor.get_text(), self.editor.get_text())

    def test_deleting_prev_word_at_the_beginning(self):
        self.editor.set_insert(self.editor.get_index(3))
        self.editor.delete_prev_word()
        self.assertEquals('ple text', self.editor.get_text(), self.editor.get_text())

    def test_next_word_stopping_at_underline(self):
        self.editor.set_text('sample_text')
        self.editor.set_insert(self.editor.get_start())
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_prev_word_stopping_at_underline(self):
        self.editor.set_text('sample_text')
        self.editor.set_insert(self.editor.get_end())
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(7), self.editor.get_insert())

    def test_next_word_stopping_at_end_of_line_preceded_by_a_space(self):
        self.editor.set_text('sampleText ')
        self.editor.set_insert(self.editor.get_index(6))
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(10), self.editor.get_insert())
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(11), self.editor.get_insert())

    def test_next_word_stopping_at_capitals1(self):
        self.editor.set_text('sampleText')
        self.editor.set_insert(self.editor.get_start())
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_next_word_stopping_at_capitals2(self):
        self.editor.set_text('sampleText')
        self.editor.set_insert(self.editor.get_index(6))
        self.editor.next_word()
        self.assertEquals(self.editor.get_end(), self.editor.get_insert())

    def test_next_word_stopping_at_capitals3(self):
        self.editor.set_text('MyHTTPClient')
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(2), self.editor.get_insert())
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_next_word_stopping_at_capitals4(self):
        self.editor.set_text('INSERT')
        self.editor.next_word()
        self.assertEquals(self.editor.get_end(), self.editor.get_insert())

    def test_next_word_stopping_at_capitals5(self):
        self.editor.set_text('INSERT ')
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_next_word_stopping_at_capitals6(self):
        self.editor.set_text(' Hello')
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals(self):
        self.editor.set_text('sampleText')
        self.editor.set_insert(self.editor.get_end())
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals2(self):
        self.editor.set_text('sampleText')
        self.editor.set_insert(self.editor.get_index(7))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals3(self):
        self.editor.set_text('MyHTTPText')
        self.editor.set_insert(self.editor.get_index(6))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(2), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals4(self):
        self.editor.set_text('INSERT')
        self.editor.set_insert(self.editor.get_end())
        self.editor.prev_word()
        self.assertEquals(self.editor.get_start(), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals5(self):
        self.editor.set_text(' INSERT')
        self.editor.set_insert(self.editor.get_index(4))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(1), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals6(self):
        self.editor.set_text('AClass')
        self.editor.set_insert(self.editor.get_index(4))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(1), self.editor.get_insert())

    def test_prev_word_stopping_at_capitals7(self):
        self.editor.set_text('MyClass')
        self.editor.set_insert(self.editor.get_index(2))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(0), self.editor.get_insert())

    def test_next_word_stopping_at_end_of_line(self):
        self.editor.set_text('sample \n   text')
        self.editor.set_insert(self.editor.get_index(6))
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(7), self.editor.get_insert())

    def test_next_word_stopping_at_start_of_line(self):
        self.editor.set_text('sample \n   text')
        self.editor.set_insert(self.editor.get_index(7))
        self.editor.next_word()
        self.assertEquals(self.editor.get_index(8), self.editor.get_insert())

    def test_prev_word_stopping_at_end_of_line(self):
        self.editor.set_text('sample \n   text')
        self.editor.set_insert(self.editor.get_index(9))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(8), self.editor.get_insert())

    def test_prev_word_stopping_at_start_of_line(self):
        self.editor.set_text('sample \n   text')
        self.editor.set_insert(self.editor.get_index(8))
        self.editor.prev_word()
        self.assertEquals(self.editor.get_index(7), self.editor.get_insert())

    def test_upper_next_word(self):
        self.editor.set_text('sample text')
        self.editor.set_insert(self.editor.get_index(1))
        self.editor.upper_next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())
        self.assertEquals('sAMPLE text', self.editor.get_text())

    def test_lower_next_word(self):
        self.editor.set_text('SAMPLE TEXT')
        self.editor.set_insert(self.editor.get_index(1))
        self.editor.lower_next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())
        self.assertEquals('Sample TEXT', self.editor.get_text())

    def test_capitalize_next_word(self):
        self.editor.set_text('sample text')
        self.editor.set_insert(self.editor.get_index(1))
        self.editor.capitalize_next_word()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())
        self.assertEquals('sAmple text', self.editor.get_text())

    def test_capitalize_next_word_after_spaces(self):
        self.editor.set_text(' text')
        self.editor.capitalize_next_word()
        self.assertEquals(' Text', self.editor.get_text())

    def test_going_to_the_start(self):
        self.editor.set_insert(self.editor.get_index(3))
        self.editor.goto_start()
        self.assertEquals(self.editor.get_start(), self.editor.get_insert())

    def test_going_to_the_end(self):
        self.editor.set_insert(self.editor.get_index(3))
        self.editor.goto_end()
        self.assertEquals(self.editor.get_end(), self.editor.get_insert())

    def test_undo(self):
        self.editor.saving_editor()
        self.editor.insert(self.editor.get_end(), '.')
        self.assertEquals('sample text.', self.editor.get_text())
        self.editor.undo()
        self.assertEquals('sample text', self.editor.get_text(),self.editor.get_text())

    def test_redo(self):
        self.editor.saving_editor()
        self.editor.insert(self.editor.get_end(), '.')
        self.editor.undo()
        self.editor.redo()
        self.assertEquals('sample text.', self.editor.get_text(),self.editor.get_text())

    def test_nothing_to_undo(self):
        self.editor.undo()
        self.editor.undo()
        self.editor.undo()

    def test_nothing_to_redo(self):
        self.editor.redo()

    def test_copying(self):
        self.editor.set_mark()
        self.editor.goto_end()
        self.editor.copy_region()
        self.editor.paste()
        self.assertEquals('sample textsample text', self.editor.get_text())

    def test_copying_in_the_middle(self):
        self.editor.next_word()
        self.editor.set_mark()
        self.editor.goto_end()
        self.editor.copy_region()
        self.editor.goto_start()
        self.editor.paste()
        self.assertEquals(' textsample text', self.editor.get_text())

    def test_cutting(self):
        self.editor.set_mark()
        self.editor.next_word()
        self.editor.cut_region()
        self.assertEquals(' text', self.editor.get_text())
        self.editor.paste()
        self.assertEquals('sample text', self.editor.get_text())

    def test_kill_line(self):
        self.editor.set_text('sample\n text')
        self.editor.set_insert(self.editor.get_index(1))
        self.editor.kill_line()
        self.assertEquals('s\n text', self.editor.get_text())
        self.editor.kill_line()
        self.assertEquals('s text', self.editor.get_text())
        self.editor.kill_line()
        self.assertEquals('s', self.editor.get_text())

    def test_mark_not_set(self):
        self.editor.cut_region()
        self.editor.copy_region()
        self.assertEquals('sample text', self.editor.get_text())

    def test_clear_mark(self):
        self.editor.set_mark()
        self.editor.next_word()
        self.editor.clear_mark()
        self.editor.cut_region()
        self.assertEquals('sample text', self.editor.get_text())

    def test_when_insert_while_mark_precedes(self):
        self.editor.next_word()
        self.editor.set_mark()
        self.editor.goto_start()
        self.editor.cut_region()
        self.assertEquals(' text', self.editor.get_text())

    def test_swap_mark_and_insert(self):
        self.editor.set_mark()
        self.editor.next_word()
        self.editor.swap_mark_and_insert()
        self.assertEquals(self.editor.get_start(), self.editor.get_insert())
        self.editor.cut_region()
        self.assertEquals(' text', self.editor.get_text())

    def test_no_mark_swap_mark_and_insert(self):
        self.editor.swap_mark_and_insert()
        self.assertEquals('sample text', self.editor.get_text())

    def test_swap_mark_and_insert_while_insert_precedes(self):
        self.editor.next_word()
        self.editor.set_mark()
        self.editor.goto_start()
        self.editor.swap_mark_and_insert()
        self.assertEquals(self.editor.get_index(6), self.editor.get_insert())
        self.editor.cut_region()
        self.assertEquals(' text', self.editor.get_text())

    def test_insert_tab(self):
        self.editor.set_text('')
        self.editor.insert_tab()
        self.assertEquals((' ' * 4), self.editor.get_text())
        self.editor.insert_tab(self.editor.get_end())
        self.assertEquals((' ' * 8), self.editor.get_text())

    def test_current_line_number(self):
        self.assertEquals(1, self.editor.get_current_line_number())
        self.editor.set_text('sample\n text \n end')
        self.editor.set_insert(self.editor.get_index(9))
        self.assertEquals(2, self.editor.get_current_line_number())
        self.editor.set_insert(self.editor.get_end())
        self.assertEquals(3, self.editor.get_current_line_number())

    def test_resetting_undo_after_set_text(self):
        self.editor.set_text('sample text')
        self.editor.undo()
        self.editor.undo()
        self.assertEquals('sample text', self.editor.get_text())

    def test_get_current_offset(self):
        self.editor.set_text('sample text')
        self.editor.set_insert(self.editor.get_start())
        self.assertEquals(0, self.editor.get_current_offset())
        self.editor.set_insert(self.editor.get_end())
        self.assertEquals(11, self.editor.get_current_offset())

    def test_get_current_offset_multiline(self):
        self.editor.set_text('sample text\n another text \n and yet another')
        self.editor.set_insert(self.editor.get_index(20))
        self.assertEquals(20, self.editor.get_current_offset())
        self.editor.set_insert(self.editor.get_index(30))
        self.assertEquals(30, self.editor.get_current_offset())
        self.editor.set_insert(self.editor.get_index(40))
        self.assertEquals(40, self.editor.get_current_offset())

    def test_after_indenting_insert_position(self):
        self.editor.set_indenter(PythonCodeIndenter(self.editor))
        self.editor.set_text("print 'hello'\n        print 'hello'\n")
        self.editor.set_insert(self.editor.get_index(15))
        self.editor.correct_line_indentation()
        self.assertEquals(self.editor.get_index(14), self.editor.get_insert())

    def test_after_indenting_insert_position2(self):
        self.editor.set_indenter(PythonCodeIndenter(self.editor))
        self.editor.set_text("def f():\n        print 'hello'\n")
        self.editor.set_insert(self.editor.get_index(9))
        self.editor.correct_line_indentation()
        self.assertEquals(self.editor.get_index(13), self.editor.get_insert())

    def test_after_indenting_insert_position3(self):
        self.editor.set_indenter(PythonCodeIndenter(self.editor))
        self.editor.set_text("def f():\n        print 'hello'\n")
        self.editor.set_insert(self.editor.get_index(22))
        self.editor.correct_line_indentation()
        self.assertEquals(self.editor.get_index(18), self.editor.get_insert())

    def test_goto_definition(self):
        self.editor.set_text('\ndef a_func():\n    pass\na_func()\n')
        self.editor.goto_line(2)
        self.assertEquals(2, self.editor.get_current_line_number())

    def test_yanking(self):
        self.editor.next_word()
        self.editor.set_mark()
        self.editor.goto_end()
        self.editor.copy_region()
        self.editor.goto_start()
        self.editor.paste()
        self.assertEquals(' textsample text', self.editor.get_text())


class TextChangeInspectorTest(unittest.TestCase):

    __factory = GraphicalEditorFactory(Tkinter.Frame())

    def setUp(self):
        super(TextChangeInspectorTest, self).setUp()
        editor = TextChangeInspectorTest.__factory.create(get_sample_editingcontext())
        self.text = editor.text
        self.change_inspector = editor.change_inspector

    def tearDown(self):
        super(TextChangeInspectorTest, self).tearDown()

    def test_is_changed(self):
        self.text.insert('insert', 'sample text')
        self.assertTrue(self.change_inspector.is_changed())
        self.change_inspector.clear_changed()
        self.assertFalse(self.change_inspector.is_changed())

    def test_get_changed_region_after_inserts(self):
        self.text.insert('insert', 'sample text')
        self.change_inspector.clear_changed()
        self.text.insert('1.3', 'a')
        self.assertEquals(('1.3', '1.4'), self.change_inspector.get_changed_region())
        self.text.insert('1.6', 'a')
        self.assertEquals(('1.3', '1.7'), self.change_inspector.get_changed_region())

    def test_get_changed_region_after_inserts2(self):
        self.text.insert('insert', 'sample text')
        self.change_inspector.clear_changed()
        self.text.insert('1.3', 'a')
        self.text.insert('1.2', 'aa')
        self.assertEquals(('1.2', '1.6'), self.change_inspector.get_changed_region())

    def test_get_changed_region_after_deletes(self):
        self.text.insert('insert', 'sample text')
        self.change_inspector.clear_changed()
        self.text.delete('1.3', '1.4')
        self.assertEquals(('1.3', '1.3'), self.change_inspector.get_changed_region())
        self.text.delete('1.5', '1.6')
        self.assertEquals(('1.3', '1.5'), self.change_inspector.get_changed_region())

    def test_get_changed_region_after_deletes2(self):
        self.text.insert('insert', 'sample text')
        self.change_inspector.clear_changed()
        self.text.delete('1.5', '1.6')
        self.text.delete('1.1', '1.2')
        self.assertEquals(('1.1', '1.4'), self.change_inspector.get_changed_region())


class KillRingManagerTest(unittest.TestCase):

    def setUp(self):
        super(KillRingManagerTest, self).setUp()
        self.ring = editor.KillRingManager()

    def tearDown(self):
        super(KillRingManagerTest, self).tearDown()

    def test_empty_ring(self):
        self.assertTrue(self.ring.yank(1) is None)

    def test_trivial_yanking(self):
        self.ring.killed('x')
        self.ring.killed('y')
        self.assertEquals('x', self.ring.yank(1))

    def test_yanking_direction(self):
        self.ring.killed('x')
        self.ring.killed('y')
        self.ring.killed('z')
        self.assertEquals('y', self.ring.yank(1))
        self.assertEquals('x', self.ring.yank(2))
        self.assertEquals('z', self.ring.yank(3))

    def test_yanking_the_last_element(self):
        self.ring.killed('x')
        self.ring.killed('y')
        self.assertEquals('y', self.ring.yank(2))

    def test_only_one_entry(self):
        self.ring.killed('x')
        self.assertEquals('x', self.ring.yank(1))

    def test_unclear_conditions2(self):
        ring = editor.KillRingManager(limit=2)
        ring.killed('1')
        ring.killed('2')
        ring.killed('3')
        self.assertEquals('3', ring.yank(2))


def suite():
    result = unittest.TestSuite()
    result.addTests(unittest.makeSuite(GraphicalEditorTest))
    result.addTests(unittest.makeSuite(TextChangeInspectorTest))
    result.addTests(unittest.makeSuite(KillRingManagerTest))
    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.