-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowtexttests.py
599 lines (545 loc) · 26.9 KB
/
flowtexttests.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import flowtext
import unittest
from email.Message import Message
class ReflowTestCase( unittest.TestCase ):
def setUp( self ):
pass
def testConstruction( self ):
flow = flowtext.parser()
self.assertEqual( 66, flow.getMaxWidth() )
#Try constructing again
flow = flowtext.parser( 23 )
self.assertEqual( 23, flow.getMaxWidth() )
def testSetMaxWidth( self ):
flow = flowtext.parser()
flow.setMaxWidth( 40 )
self.assertEqual( 40, flow.getMaxWidth() )
def setOverWide():
flow.setMaxWidth( 99 )
self.assertRaises( ValueError, setOverWide )
def testBasicNormalUnwrap( self ):
flow = flowtext.parser()
testText = ( "This line spans a few \n" +
"lines and has a couple of soft \n" +
"breaks in it.\n" )
expectedText = ( "This line spans a few " +
"lines and has a couple of soft " +
"breaks in it.\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testNegativeUnwrap( self ):
flow = flowtext.parser()
testText = ( "This line spans a few \n" +
"lines and has a couple of soft \n" +
"breaks in it. \n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertNotEqual( testText, outputText )
def testUnwrapTwoParagraphs( self ):
flow = flowtext.parser()
testText = ( "This line spans a few \n" +
"lines and has a couple of soft \n" +
"breaks in it.\n\n" +
"and sneakily has a second \n" +
"paragraph\n" )
expectedText = ( "This line spans a few " +
"lines and has a couple of soft " +
"breaks in it.\n\n" +
"and sneakily has a second " +
"paragraph\n" )
self.assertEqual( expectedText, flow.unwrap( testText ) )
def testBUnwrapQuotedWithGt( self ):
flow = flowtext.parser()
testText = ( ">This line is \n" +
">quoted and has a couple of soft \n" +
">breaks in it.\n" )
expectedText = ( ">This line is " +
"quoted and has a couple of soft " +
"breaks in it.\n" )
self.assertEqual( expectedText, flow.unwrap( testText ) )
def testASimpleUnwrapQuotedWithGt( self ):
flow = flowtext.parser()
testText = ( ">This line is \n>quoted\n" )
expectedText = ( ">This line is quoted\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testUnwrapMixedNormalAndQuoted( self ):
flow = flowtext.parser()
testText = ( ">This line is \n" +
">quoted and has a couple of soft \n" +
">breaks in it.\n\n" +
"This line is not \n" +
"quoted\n" )
expectedText = ( ">This line is " +
"quoted and has a couple of soft " +
"breaks in it.\n\n" +
"This line is not " +
"quoted\n" )
self.assertEqual( expectedText, flow.unwrap( testText ) )
def testUnwrapTwoGTQuotedLines( self ):
flow = flowtext.parser()
testText = ( ">This line is \n" +
">quoted and has a couple of soft \n" +
">breaks in it.\n\n" +
">This line is also \n" +
">quoted\n" )
expectedText = ( ">This line is " +
"quoted and has a couple of soft " +
"breaks in it.\n\n" +
">This line is also " +
"quoted\n" )
self.assertEqual( expectedText, flow.unwrap( testText ) )
def testUnwrapQuotedWithOnlySoftWrap(self):
flow = flowtext.parser()
testText = ( ">This line is \n" +
"quoted and has a couple of soft \n" +
"breaks in it.\n")
expectedText = ( ">This line is " +
"quoted and has a couple of soft " +
"breaks in it.\n")
self.assertEqual( expectedText, flow.unwrap( testText ) )
def testUnwrapQuotedWithSoftWrapAndQuoted(self):
flow = flowtext.parser()
testText = ( ">This line is \n" +
"quoted and has a couple of soft \n" +
">breaks in it.\n")
expectedText = ( ">This line is " +
"quoted and has a couple of soft " +
"breaks in it.\n")
self.assertEqual( expectedText, flow.unwrap( testText ) )
def testStandardiseNewlines(self):
flow = flowtext.parser()
testText = ("This is a basic newline\n" +
"And another type\r" +
"And another\n\r" +
"And yet another\r\n")
expectedText = ("This is a basic newline\n" +
"And another type\n" +
"And another\n" +
"And yet another\n")
self.assertEqual( expectedText, flow.standardiseNewlines(testText))
def testUnwrapUsenetSignature(self):
flow = flowtext.parser()
testText = ( "This line is \n"
"quoted and has a couple of soft \n"
"breaks in it.\n\n"
">This line is also \n"
">quoted and has an \n"
">example of embaressing wrap\n"
"-- \n"
"And this is a usenet signature\n")
expectedText = ( "This line is "
"quoted and has a couple of soft "
"breaks in it.\n\n"
">This line is also "
"quoted and has an "
"example of embaressing wrap\n"
"-- \n"
"And this is a usenet signature\n")
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testUnwrapEndingWithCr(self):
flow = flowtext.parser()
testText = ( "This line ends with a CR which should be untouched\n")
expectedText = ( "This line ends with a CR which should be untouched\n")
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testUnwrapMixedQuoted( self ):
flow = flowtext.parser()
testText = ( "This line is \n" +
"quoted and has a couple of soft \n" +
"breaks in it.\n\n" +
">This line is also \n" +
">quoted and has an \n" +
">example of embaressing wrap\n")
expectedText = ( "This line is " +
"quoted and has a couple of soft " +
"breaks in it.\n\n" +
">This line is also " +
"quoted and has an " +
"example of embaressing wrap\n")
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testUnwrapIllegalQuoteChange( self ):
flow = flowtext.parser()
testText = ( ">>This line is quoted \n"
">>to a depth of 2 \n"
">but this line it wraps to is only 1 and fixed\n"
">>>This line is quoted to \n"
">>>a depth of 3 \n"
">>but this line it wraps to \n"
">>is flowed and quoted to a \n"
">>depth of 2.\n")
expectedText = ( ">>This line is quoted to a depth of 2\n"
">but this line it wraps to is only 1 and fixed\n"
">>>This line is quoted to a depth of 3\n"
">>but this line it wraps to is flowed and quoted to a "
"depth of 2.\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testUnwrapAll( self ):
flow = flowtext.parser()
testText = ( "This line spans a few \n"
"lines and has a couple of soft \n"
"breaks in it. It also has a very, very long line which spans a little "
"too long and therefore needs to be well wrapped.\n"
"\nAfter which there is a new paragraph. This new paragraph has only "
"one long line\n"
" And now an indented (therefore space stuffed) section \n"
" which also spans a few lines, with soft line breaks (using the space "
"then crlf convention)\n\n"
">Another quoted test using the greater than sign \n"
">but its otherwise nothing special.\n\n"
">And here I am going to give a soft break \n"
">>And change the quoting depth - this is an illegal operation\n" )
expectedText = ( "This line spans a few "
"lines and has a couple of soft "
"breaks in it. It also has a very, very long line which spans a little "
"too long and therefore needs to be well wrapped.\n"
"\nAfter which there is a new paragraph. This new paragraph has only "
"one long line\n"
" And now an indented (therefore space stuffed) section "
"which also spans a few lines, with soft line breaks (using the space "
"then crlf convention)\n\n"
">Another quoted test using the greater than sign "
"but its otherwise nothing special.\n\n"
">And here I am going to give a soft break\n"
">>And change the quoting depth - this is an illegal operation\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testWrapBasic(self):
flow = flowtext.parser()
testText = ( "This line is very, very long indeed and will go on and on and on "
"and will be over 66 characters long hopefully forcing the wrapping "
"mechanism into action. Of course we then need to investigate real word "
"boundary wrapping, not merely character splitting\n" )
expectedText = ("This line is very, very long indeed and will go on and on and on \n"
"and will be over 66 characters long hopefully forcing the \n"
"wrapping mechanism into action. Of course we then need to \n"
"investigate real word boundary wrapping, not merely character \n"
"splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testWrapQuotedNoTrailingCr(self):
flow = flowtext.parser()
testText = ( ">This line is very, very long indeed and will go on and on and on "
"and will be over 66 characters long hopefully forcing the wrapping "
"mechanism into action. Of course we then need to investigate real word "
"boundary wrapping, not merely character splitting\n" )
expectedText = (">This line is very, very long indeed and will go on and on and on \n"
">and will be over 66 characters long hopefully forcing the \n"
">wrapping mechanism into action. Of course we then need to \n"
">investigate real word boundary wrapping, not merely character \n"
">splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
def testWrapQuotedLongQuote(self):
flow = flowtext.parser()
testText = ( ">>>>>This line is very, very long indeed and will go on and on and on "
"and will be over 66 characters long hopefully forcing the wrapping "
"mechanism into action. Of course we then need to investigate real word "
"boundary wrapping, not merely character splitting\n" )
expectedText = (">>>>>This line is very, very long indeed and will go on and on \n"
">>>>>and on and will be over 66 characters long hopefully forcing \n"
">>>>>the wrapping mechanism into action. Of course we then need \n"
">>>>>to investigate real word boundary wrapping, not merely \n"
">>>>>character splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
def testWrapLongestLine(self):
flow = flowtext.parser()
testText = ( "ThisIsAllOneWordAndWillNotBeWrappedThislineisvery,verylongindeedandwillgoonandonandon"
"andwillbeover66characterslonghopefullyforcingthewrapping"
"mechanismintoaction.Ofcoursewethenneedtoinvestigaterealword"
"boundarywrapping,notmerelycharactersplitting\n" )
expectedText = testText
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testUnwrapSpaceStuffed(self):
flow = flowtext.parser()
testText = ( " >This is a special line, and although it is more than 66 characters, \n"
" it is space stuffed, and therefore the > should be ignored\n" )
expectedText = (" >This is a special line, and although it is more than 66 characters, "
"it is space stuffed, and therefore the > should be ignored\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.unwrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testWrapQuoted(self):
flow = flowtext.parser()
testText = ( ">This line is very, very long indeed and will go on and on and on "
"and will be over 66 characters long hopefully forcing the wrapping "
"mechanism into action. Of course we then need to investigate real word "
"boundary wrapping, not merely character splitting\n" )
expectedText = (">This line is very, very long indeed and will go on and on and on \n"
">and will be over 66 characters long hopefully forcing the \n"
">wrapping mechanism into action. Of course we then need to \n"
">investigate real word boundary wrapping, not merely character \n"
">splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testFlow(self):
flow = flowtext.parser()
testText = ( ">This line is very, very long \nindeed and will go on and on and on "
"and will be over 66 characters long hopefully \nforcing the wrapping "
"mechanism into action. Of course we then \nneed to investigate real word "
"boundary wrapping, not merely character \nsplitting\n" )
expectedText = (">This line is very, very long indeed and will go on and on and on \n"
">and will be over 66 characters long hopefully forcing the \n"
">wrapping mechanism into action. Of course we then need to \n"
">investigate real word boundary wrapping, not merely character \n"
">splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.flow( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testFlow79(self):
flow = flowtext.parser(79)
testText = ( ">This line is very, very long \nindeed and will go on and on and on "
"and will be over 79 characters long hopefully \nforcing the wrapping "
"mechanism into action. Of course we then \nneed to investigate real word "
"boundary wrapping, not merely character \nsplitting\n" )
expectedText = (">This line is very, very long indeed and will go on and on and on and will be \n"
">over 79 characters long hopefully forcing the wrapping mechanism into action. \n"
">Of course we then need to investigate real word boundary wrapping, not merely \n"
">character splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.flow( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testPrepare(self):
flow = flowtext.parser()
testText = ( "This small document needs to be space stuffed\n"
">It contains this line which is not quoted\n"
" And this line with an intentional space\n"
"From here to infinity\n" )
expectedText = ("This small document needs to be space stuffed\n"
" >It contains this line which is not quoted\n"
" And this line with an intentional space\n"
" From here to infinity\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
outputText = flow.prepNonQuotedText( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testGenerateMessage(self):
flow = flowtext.parser()
msg = flow.generateMessage(">This line is very, very long \nindeed and will go on and on and on "
"and will be over 66 characters long hopefully \nforcing the wrapping "
"mechanism into action. Of course we then \nneed to investigate real word "
"boundary wrapping, not merely character \nsplitting\n")
expectedText = (">This line is very, very long indeed and will go on and on and on \n"
">and will be over 66 characters long hopefully forcing the \n"
">wrapping mechanism into action. Of course we then need to \n"
">investigate real word boundary wrapping, not merely character \n"
">splitting\n" )
self.assertEqual(msg.get_content_type(), "text/plain")
self.assertEqual(msg.get_param("format"), "flowed")
self.assertEqual(msg.get_payload(), expectedText)
def testParsesMessage(self):
flow = flowtext.parser()
msg = Message()
def test():
flow.parseMessage(msg)
self.assertRaises(ValueError, test)
msg = flow.generateMessage(">This line is very, very long \nindeed and will go on and on and on "
"and will be over 66 characters long hopefully \nforcing the wrapping "
"mechanism into action. Of course we then \nneed to investigate real word "
"boundary wrapping, not merely character \nsplitting\n")
expectedText = (">This line is very, very long indeed and will go on and on and on \n"
">and will be over 66 characters long hopefully forcing the \n"
">wrapping mechanism into action. Of course we then need to \n"
">investigate real word boundary wrapping, not merely character \n"
">splitting\n" )
self.assertEqual(flow.parseMessage(msg), expectedText)
class ReflowSpeedTests( unittest.TestCase ):
def setUp( self ):
self.flow = flowtext.parser()
def testWrapSpeedQuoted(self):
testText = ( ">This line is very, very long indeed and will go on and on and on "
"and will be over 66 characters long hopefully forcing the wrapping "
"mechanism into action. Of course we then need to investigate real word "
"boundary wrapping, not merely character splitting\n" )
expectedText = (">This line is very, very long indeed and will go on and on and on \n"
">and will be over 66 characters long hopefully forcing the \n"
">wrapping mechanism into action. Of course we then need to \n"
">investigate real word boundary wrapping, not merely character \n"
">splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
for i in range(0, 10000):
outputText = self.flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
def testWrapSpeedNotQuoted(self):
testText = ( "This line is very, very long indeed and will go on and on and on "
"and will be over 66 characters long hopefully forcing the wrapping "
"mechanism into action. Of course we then need to investigate real word "
"boundary wrapping, not merely character splitting\n" )
expectedText = ("This line is very, very long indeed and will go on and on and on \n"
"and will be over 66 characters long hopefully forcing the \n"
"wrapping mechanism into action. Of course we then need to \n"
"investigate real word boundary wrapping, not merely character \n"
"splitting\n" )
debug = False
if(debug):
print "\n---------------------------\ninput text\n"
print testText
for i in range(0, 10000):
outputText = self.flow.wrap( testText, debug )
if(debug):
print "\n---------------------------\nExpected text\n"
print expectedText
print "\n---------------------------\noutput text\n"
print outputText
self.assertEqual( expectedText, outputText )
testSuite = unittest.TestSuite( ( unittest.makeSuite( ReflowTestCase ) ) )
runner = unittest.TextTestRunner( verbosity = 2 )
runner.run( testSuite )