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 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
|
# vim: ts=4:sw=4:expandtab
#
# This file is part of ReText
# Copyright: 2014 Maurice van der Pot
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import unittest
from ReText import tablemode
class TestTableMode(unittest.TestCase):
def performEdit(self, text, offset, editSize, paddingchar=None, fragment=None):
if editSize < 0:
text = text[:offset + editSize] + text[offset:]
else:
fragment = paddingchar * editSize if not fragment else fragment
text = text[:offset] + fragment + text[offset:]
return text
def checkDetermineEditLists(self, paddingChars, before, edit, after, alignWithAnyEdge):
class Row:
def __init__(self, text, separatorLine, paddingChar):
self.text = text
self.separatorline = separatorLine
self.paddingchar = paddingChar
# Do some sanity checks on the test data to catch simple mistakes
self.assertEqual(len(paddingChars), len(before),
'The number of padding chars should be equal to the number of rows')
self.assertEqual(len(before), len(after),
'The number of rows before and after should be the same')
# Apart from spacing edit only contains a's or d's
self.assertTrue(edit[1].strip(' d') == '' or
edit[1].strip(' a') == '',
"An edit should be a sequence of a's or d's surrounded by spaces")
rows = []
for paddingChar, text in zip(paddingChars, before):
rows.append(Row(text, (paddingChar != ' '), paddingChar))
editedline = edit[0]
editstripped = edit[1].strip()
editsize = len(editstripped)
# The offset passed to _determineEditLists is the one received from the
# contentsChange signal and is always the start of the set of chars
# that are added or removed.
contentsChangeOffset = edit[1].index(editstripped[0])
# However, the editoffset indicates the position before which chars
# must be deleted or after which they must be added (just like the
# offset used in the edits returned by _determineEditLists),
# so for deletions we'll need to add the size of the edit to it
if editstripped[0] == 'd':
editsize = -editsize
editoffset = contentsChangeOffset + len(editstripped)
else:
editoffset = contentsChangeOffset
editLists = tablemode._determineEditLists(
rows,
edit[0],
contentsChangeOffset,
editsize,
alignWithAnyEdge,
)
editedRows = []
self.assertEqual(len(editLists), len(rows))
for i, (row, editList) in enumerate(zip(rows, editLists)):
editedText = row.text
for editEntry in editList:
editedText = self.performEdit(
editedText,
editEntry[0],
editEntry[1],
paddingchar=row.paddingchar,
)
editedRows.append(editedText)
editedRows[editedline] = self.performEdit(
editedRows[editedline],
editoffset,
editsize,
fragment=editstripped,
)
if editedRows != after:
if alignWithAnyEdge:
alignmentScenario = "when aligning any edge with another"
else:
alignmentScenario = "when only aligning edges of cells in the same column"
assertMessage = [f"Output differs {alignmentScenario}.",
"",
"Input:"] + \
[f"{i:3d} '{line}'" for i, line in enumerate(before)] + \
["",
"Edit:",
"{:3d} '{}'".format(*edit),
"",
"Expected output:"] + \
[f"{i:3d} '{line}'" for i, line in enumerate(after)] + \
["",
"Actual output:"] + \
[f"{i:3d} '{line}'" for i, line in enumerate(editedRows)]
self.fail('\n'.join(assertMessage))
def test_simpleInsert(self):
# Insert at the start of a cell so it doesn't need to grow
separatorChars = ' '
before = ['| |',
'| |']
edit = (0, ' a ')
after = ['|a |',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert at the last position in a cell where it doesn't need to grow
separatorChars = ' '
before = ['| |',
'| |']
edit = (0, ' a')
after = ['| a|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert at the end of a cell so it will have to grow
separatorChars = ' '
before = ['| |',
'| |']
edit = (0, ' a')
after = ['| a|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
def test_insertPushAhead(self):
# Insert with enough room to push without growing the cell
separatorChars = ' '
before = ['| x |',
'| |']
edit = (0, ' a ')
after = ['|a x|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert without enough room to push, so the cell will have to grow
separatorChars = ' '
before = ['| x|',
'| |']
edit = (0, ' a ')
after = ['|a x|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
# Insert without enough room to push, so the cell will have to grow,
# but the edge of the cell below it does not move with it because it is
# of an earlier column
separatorChars = ' '
before = ['| | x|',
' | |']
edit = (0, ' a ')
after = ['| |a x|',
' | |']
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert without enough room to push, so the cell will have to grow,
# but the edge of the cell below it does not move with it because it is
# of a later column
separatorChars = ' '
before = [' | x|',
'| | |']
edit = (0, ' a ')
after = [' |a x|',
'| | |']
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert multiple characters forcing a partial grow
separatorChars = ' '
before = ['| |',
'| |']
edit = (0, ' aaaaaa')
after = ['| aaaaaa|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert multiple characters forcing a partial grow through pushing other chars ahead
separatorChars = ' '
before = ['| bb |',
'| |']
edit = (0, ' aaaaaaa')
after = ['| aaaaaaabb|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
def test_insertInSeparatorCell(self):
# Insert in a cell on a separator line
separatorChars = ' -'
before = ['| |',
'|----|']
edit = (1, ' a ')
after = ['| |',
'|--a-|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert in a cell on a separator line forcing it to grow
separatorChars = ' -'
before = ['| |',
'|----|']
edit = (1, ' a ')
after = ['| |',
'|---a-|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert in a cell on a separator line with an alignment marker
separatorChars = ' -'
before = ['| |',
'|---:|']
edit = (1, ' a ')
after = ['| |',
'|--a:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert in a cell on a separator line with an alignment marker forcing it to grow
separatorChars = ' -'
before = ['| |',
'|---:|']
edit = (1, ' a ')
after = ['| |',
'|---a:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert in a cell on a separator line after the alignment marker forcing it to grow
separatorChars = ' -'
before = ['| |',
'|---:|']
edit = (1, ' a')
after = ['| |',
'|---:a|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
def test_insertAboveSeparatorLine(self):
# Insert on another line, without growing the cell
separatorChars = ' -'
before = ['| |',
'|----|']
edit = (0, ' a')
after = ['| a|',
'|----|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert on another line, forcing the separator cell to grow
separatorChars = ' -'
before = ['| |',
'|----|']
edit = (0, ' a')
after = ['| a|',
'|-----|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert on another line, without growing the cell with alignment marker
separatorChars = ' -'
before = ['| |',
'|---:|']
edit = (0, ' a')
after = ['| a|',
'|---:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert on another line, forcing the separator cell with alignment marker to grow
separatorChars = ' -'
before = ['| |',
'|---:|']
edit = (0, ' a')
after = ['| a|',
'|----:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Insert on another line, forcing the separator cell that ends with a regular char to grow
separatorChars = ' -'
before = ['| |',
'|--- |']
edit = (0, ' a')
after = ['| a|',
'|---- |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
def test_insertCascade(self):
# Test if growing of cells cascades onto other lines through edges that are shifted
separatorChars = ' '
before = ['| |',
' | |',
' | |',
' |']
edit = (0, ' a')
after = ['| a|',
' | |',
' | |',
' |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
# Test if growing of cells cascades onto other lines but does not affect unconnected edges
separatorChars = ' '
before = ['| |',
' | |',
' | | |']
edit = (0, ' a')
after = ['| a|',
' | |',
' | | |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
def test_simpleDelete(self):
# Delete at start of cell
separatorChars = ' '
before = ['|abcd|',
'| |']
edit = (0, ' d ')
after = ['|bcd|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Delete at end of cell
separatorChars = ' '
before = ['|abcd|',
'| |']
edit = (0, ' d')
after = ['|abc|',
'| |']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
def test_deleteShrinking(self):
# Shrinking limited by cell on other row
separatorChars = ' '
before = ['|abc |',
'|efgh|']
edit = (0, ' d ')
after = ['|bc |',
'|efgh|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrinking limited by cell on other row (cont'd)
separatorChars = ' '
before = ['|abcd|',
'|efgh|']
edit = (0, ' d')
after = ['|abc |',
'|efgh|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrinking of next cell limited by cell on other row
separatorChars = ' '
before = ['|abc | |',
'|efghi|klm|']
edit = (0, ' d ')
after = ['|bc | |',
'|efghi|klm|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrink current cell fully, grow next cell a partially
separatorChars = ' '
before = ['| aabb| |',
'|xxxxxx|x |']
edit = (0, ' dddd')
after = ['| | |',
'|xxxxxx|x|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrink current cell fully, do not change next cell
separatorChars = ' '
before = ['| aabb| |',
'|xxxxxxxx |']
edit = (0, ' dddd')
after = ['| | |',
'|xxxxxxxx |']
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
def test_deleteShrinkingSeparatorRow(self):
# Shrinking not limited by size of separator cell
separatorChars = ' -'
before = ['|abcd|',
'|----|']
edit = (0, ' d ')
after = ['|acd|',
'|---|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrinking limited by size of separator cell
separatorChars = ' -'
before = ['|abc|',
'|---|']
edit = (0, ' d ')
after = ['|ac |',
'|---|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrinking not limited by size of separator cell with alignment markers
separatorChars = ' -'
before = ['|abcd|',
'|:--:|']
edit = (0, ' d ')
after = ['|acd|',
'|:-:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrinking limited by size of separator cell with alignment markers
separatorChars = ' -'
before = ['|abc|',
'|:-:|']
edit = (0, ' d ')
after = ['|ac |',
'|:-:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
# Shrinking partially limited by size of separator cell with alignment markers
separatorChars = ' -'
before = ['|abcde|',
'|:---:|']
edit = (0, ' dddd')
after = ['|a |',
'|:-:|']
self.checkDetermineEditLists(separatorChars, before, edit, after, True)
self.checkDetermineEditLists(separatorChars, before, edit, after, False)
if __name__ == '__main__':
unittest.main()
|