~ubuntu-branches/ubuntu/utopic/retext/utopic

« back to all changes in this revision

Viewing changes to tests/test_tablemode.py

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev
  • Date: 2014-07-26 12:38:59 UTC
  • mfrom: (20.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140726123859-49z5c24mluzkpui1
Tags: 5.0.0-1
* New upstream release.
* Update debian/copyright.
* Run upstream tests during build.
* Wrap-and-sort debian/control.
* Update debian/watch to point to PyPI.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: noexpandtab:ts=4:sw=4
 
2
# This file is part of ReText
 
3
# Copyright: Maurice van der Pot 2014
 
4
# License: GNU GPL v2 or higher
 
5
 
 
6
import unittest
 
7
from ReText import tablemode
 
8
 
 
9
 
 
10
class TestTableMode(unittest.TestCase):
 
11
 
 
12
        def performEdit(self, text, offset, editSize, paddingchar=None, fragment=None):
 
13
                if editSize < 0:
 
14
                        text = text[:offset + editSize] + text[offset:]
 
15
                else:
 
16
                        fragment = paddingchar * editSize if not fragment else fragment
 
17
                        text = text[:offset] + fragment + text[offset:]
 
18
                return text
 
19
 
 
20
        def checkDetermineEditLists(self, paddingChars, before, edit, after):
 
21
                class Row():
 
22
                        def __init__(self, text, separatorLine, paddingChar):
 
23
                                self.text = text
 
24
                                self.separatorline = separatorLine
 
25
                                self.paddingchar = paddingChar
 
26
 
 
27
 
 
28
                # Do some sanity checks on the test data to catch simple mistakes
 
29
                self.assertEqual(len(paddingChars), len(before),
 
30
                                 'The number of padding chars should be equal to the number of rows')
 
31
                self.assertEqual(len(before), len(after),
 
32
                                 'The number of rows before and after should be the same')
 
33
                # Apart from spacing edit only contains a's or d's
 
34
                self.assertTrue(edit[1].strip(' d') == '' or
 
35
                                edit[1].strip(' a') == '',
 
36
                                                "An edit should be a sequence of a's or d's surrounded by spaces")
 
37
 
 
38
                rows = []
 
39
                for paddingChar, text in zip(paddingChars, before):
 
40
                        rows.append(Row(text, (paddingChar != ' '), paddingChar))
 
41
 
 
42
                editedline = edit[0]
 
43
                editstripped = edit[1].strip()
 
44
                editsize = len(editstripped)
 
45
 
 
46
                # The offset passed to _determineEditLists is the one received from the
 
47
                # contentsChange signal and is always the start of the set of chars
 
48
                # that are added or removed.
 
49
                contentsChangeOffset = edit[1].index(editstripped[0])
 
50
 
 
51
                # However, the editoffset indicates the position before which chars
 
52
                # must be deleted or after which they must be added (just like the
 
53
                # offset used in the edits returned by _determineEditLists),
 
54
                # so for deletions we'll need to add the size of the edit to it
 
55
                if editstripped[0] == 'd':
 
56
                        editsize = -editsize
 
57
                        editoffset = contentsChangeOffset + len(editstripped)
 
58
                else:
 
59
                        editoffset = contentsChangeOffset
 
60
 
 
61
                editLists = tablemode._determineEditLists(rows, edit[0], contentsChangeOffset, editsize)
 
62
 
 
63
 
 
64
                editedRows = []
 
65
 
 
66
                self.assertEqual(len(editLists), len(rows))
 
67
 
 
68
                for i, (row, editList) in enumerate(zip(rows, editLists)):
 
69
                        editedText = row.text
 
70
 
 
71
                        for editEntry in editList:
 
72
                                editedText = self.performEdit(editedText, editEntry[0], editEntry[1], paddingchar=row.paddingchar)
 
73
 
 
74
                        editedRows.append(editedText)
 
75
 
 
76
                editedRows[editedline] = self.performEdit(editedRows[editedline], editoffset, editsize, fragment=editstripped)
 
77
 
 
78
                if editedRows != after:
 
79
                        assertMessage = ["Output differs.",
 
80
                                         "",
 
81
                                         "Input:"] + \
 
82
                                        ["%3d '%s'" % (i, line) for i, line in enumerate(before)] + \
 
83
                                        ["",
 
84
                                         "Edit:",
 
85
                                         "%3d '%s'" % edit,
 
86
                                         "",
 
87
                                         "Expected output:"] + \
 
88
                                        ["%3d '%s'" % (i, line) for i, line in enumerate(after)] + \
 
89
                                        ["",
 
90
                                         "Actual output:"] + \
 
91
                                        ["%3d '%s'" % (i, line) for i, line in enumerate(editedRows)]
 
92
 
 
93
                        self.fail('\n'.join(assertMessage))
 
94
 
 
95
        def test_simpleInsert(self):
 
96
                # Insert at the start of a cell so it doesn't need to grow
 
97
                separatorChars = '  '
 
98
                before  = ['|    |',
 
99
                           '|    |']
 
100
 
 
101
                edit = (0, ' a   ')
 
102
 
 
103
                after   = ['|a   |',
 
104
                           '|    |']
 
105
 
 
106
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
107
 
 
108
                # Insert at the last position in a cell where it doesn't need to grow
 
109
                separatorChars = '  '
 
110
                before  = ['|    |',
 
111
                           '|    |']
 
112
 
 
113
                edit = (0, '    a')
 
114
 
 
115
                after   = ['|   a|',
 
116
                           '|    |']
 
117
 
 
118
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
119
 
 
120
                # Insert at the end of a cell so it will have to grow
 
121
                separatorChars = '  '
 
122
                before  = ['|    |',
 
123
                           '|    |']
 
124
 
 
125
                edit = (0, '     a')
 
126
 
 
127
                after   = ['|    a|',
 
128
                           '|     |']
 
129
 
 
130
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
131
 
 
132
        def test_insertPushAhead(self):
 
133
 
 
134
                # Insert with enough room to push without growing the cell
 
135
                separatorChars = '  '
 
136
                before  = ['|  x |',
 
137
                           '|    |']
 
138
 
 
139
                edit = (0, ' a    ')
 
140
 
 
141
                after   = ['|a  x|',
 
142
                           '|    |']
 
143
 
 
144
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
145
 
 
146
                # Insert without enough room to push, so the cell will have to grow
 
147
                separatorChars = '  '
 
148
                before  = ['|   x|',
 
149
                           '|    |']
 
150
 
 
151
                edit = (0, ' a    ')
 
152
 
 
153
                after   = ['|a   x|',
 
154
                           '|     |']
 
155
 
 
156
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
157
 
 
158
                # Insert multiple characters forcing a partial grow
 
159
                separatorChars = '  '
 
160
                before  = ['|    |',
 
161
                           '|    |']
 
162
 
 
163
                edit = (0, '  aaaaaa')
 
164
 
 
165
                after   = ['| aaaaaa|',
 
166
                           '|       |']
 
167
 
 
168
                # Insert multiple characters forcing a partial grow through pushing other chars ahead
 
169
                separatorChars = '  '
 
170
                before  = ['| bb   |',
 
171
                           '|      |']
 
172
 
 
173
                edit = (0, '  aaaaaaa')
 
174
 
 
175
                after   = ['| aaaaaaabb|',
 
176
                           '|          |']
 
177
 
 
178
 
 
179
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
180
 
 
181
        def test_insertInSeparatorCell(self):
 
182
 
 
183
                # Insert in a cell on a separator line
 
184
                separatorChars = ' -'
 
185
                before  = ['|    |',
 
186
                           '|----|']
 
187
 
 
188
                edit = (1, '   a  ')
 
189
 
 
190
                after   = ['|    |',
 
191
                           '|--a-|']
 
192
 
 
193
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
194
 
 
195
                # Insert in a cell on a separator line forcing it to grow
 
196
                separatorChars = ' -'
 
197
                before  = ['|    |',
 
198
                           '|----|']
 
199
 
 
200
                edit = (1, '    a ')
 
201
 
 
202
                after   = ['|     |',
 
203
                           '|---a-|']
 
204
 
 
205
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
206
 
 
207
                # Insert in a cell on a separator line with an alignment marker
 
208
                separatorChars = ' -'
 
209
                before  = ['|    |',
 
210
                           '|---:|']
 
211
 
 
212
                edit = (1, '   a ')
 
213
 
 
214
                after   = ['|    |',
 
215
                           '|--a:|']
 
216
 
 
217
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
218
 
 
219
                # Insert in a cell on a separator line with an alignment marker forcing it to grow
 
220
                separatorChars = ' -'
 
221
                before  = ['|    |',
 
222
                           '|---:|']
 
223
 
 
224
                edit = (1, '    a ')
 
225
 
 
226
                after   = ['|     |',
 
227
                           '|---a:|']
 
228
 
 
229
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
230
 
 
231
                # Insert in a cell on a separator line after the alignment marker forcing it to grow
 
232
                separatorChars = ' -'
 
233
                before  = ['|    |',
 
234
                           '|---:|']
 
235
 
 
236
                edit = (1, '     a')
 
237
 
 
238
                after   = ['|     |',
 
239
                           '|---:a|']
 
240
 
 
241
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
242
 
 
243
        def test_insertAboveSeparatorLine(self):
 
244
                # Insert on another line, without growing the cell
 
245
                separatorChars = ' -'
 
246
                before  = ['|    |',
 
247
                           '|----|']
 
248
 
 
249
                edit = (0, '    a')
 
250
 
 
251
                after   = ['|   a|',
 
252
                           '|----|']
 
253
 
 
254
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
255
 
 
256
                # Insert on another line, forcing the separator cell to grow
 
257
                separatorChars = ' -'
 
258
                before  = ['|    |',
 
259
                           '|----|']
 
260
 
 
261
                edit = (0, '     a')
 
262
 
 
263
                after   = ['|    a|',
 
264
                           '|-----|']
 
265
 
 
266
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
267
 
 
268
                # Insert on another line, without growing the cell with alignment marker
 
269
                separatorChars = ' -'
 
270
                before  = ['|    |',
 
271
                           '|---:|']
 
272
 
 
273
                edit = (0, '    a')
 
274
 
 
275
                after   = ['|   a|',
 
276
                           '|---:|']
 
277
 
 
278
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
279
 
 
280
                # Insert on another line, forcing the separator cell with alignment marker to grow
 
281
                separatorChars = ' -'
 
282
                before  = ['|    |',
 
283
                           '|---:|']
 
284
 
 
285
                edit = (0, '     a')
 
286
 
 
287
                after   = ['|    a|',
 
288
                           '|----:|']
 
289
 
 
290
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
291
 
 
292
                # Insert on another line, forcing the separator cell that ends with a regular char to grow
 
293
                separatorChars = ' -'
 
294
                before  = ['|    |',
 
295
                           '|--- |']
 
296
 
 
297
                edit = (0, '     a')
 
298
 
 
299
                after   = ['|    a|',
 
300
                           '|---- |']
 
301
 
 
302
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
303
 
 
304
        def test_insertCascade(self):
 
305
                # Test if growing of cells cascades onto other lines through edges that are shifted
 
306
                separatorChars = '    '
 
307
                before  = ['|    |',
 
308
                           '     |    |',
 
309
                                   '          |    |',
 
310
                                   '     |']
 
311
 
 
312
                edit = (0, '     a')
 
313
 
 
314
                after   = ['|    a|',
 
315
                           '      |    |',
 
316
                                   '           |    |',
 
317
                                   '      |']
 
318
 
 
319
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
320
 
 
321
                # Test if growing of cells cascades onto other lines but does not affect unconnected edges
 
322
                separatorChars = '   '
 
323
                before  = ['|    |',
 
324
                           '     |    |',
 
325
                           '       |  |    |']
 
326
 
 
327
                edit = (0, '     a')
 
328
 
 
329
                after   = ['|    a|',
 
330
                           '      |    |',
 
331
                           '       |   |    |']
 
332
 
 
333
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
334
 
 
335
        def test_simpleDelete(self):
 
336
                # Delete at start of cell
 
337
                separatorChars = '  '
 
338
                before  = ['|abcd|',
 
339
                           '|    |']
 
340
 
 
341
                edit = (0, ' d   ')
 
342
 
 
343
                after   = ['|bcd|',
 
344
                           '|   |']
 
345
 
 
346
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
347
 
 
348
                # Delete at end of cell
 
349
                separatorChars = '  '
 
350
                before  = ['|abcd|',
 
351
                           '|    |']
 
352
 
 
353
                edit = (0, '    d')
 
354
 
 
355
                after   = ['|abc|',
 
356
                           '|   |']
 
357
 
 
358
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
359
 
 
360
        def test_deleteShrinking(self):
 
361
                # Shrinking limited by cell on other row
 
362
                separatorChars = '  '
 
363
                before  = ['|abc |',
 
364
                           '|efgh|']
 
365
 
 
366
                edit = (0, ' d  ')
 
367
 
 
368
                after   = ['|bc  |',
 
369
                           '|efgh|']
 
370
 
 
371
                # Shrinking limited by cell on other row (cont'd)
 
372
                separatorChars = '  '
 
373
                before  = ['|abcd|',
 
374
                           '|efgh|']
 
375
 
 
376
                edit = (0, '    d')
 
377
 
 
378
                after   = ['|abc |',
 
379
                           '|efgh|']
 
380
 
 
381
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
382
 
 
383
                # Shrinking of next cell limited by cell on other row
 
384
                separatorChars = '  '
 
385
                before  = ['|abc |    |',
 
386
                           '|efghijklm|']
 
387
 
 
388
                edit = (0, ' d  ')
 
389
 
 
390
                after   = ['|bc |     |',
 
391
                           '|efghijklm|']
 
392
 
 
393
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
394
 
 
395
                # Shrink current cell fully, shrink next cell partially
 
396
                separatorChars = '  '
 
397
                before  = ['| aabb|    |',
 
398
                           '|xxxxxxxx  |']
 
399
 
 
400
                edit = (0, '  dddd')
 
401
 
 
402
                after   = ['| |      |',
 
403
                           '|xxxxxxxx|']
 
404
 
 
405
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
406
 
 
407
        def test_deleteShrinkingSeparatorRow(self):
 
408
                # Shrinking not limited by size of separator cell
 
409
                separatorChars = ' -'
 
410
                before  = ['|abcd|',
 
411
                           '|----|']
 
412
 
 
413
                edit = (0, '  d ')
 
414
 
 
415
                after   = ['|acd|',
 
416
                           '|---|']
 
417
 
 
418
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
419
 
 
420
                # Shrinking limited by size of separator cell
 
421
                separatorChars = ' -'
 
422
                before  = ['|abc|',
 
423
                           '|---|']
 
424
 
 
425
                edit = (0, '  d  ')
 
426
 
 
427
                after   = ['|ac |',
 
428
                           '|---|']
 
429
 
 
430
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
431
 
 
432
                # Shrinking not limited by size of separator cell with alignment markers
 
433
                separatorChars = ' -'
 
434
                before  = ['|abcd|',
 
435
                           '|:--:|']
 
436
 
 
437
                edit = (0, '  d ')
 
438
 
 
439
                after   = ['|acd|',
 
440
                           '|:-:|']
 
441
 
 
442
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
443
 
 
444
                # Shrinking limited by size of separator cell with alignment markers
 
445
                separatorChars = ' -'
 
446
                before  = ['|abc|',
 
447
                           '|:-:|']
 
448
 
 
449
                edit = (0, '  d  ')
 
450
 
 
451
                after   = ['|ac |',
 
452
                           '|:-:|']
 
453
 
 
454
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
455
 
 
456
                # Shrinking partially limited by size of separator cell with alignment markers
 
457
                separatorChars = ' -'
 
458
                before  = ['|abcde|',
 
459
                           '|:---:|']
 
460
 
 
461
                edit = (0, '  dddd')
 
462
 
 
463
                after   = ['|a  |',
 
464
                           '|:-:|']
 
465
 
 
466
                self.checkDetermineEditLists(separatorChars, before, edit, after)
 
467
 
 
468
if __name__ == '__main__':
 
469
        unittest.main()