-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
282 lines (210 loc) · 11.3 KB
/
tests.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
import unittest
import random
from model import Document, UndoableDocument
class DocumentTestCase(unittest.TestCase):
def setUp(self):
self.doc=Document()
def test_insert(self):
self.doc.insert(0,'here')
self.assertEqual(self.doc.visible_text, 'here')
self.doc.insert(0,'again ')
self.assertEqual(self.doc.visible_text, 'again here')
self.doc.insert(8,'1')
self.assertEqual(self.doc.visible_text, 'again he1re')
self.doc.insert(8,'23')
self.assertEqual(self.doc.visible_text, 'again he231re')
self.doc.insert(len(self.doc.visible_text),'ing')
self.assertEqual(self.doc.visible_text, 'again he231reing')
def test_remove(self):
self.doc.insert(0,'here is some stuff\nover a few lines')
self.assertEqual(self.doc.visible_text, 'here is some stuff\nover a few lines')
self.doc.remove(0,5)
self.assertEqual(self.doc.visible_text, 'is some stuff\nover a few lines')
self.doc.remove(1,1)
self.assertEqual(self.doc.visible_text, 'i some stuff\nover a few lines')
self.doc.remove(len(self.doc.visible_text)-4,4)
self.assertEqual(self.doc.visible_text, 'i some stuff\nover a few l')
def test_search(self):
self.doc.insert(0,'hello there\nthis is a test\nof search')
self.doc.search('hello')
self.assertEqual(self.doc.current_search, 'hello')
self.assertEqual(self.doc.visible_text, 'hello there')
self.doc.search('o')
self.assertEqual(self.doc.visible_text, 'hello there\nof search')
self.doc.search('th')
self.assertEqual(self.doc.visible_text, 'hello there\nthis is a test')
self.doc.search('')
self.assertEqual(self.doc.visible_text, 'hello there\nthis is a test\nof search')
def test_search_multiple_words(self):
self.doc.insert(0,'hello there\nthis is a test\nof search')
self.doc.search('this test')
self.assertEqual(self.doc.current_search, 'this test')
self.assertEqual(self.doc.visible_text, 'this is a test')
def test_search_and_insert(self):
self.doc.search('test')
self.assertEqual(self.doc.visible_text, '')
self.doc.search('')
self.doc.insert(0,'hello there\nthis is a test\nof search')
self.doc.search('test')
self.assertEqual(self.doc.visible_text, 'this is a test')
self.doc.insert(0, 'again ')
self.assertEqual(self.doc.visible_text, 'again this is a test')
self.assertEqual(self.doc.text,'hello there\nagain this is a test\nof search')
self.doc.search('')
self.assertEqual(self.doc.visible_text,'hello there\nagain this is a test\nof search')
self.doc.search('search')
self.assertEqual(self.doc.visible_text, 'of search')
self.doc.insert(0,'hello ')
self.assertEqual(self.doc.visible_text, 'hello of search')
self.doc.search('hello')
self.assertEqual(self.doc.visible_text,'hello there\nhello of search')
self.doc.insert(11, 'go')
self.assertEqual(self.doc.visible_text,'hello therego\nhello of search')
self.assertEqual(self.doc.text,'hello therego\nagain this is a test\nhello of search')
self.doc.insert(15, '23')
self.assertEqual(self.doc.visible_text,'hello therego\nh23ello of search')
self.assertEqual(self.doc.text,'hello therego\nagain this is a test\nh23ello of search')
def test_search_and_remove(self):
self.doc.search('')
self.doc.insert(0,'hello there\nthis is a test\nof search\nhello there')
self.doc.search('hello')
self.assertEqual(self.doc.visible_text, 'hello there\nhello there')
self.doc.remove(0, 3)
self.assertEqual(self.doc.visible_text, 'lo there\nhello there')
self.doc.remove(len(self.doc.visible_text)-3, 3)
self.assertEqual(self.doc.visible_text, 'lo there\nhello th')
# now remove accross multiple visible regions
self.doc.remove(7, 3)
self.assertEqual(self.doc.visible_text, 'lo therello th')
# finally check underlying text matches what we expect
self.assertEqual(self.doc.text, 'lo therello th\nthis is a test\nof search\n')
def test_insert_remove(self):
# in the simple case (without search) inserting them remove should
# have no effect
self.doc.insert(0,'hello there this is a test\n of inserting and removing\nsome text')
self._check_inserting_and_removing()
def test_insert_remove_while_searching(self):
# when inserting and removing (in that order) with search nothing should change either
self.doc.insert(0,'hello there this is a test\n of inserting and removing\nsome text')
self.doc.search('te')
self._check_inserting_and_removing()
self.doc.search('hello')
self._check_inserting_and_removing()
self.doc.search('inserting')
self._check_inserting_and_removing()
self.doc.search('some')
self._check_inserting_and_removing()
# now try a search with no visible lines
self.doc.search('876fdjfdsf87')
self._check_inserting_and_removing()
def _check_inserting_and_removing(self):
for i in range(0,200):
text_length=len(self.doc.visible_text)
offset=random.choice(range(text_length+1))
length=random.choice(range(text_length+1)) + 1
s=''.join(random.choice('gsgs8hkjefw98y') for i in range(length))
visible_text_before=self.doc.visible_text
text_before=self.doc.text
self.doc.insert(offset, s)
self.assert_( visible_text_before != self.doc.visible_text )
self.assert_( text_before != self.doc.text )
self.doc.remove(offset, length)
self.assertEqual( self.doc.visible_text, visible_text_before )
self.assertEqual( self.doc.text, text_before )
class UndoableDocumentTestCase(unittest.TestCase):
def setUp(self):
self.doc=UndoableDocument()
def test_undo_insert(self):
self.doc.insert(0,'here is some text')
self.assertEqual( self.doc.text, 'here is some text' )
self.assert_( self.doc.can_undo() )
self.doc.undo()
self.assert_( not self.doc.can_undo() )
self.assertEqual( self.doc.text, '' )
self.doc.insert(0,'here is some text')
self.doc.insert(2,'3')
self.assertEqual( self.doc.text, 'he3re is some text' )
self.assert_( self.doc.can_undo() )
self.doc.undo()
self.assert_( self.doc.can_undo() )
self.assertEqual( self.doc.text, 'here is some text' )
def test_undo_remove(self):
self.doc.insert(0,'here is some text')
self.assertEqual( self.doc.text, 'here is some text' )
self.doc.remove(1, 2)
self.assertEqual( self.doc.text, 'he is some text' )
self.assert_( self.doc.can_undo() )
self.doc.undo()
self.assertEqual( self.doc.text, 'here is some text' )
def test_undo_insert_search(self):
self.doc.insert(0,'here is some text\nhello there')
self.doc.search('hello')
self.assertEqual( self.doc.visible_text, 'hello there' )
self.doc.insert(0, '123 ')
self.assertEqual( self.doc.visible_text, '123 hello there' )
self.assertEqual( self.doc.text, 'here is some text\n123 hello there')
self.assert_( self.doc.can_undo() )
self.doc.undo()
self.assertEqual( self.doc.visible_text, 'hello there' )
self.assertEqual( self.doc.text, 'here is some text\nhello there')
self.assertEqual( self.doc.current_search, 'hello')
self.assert_( self.doc.can_undo() )
self.doc.undo() # undo search
self.assert_( self.doc.can_undo() )
self.doc.undo() # undo first insert
self.assert_( not self.doc.can_undo() )
self.assertEqual( self.doc.current_search, '')
self.assertEqual( self.doc.visible_text, '' )
self.assertEqual( self.doc.text, '')
def test_undo_remove_search(self):
self.doc.insert(0,'here is some text\nhello there')
self.doc.search('hello')
self.assertEqual( self.doc.visible_text, 'hello there' )
self.doc.remove(1, 2)
self.assertEqual( self.doc.visible_text, 'hlo there' )
self.assert_( self.doc.can_undo() )
self.doc.undo()
self.assertEqual( self.doc.current_search, 'hello')
self.assertEqual( self.doc.visible_text, 'hello there' )
self.assertEqual( self.doc.text, 'here is some text\nhello there' )
def test_undo_remove_search_accross_visible(self):
self.doc.insert(0,'hello today\nhere is some text\nhello there')
self.doc.search('hello')
self.assertEqual( self.doc.visible_text, 'hello today\nhello there' )
self.doc.remove(10, 3)
self.assertEqual( self.doc.visible_text, 'hello todaello there' )
self.assertEqual( self.doc.text, 'hello todaello there\nhere is some text\n' )
self.assert_( self.doc.can_undo() )
self.doc.undo()
self.assertEqual( self.doc.current_search, 'hello')
self.assertEqual( self.doc.visible_text, 'hello today\nhello there' )
self.assertEqual( self.doc.text, 'hello today\nhere is some text\nhello there' )
self.doc.undo() # undo search
self.doc.undo() # undo first insert
self.assertEqual( self.doc.visible_text, self.doc.text )
self.assertEqual( self.doc.text, '' )
def test_undo_redo(self):
self.doc.insert(0,'hello today\nhere is some text\nhello there')
self.assertEqual( self.doc.visible_text, 'hello today\nhere is some text\nhello there')
self.doc.search('hello')
self.assertEqual( self.doc.visible_text, 'hello today\nhello there' )
self.assert_( not self.doc.can_redo() )
self.assertEqual( self.doc.current_search, 'hello')
self.doc.undo() # undo the search
self.assertEqual( self.doc.visible_text, 'hello today\nhere is some text\nhello there')
self.assertEqual( self.doc.current_search, '')
self.assert_( self.doc.can_redo() )
self.doc.redo() # redo the search
self.assertEqual( self.doc.visible_text, 'hello today\nhello there' )
self.assert_( not self.doc.can_redo() )
self.assertEqual( self.doc.current_search, 'hello')
self.doc.undo() # undo the search again
# now do something else and verify we have lost the redo
self.assert_( self.doc.can_redo() )
self.doc.insert(0, '12')
self.assert_( not self.doc.can_redo() )
self.assertEqual( self.doc.visible_text, '12hello today\nhere is some text\nhello there')
self.doc.undo()
self.assertEqual( self.doc.visible_text, 'hello today\nhere is some text\nhello there')
self.doc.redo()
self.assertEqual( self.doc.visible_text, '12hello today\nhere is some text\nhello there')