/usr/lib/python2.7/dist-packages/mx/TextTools/mxTextTools/test.py is in python-egenix-mxtexttools 3.2.7-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 | # -*- coding: latin-1 -*-
from mx.TextTools import __version__
from mx.TextTools.Examples.HTML import *
from mx.TextTools.Constants.TagTables import *
import pprint, time, pickle
print 'Testing mxTextTools version', __version__
print
# Test for Unicode
try:
unicode
except NameError:
HAVE_UNICODE = 0
else:
HAVE_UNICODE = 1
ua = unicode('a')
ub = unicode('b')
uc = unicode('c')
ud = unicode('d')
ue = unicode('e')
uabc = unicode('abc')
uHello = unicode('Hello')
uempty = unicode('')
# Find a HTML file usable for the test
if len(sys.argv) > 1:
filenames = sys.argv[1:]
else:
filenames = ['/usr/share/doc/packages/mysql/html/manual.html',
'../Doc/mxTextTools.html']
text = ''
for filename in filenames:
try:
text = open(filename).read()
except IOError:
pass
else:
print 'HTML file used for testing the Tagging Engine:'
print ' ', filename
print
break
if not text:
text = """
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>mx Extension Series - License Information</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">
...
<CENTER><FONT SIZE=-1>© 2000, Copyright by eGenix.com
Software GmbH, Langengeld, Germany; All Rights Reserved. mailto:
<A HREF="mailto:info@egenix.com">info@egenix.com</A>
</FONT></CENTER>
</BODY>
</HTML>
"""#"
# Test suite
while 1:
if 1:
print 'Tagging Engine:'
print ' parsing HTML ...',
utext = upper(text)
t = time.clock()
result, taglist, nextindex = tag(utext, htmltable)
assert result == 1
print ' done. (%5.2f sec.; len(taglist)=%i)' % \
(time.clock() - t, len(taglist))
if HAVE_UNICODE:
print ' parsing Unicode HTML ...',
try:
uutext = unicode(utext, 'latin-1')
except UnicodeError:
print ' ... HTML data must be Latin-1; skipping test.'
else:
t = time.clock()
result, utaglist, nextindex = tag(uutext, htmltable)
assert result == 1
print ' done. (%5.2f sec.; len(utaglist)=%i)' % \
(time.clock() - t, len(utaglist))
assert taglist == utaglist
utaglist = None
uutext = None
utext = None
taglist = None
print ' testing some tag table semantics...'
table = ((None,Word,'Word'),)
assert tag('Word',table)[0] == 1
assert tag('word',table)[0] == 0
assert tag('xyz',table)[0] == 0
table = ((None,Word,'Word',MatchFail),)
assert tag('Word',table)[0] == 1
assert tag('word',table)[0] == 0
assert tag('xyz',table)[0] == 0
table = ((None,Word,'Word',MatchOk),)
assert tag('Word',table)[0] == 1
assert tag('word',table)[0] == 1
assert tag('xyz',table)[0] == 1
table = ((None,Word,'Word',MatchOk,MatchFail),)
assert tag('Word',table)[0] == 0
assert tag('word',table)[0] == 1
assert tag('xyz',table)[0] == 1
print ' done.'
#continue
print 'splitat()'
assert splitat('Hello','l') == ('He', 'lo')
assert splitat('Hello','l',2) == ('Hel', 'o')
assert splitat('Hello','l',-1) == ('Hel', 'o')
assert splitat('Hello','l',-2) == ('He', 'lo')
if HAVE_UNICODE:
assert splitat(uHello,'l') == (unicode('He'), unicode('lo'))
assert splitat(uHello,'l',2) == (unicode('Hel'), unicode('o'))
assert splitat(uHello,unicode('l'),-1) == (unicode('Hel'), unicode('o'))
assert splitat(uHello,unicode('l'),-2) == (unicode('He'), unicode('lo'))
print 'suffix()'
assert suffix('abc.html/',('.htm','abc','.html','/'),0,3) == 'abc'
assert suffix('abc.html/',('.htm','abc','.html','/'),0,4) == None
assert suffix('abc.html/',('.htm','abc','.html','/'),0,8) == '.html'
if HAVE_UNICODE:
assert suffix(unicode('abc.html/'),('.htm','abc','.html','/'),0,3) == unicode('abc')
assert suffix(unicode('abc.html/'),(unicode('.htm'),unicode('abc'),'.html','/'),0,3) == unicode('abc')
assert suffix(unicode('abc.html/'),('.htm','abc',unicode('.html'),'/'),0,3) == unicode('abc')
try:
suffix('abc.html/',(unicode('.htm'),'abc','.html','/'))
except TypeError:
pass
else:
raise AssertionError, \
'suffix(string,...) should not accept unicode suffixes'
assert suffix(unicode('abc.html/'),('.htm','abc','.html','/'),0,4) == None
print 'prefix()'
assert prefix('abc.html/',('.htm','abc','.html','/'),0,3) == 'abc'
assert prefix('abc.html/',('.htm','abc','.html','/'),1,4) == None
assert prefix('abc.html/',('.htm','abc','.html','/'),3,9) == '.htm'
if HAVE_UNICODE:
assert prefix(unicode('abc.html/'),('.htm','abc','.html','/'),0,3) == unicode('abc')
assert prefix(unicode('abc.html/'),(unicode('.htm'),unicode('abc'),'.html','/'),0,3) == unicode('abc')
assert prefix(unicode('abc.html/'),('.htm','abc',unicode('.html'),'/'),0,3) == unicode('abc')
try:
prefix('abc.html/',(unicode('.htm'),'abc','.html','/'))
except TypeError:
pass
else:
raise AssertionError, \
'prefix(string,...) should not accept unicode prefixes'
assert prefix(unicode('abc.html/'),('.htm','abc','.html','/'),0,4) == unicode('abc')
print 'join()'
assert join(('a','b','c')) == 'abc'
assert join(['a','b','c']) == 'abc'
assert join(('a','b','c'),' ') == 'a b c'
assert join(['a','b','c'],' ') == 'a b c'
assert join((('abc',0,1),('abc',1,2),('abc',2,3))) == 'abc'
assert join((('abc',0,1),'b',('abc',2,3))) == 'abc'
assert join((('abc',0,3),)) == 'abc'
if HAVE_UNICODE:
assert join((ua,ub,uc)) == uabc
assert join([ua,ub,uc]) == uabc
assert join((ua,ub,uc),' ') == unicode('a b c')
assert join([ua,ub,uc],' ') == unicode('a b c')
assert join(((uabc,0,1),(uabc,1,2),(uabc,2,3))) == uabc
assert join(((uabc,0,1),ub,(uabc,2,3))) == uabc
assert join(((uabc,0,3),)) == uabc
print 'upper()'
assert upper('HeLLo') == 'HELLO'
assert upper('hello') == 'HELLO'
assert upper('HELLO') == 'HELLO'
assert upper('HELLO ') == 'HELLO '
assert upper('HELLO 123') == 'HELLO 123'
if HAVE_UNICODE:
assert upper(unicode('HeLLo')) == unicode('HELLO')
assert upper(unicode('hello')) == unicode('HELLO')
assert upper(unicode('HELLO')) == unicode('HELLO')
assert upper(unicode('HELLO ')) == unicode('HELLO ')
assert upper(unicode('HELLO 123')) == unicode('HELLO 123')
print 'lower()'
assert lower('HeLLo') == 'hello'
assert lower('hello') == 'hello'
assert lower('HELLO') == 'hello'
assert lower('HELLO ') == 'hello '
assert lower('HELLO 123') == 'hello 123'
if HAVE_UNICODE:
assert lower(unicode('HeLLo')) == unicode('hello')
assert lower(unicode('hello')) == unicode('hello')
assert lower(unicode('HELLO')) == unicode('hello')
assert lower(unicode('HELLO ')) == unicode('hello ')
assert lower(unicode('HELLO 123')) == unicode('hello 123')
print 'isascii()'
assert isascii('abc') == 1
assert isascii('abcäöü') == 0
assert isascii('abcäöüdef') == 0
assert isascii('.,- 1234') == 1
if HAVE_UNICODE:
assert isascii(uabc) == 1
assert isascii(unicode('abcäöü', 'latin-1')) == 0
assert isascii(unicode('abcäöüdef', 'latin-1')) == 0
assert isascii(unicode('.,- 1234')) == 1
print 'setstrip()'
assert setstrip('Hello', set('')) == 'Hello'
assert setstrip('Hello', set('o')) == 'Hell'
assert setstrip(' Hello ', set(' o')) == 'Hell'
assert setstrip(' Hello ', set(' o'), 0, len(' Hello '), -1) == 'Hello '
assert setstrip(' Hello ', set(' o'), 0, len(' Hello '), 1) == ' Hell'
assert setstrip(' ', set(' ')) == ''
print 'setsplit()'
assert setsplit('Hello', set('l')) == ['He', 'o']
assert setsplit('Hello', set('lo')) == ['He',]
assert setsplit('Hello', set('abc')) == ['Hello',]
print 'setsplitx()'
assert setsplitx('Hello', set('l')) == ['He', 'll', 'o']
assert setsplitx('Hello', set('lo')) == ['He', 'llo']
assert setsplitx('Hello', set('abc')) == ['Hello',]
print 'joinlist()'
assert joinlist('Hello', [('A',1,2), ('B',3,4)]) == \
[('Hello', 0, 1), 'A', ('Hello', 2, 3), 'B', ('Hello', 4, 5)]
assert join(joinlist('Hello', [('A',1,2), ('B',3,4)])) == \
'HAlBo'
if HAVE_UNICODE:
assert joinlist(uHello, [('A',1,2), ('B',3,4)]) == \
[(uHello, 0, 1), 'A', (uHello, 2, 3), 'B', (uHello, 4, 5)]
assert join(joinlist(uHello, [('A',1,2), ('B',3,4)])) == \
unicode('HAlBo')
assert join(joinlist('Hello', [(ua,1,2), (ub,3,4)])) == \
unicode('Halbo')
print 'charsplit()'
assert charsplit('Hello', 'l') == ['He', '', 'o']
assert charsplit('Hello', 'e') == ['H', 'llo']
assert charsplit('HelloHello', 'e') == ['H', 'lloH', 'llo']
if HAVE_UNICODE:
assert charsplit(uHello, unicode('l')) == [unicode('He'), unicode(''), unicode('o')]
assert charsplit(uHello, unicode('e')) == [unicode('H'), unicode('llo')]
assert charsplit(uHello*2, unicode('e')) == [unicode('H'), unicode('lloH'), unicode('llo')]
print 'CharSet().contains()'
tests = [
("a-z",
('a', 1), ('b', 1), ('c', 1), ('z', 1),
('A', 0), ('B', 0), ('C', 0), ('Z', 0),
),
("a\-z",
('a', 1), ('b', 0), ('c', 0), ('z', 1), ('-', 1),
),
]
if HAVE_UNICODE:
tests[len(tests):] = [
("a-z",
('a', 1), ('b', 1), ('c', 1), ('z', 1),
('A', 0), ('B', 0), ('C', 0), ('Z', 0),
(unicode('a'), 1), (unicode('b'), 1), (unicode('c'), 1), (unicode('z'), 1),
(unicode('A'), 0), (unicode('B'), 0), (unicode('C'), 0), (unicode('Z'), 0),
),
("abc",
('a', 1), ('b', 1), ('c', 1), ('z', 0),
('A', 0), ('B', 0), ('C', 0), ('Z', 0),
(unicode('a'), 1), (unicode('b'), 1), (unicode('c'), 1), (unicode('z'), 0),
(unicode('A'), 0), (unicode('B'), 0), (unicode('C'), 0), (unicode('Z'), 0),
),
(unicode("abc"),
('a', 1), ('b', 1), ('c', 1), ('z', 0),
('A', 0), ('B', 0), ('C', 0), ('Z', 0),
(unicode('a'), 1), (unicode('b'), 1), (unicode('c'), 1), (unicode('z'), 0),
(unicode('A'), 0), (unicode('B'), 0), (unicode('C'), 0), (unicode('Z'), 0),
),
(unicode('a-z\uFFFF', 'unicode-escape'),
('a', 1), ('b', 1), ('c', 1), ('z', 1),
('A', 0), ('B', 0), ('C', 0), ('Z', 0),
(unicode('a'), 1), (unicode('b'), 1), (unicode('c'), 1), (unicode('z'), 1),
(unicode('A'), 0), (unicode('B'), 0), (unicode('C'), 0), (unicode('Z'), 0),
(unichr(55555), 0), (unichr(1234), 0), (unichr(1010), 0),
(unichr(0xFFFF), 1),
),
(unicode("a\-z"),
('a', 1), ('b', 0), ('c', 0), ('z', 1), ('-', 1),
),
]
for test in tests:
cs = CharSet(test[0])
for ch, rc in test[1:]:
assert cs.contains(ch) == rc, \
'CharSet(%s).contains(%s) ... expected: %s' % \
(repr(cs.definition), repr(ch), rc)
print 'CharSet().search()'
tests = [
("a-z",
('', None), ('abc', 0), ('ABCd', 3),
),
("a\-z",
('', None), ('bcd', None), ('ABCd', None), ('zzz', 0),
),
("abc",
('', None), ('bcd', 0), ('ABCd', None), ('zzz', None), ('dddbbb', 3),
),
]
if HAVE_UNICODE:
tests[len(tests):] = [
("a-z",
('', None), ('abc', 0), ('ABCd', 3),
(unicode(''), None), (unicode('abc'), 0), (unicode('ABCd'), 3),
),
("a\-z",
('', None), ('bcd', None), ('ABCd', None), ('zzz', 0),
(unicode(''), None), (unicode('bcd'), None), (unicode('ABCd'), None), (unicode('zzz'), 0),
),
("abc",
('', None), ('bcd', 0), ('ABCd', None), ('zzz', None), ('dddbbb', 3),
(unicode(''), None), (unicode('bcd'), 0), (unicode('ABCd'), None), (unicode('zzz'), None), (unicode('dddbbb'), 3),
),
(unicode('a-z\uFFFF', 'unicode-escape'),
('', None), ('abc', 0), ('ABCd', 3),
(unicode(''), None), (unicode('abc'), 0), (unicode('ABCd'), 3),
(unichr(0xFFFF), 0),
),
(unicode('a\-z'),
('', None), ('bcd', None), ('ABCd', None), ('zzz', 0),
(unicode(''), None), (unicode('bcd'), None), (unicode('ABCd'), None), (unicode('zzz'), 0),
),
(unicode('abc'),
('', None), ('bcd', 0), ('ABCd', None), ('zzz', None), ('dddbbb', 3),
(unicode(''), None), (unicode('bcd'), 0), (unicode('ABCd'), None), (unicode('zzz'), None), (unicode('dddbbb'), 3),
),
]
for test in tests:
cs = CharSet(test[0])
for ch, rc in test[1:]:
assert cs.search(ch) == rc, \
'CharSet(%s).search(%s) ... expected: %s' % \
(repr(cs.definition), repr(ch), rc)
print 'CharSet().match()'
tests = [
("a-z",
('', 0), ('abc', 3), ('ABCd', 0),
),
("a\-z",
('', 0), ('bcd', 0), ('ABCd', 0), ('zzz', 3),
),
("abc",
('', 0), ('bcd', 2), ('ABCd', 0), ('zzz', 0), ('dddbbb', 0),
),
]
if HAVE_UNICODE:
tests[len(tests):] = [
("a-z",
('', 0), ('abc', 3), ('ABCd', 0),
(unicode(''), 0), (unicode('abc'), 3), (unicode('ABCd'), 0),
),
("a\-z",
('', 0), ('bcd', 0), ('ABCd', 0), ('zzz', 3),
(unicode(''), 0), (unicode('bcd'), 0), (unicode('ABCd'), 0), (unicode('zzz'), 3),
),
("abc",
('', 0), ('bcd', 2), ('ABCd', 0), ('zzz', 0), ('dddbbb', 0),
(unicode(''), 0), (unicode('bcd'), 2), (unicode('ABCd'), 0), (unicode('zzz'), 0), (unicode('dddbbb'), 0),
),
(unicode('a-z\uFFFF', 'unicode-escape'),
('', 0), ('abc', 3), ('ABCd', 0),
(unicode(''), 0), (unicode('abc'), 3), (unicode('ABCd'), 0),
(unichr(0xFFFF), 1),
),
(unicode('a\-z'),
('', 0), ('bcd', 0), ('ABCd', 0), ('zzz', 3),
(unicode(''), 0), (unicode('bcd'), 0), (unicode('ABCd'), 0), (unicode('zzz'), 3),
),
(unicode('abc'),
('', 0), ('bcd', 2), ('ABCd', 0), ('zzz', 0), ('dddbbb', 0),
(unicode(''), 0), (unicode('bcd'), 2), (unicode('ABCd'), 0), (unicode('zzz'), 0), (unicode('dddbbb'), 0),
),
]
for test in tests:
cs = CharSet(test[0])
for ch, rc in test[1:]:
assert cs.match(ch) == rc, \
'CharSet(%s).match(%s) ... expected: %s' % \
(repr(cs.definition), repr(ch), rc)
print 'CharSet().strip()'
assert CharSet('').strip('Hello') == 'Hello'
assert CharSet('o').strip('Hello') == 'Hell'
assert CharSet(' o').strip(' Hello ') == 'Hell'
assert CharSet(' o').strip(' Hello ', -1, 0, len(' Hello ')) == 'Hello '
assert CharSet(' o').strip(' Hello ', 1, 0, len(' Hello ')) == ' Hell'
assert CharSet(' ').strip(' ') == ''
if HAVE_UNICODE:
assert CharSet('').strip(unicode('Hello')) == unicode('Hello')
assert CharSet('o').strip(unicode('Hello')) == unicode('Hell')
assert CharSet(' o').strip(unicode(' Hello ')) == unicode('Hell')
assert CharSet(' o').strip(unicode(' Hello '), -1, 0, len(unicode(' Hello '))) == unicode('Hello ')
assert CharSet(' o').strip(unicode(' Hello '), 1, 0, len(unicode(' Hello '))) == unicode(' Hell')
assert CharSet(unicode('')).strip(unicode('Hello')) == unicode('Hello')
assert CharSet(unicode('o')).strip(unicode('Hello')) == unicode('Hell')
assert CharSet(unicode(' o')).strip(unicode(' Hello ')) == unicode('Hell')
assert CharSet(unicode(' o')).strip(unicode(' Hello '), -1, 0, len(unicode(' Hello '))) == unicode('Hello ')
assert CharSet(unicode(' o')).strip(unicode(' Hello '), 1, 0, len(unicode(' Hello '))) == unicode(' Hell')
print 'CharSet().split()'
assert CharSet('l').split('Hello') == ['He', 'o']
assert CharSet('lo').split('Hello') == ['He',]
assert CharSet('abc').split('Hello') == ['Hello',]
if HAVE_UNICODE:
assert CharSet('l').split(unicode('Hello')) == ['He', 'o']
assert CharSet('lo').split(unicode('Hello')) == ['He',]
assert CharSet('abc').split(unicode('Hello')) == ['Hello',]
assert CharSet(unicode('l')).split(unicode('Hello')) == ['He', 'o']
assert CharSet(unicode('lo')).split(unicode('Hello')) == ['He',]
assert CharSet(unicode('abc')).split(unicode('Hello')) == ['Hello',]
print 'CharSet().splitx()'
assert CharSet('l').splitx('Hello') == ['He', 'll', 'o']
assert CharSet('lo').splitx('Hello') == ['He', 'llo']
assert CharSet('abc').splitx('Hello') == ['Hello',]
assert CharSet(' ').splitx('x y ') == ['x', ' ', 'y', ' ']
assert CharSet(' ').splitx(' x y ') == ['', ' ', 'x', ' ', 'y', ' ']
if HAVE_UNICODE:
assert CharSet('l').splitx(unicode('Hello')) == ['He', 'll', 'o']
assert CharSet('lo').splitx(unicode('Hello')) == ['He', 'llo']
assert CharSet('abc').splitx(unicode('Hello')) == ['Hello',]
assert CharSet(' ').splitx(unicode('x y ')) == ['x', ' ', 'y', ' ']
assert CharSet(' ').splitx(unicode(' x y ')) == ['', ' ', 'x', ' ', 'y', ' ']
assert CharSet(unicode('l')).splitx(unicode('Hello')) == ['He', 'll', 'o']
assert CharSet(unicode('lo')).splitx(unicode('Hello')) == ['He', 'llo']
assert CharSet(unicode('abc')).splitx(unicode('Hello')) == ['Hello',]
assert CharSet(unicode(' ')).splitx(unicode('x y ')) == ['x', ' ', 'y', ' ']
assert CharSet(unicode(' ')).splitx(unicode(' x y ')) == ['', ' ', 'x', ' ', 'y', ' ']
print 'CharSet() negative logic matching'
assert CharSet('0-9').contains('a') == 0
assert CharSet('^0-9').contains('a') == 1
assert CharSet('0-9').match('abc') == 0
assert CharSet('0-9').match('123abc') == 3
assert CharSet('0-9').match('abc123') == 0
assert CharSet('0-9').search('abc') == None
assert CharSet('0-9').search('123abc') == 0
assert CharSet('0-9').search('abc123') == 3
assert CharSet('^0-9').match('abc') == 3
assert CharSet('^0-9').match('123abc') == 0
assert CharSet('^0-9').match('abc123') == 3
assert CharSet('^0-9').search('abc') == 0
assert CharSet('^0-9').search('123abc') == 3
assert CharSet('^0-9').search('abc123') == 0
if HAVE_UNICODE:
assert CharSet('0-9').contains(unicode('a')) == 0
assert CharSet('^0-9').contains(unicode('a')) == 1
assert CharSet('0-9').match(unicode('abc')) == 0
assert CharSet('0-9').match(unicode('123abc')) == 3
assert CharSet('0-9').match(unicode('abc123')) == 0
assert CharSet('0-9').search(unicode('abc')) == None
assert CharSet('0-9').search(unicode('123abc')) == 0
assert CharSet('0-9').search(unicode('abc123')) == 3
assert CharSet('^0-9').match(unicode('abc')) == 3
assert CharSet('^0-9').match(unicode('123abc')) == 0
assert CharSet('^0-9').match(unicode('abc123')) == 3
assert CharSet('^0-9').search(unicode('abc')) == 0
assert CharSet('^0-9').search(unicode('123abc')) == 3
assert CharSet('^0-9').search(unicode('abc123')) == 0
assert CharSet(unicode('0-9')).contains(unicode('a')) == 0
assert CharSet(unicode('^0-9')).contains(unicode('a')) == 1
assert CharSet(unicode('0-9')).match(unicode('abc')) == 0
assert CharSet(unicode('0-9')).match(unicode('123abc')) == 3
assert CharSet(unicode('0-9')).match(unicode('abc123')) == 0
assert CharSet(unicode('0-9')).search(unicode('abc')) == None
assert CharSet(unicode('0-9')).search(unicode('123abc')) == 0
assert CharSet(unicode('0-9')).search(unicode('abc123')) == 3
assert CharSet(unicode('^0-9')).match(unicode('abc')) == 3
assert CharSet(unicode('^0-9')).match(unicode('123abc')) == 0
assert CharSet(unicode('^0-9')).match(unicode('abc123')) == 3
assert CharSet(unicode('^0-9')).search(unicode('abc')) == 0
assert CharSet(unicode('^0-9')).search(unicode('123abc')) == 3
assert CharSet(unicode('^0-9')).search(unicode('abc123')) == 0
print 'CharSet() pickling'
cs = CharSet('abc')
pcs = pickle.dumps(cs)
cs1 = pickle.loads(pcs)
assert cs.match('abc') == cs1.match('abc')
assert cs.match('') == cs1.match('')
assert cs.match('eee') == cs1.match('eee')
assert cs.match(' abc') == cs1.match(' abc')
assert cs.match('abc...d') == cs1.match('abc...d')
assert cs.search('xxxabc') == cs1.search('xxxabc')
###
htmltag = (
(None,Is,'<'),
# is this a closing tag ?
('closetag',Is,'/',+1),
# a coment ?
('comment',Is,'!','check-xmp-tag'),
(None,Word,'--',+4),
('text',WordStart,'-->',+1),
(None,Skip,3),
(None,Jump,To,MatchOk),
# a SGML-Tag ?
('other',AllNotIn,'>',+1),
(None,Is,'>'),
(None,Jump,To,MatchOk),
# XMP-Tag ?
'check-xmp-tag',
('tagname',Word,'xmp','get-tagname'),
(None,Is,'>'),
('text',WordStart,'</xmp>'),
(None,Skip,len('</xmp>')),
(None,Jump,To,MatchOk),
# get the tag name
'get-tagname',
('tagname',AllInCharSet,tagname_charset),
# look for attributes
'get-attributes',
(None,AllInCharSet,white_charset,'incorrect-attributes'),
(None,Is,'>',+1,MatchOk),
('tagattr',Table,tagattr),
(None,Jump,To,-3),
(None,Is,'>',+1,MatchOk),
# handle incorrect attributes
'incorrect-attributes',
(error,AllNotIn,'> \n\r\t'),
(None,Jump,To,'get-attributes')
)
print 'TagTable()'
htmltable_tt = TagTable(htmltable)
htmltag_tt = TagTable(htmltag)
if HAVE_UNICODE:
print 'UnicodeTagTable()'
utt = UnicodeTagTable(htmltag)
print 'TagTable() pickling'
ptt = pickle.dumps(htmltable_tt)
tt1 = pickle.loads(ptt)
print 'TextSearch() pickling'
pts = pickle.dumps(TextSearch('test'))
ts1 = pickle.loads(pts)
if 0:
print 'HTML Table:'
pprint.pprint(htmltable)
print 'TagTable .dump() version of the HTML table:'
pprint.pprint(htmltable_tt.dump())
###
print 'TextSearch() object (Boyer-Moore)'
ts = TextSearch('test')
ts = TextSearch('test', None)
ts = TextSearch('test', 'x'*256)
ts = TextSearch('test', None, BOYERMOORE)
ts = TextSearch('test')
assert ts.search(' test') == (4, 8), ts.search(' test')
assert ts.search(' test ') == (4, 8)
assert ts.search(' abc ') == (0, 0)
assert ts.find(' test') == 4, ts.find(' test')
assert ts.find(' test ') == 4
assert ts.find(' abd ') == -1
assert ts.findall(' test test ') == [(4, 8), (10, 14)]
assert ts.findall(' abc def ') == []
if HAVE_UNICODE:
try:
ts.search(unicode(' test'))
except TypeError:
pass
else:
raise AssertionError,'Boyer-Moore does not work with Unicode'
try:
ts.find(unicode(' test'))
except TypeError:
pass
else:
raise AssertionError,'Boyer-Moore does not work with Unicode'
try:
ts.findall(unicode(' test test '))
except TypeError:
pass
else:
raise AssertionError,'Boyer-Moore does not work with Unicode'
try:
ts = TextSearch('test', None, FASTSEARCH)
except ValueError:
pass
else:
print 'TextSearch() object (FastSearch)'
assert ts.search(' test') == (4, 8)
assert ts.search(' test ') == (4, 8)
assert ts.search(' abc ') == (0, 0)
assert ts.find(' test') == 4
assert ts.find(' test ') == 4
assert ts.find(' abd ') == -1
assert ts.findall(' test test ') == [(4, 8), (10, 14)]
assert ts.findall(' abc def ') == []
print 'TextSearch() object (Trivial)'
ts = TextSearch('test', algorithm=TRIVIAL)
assert ts.search(' test') == (4, 8)
assert ts.search(' test ') == (4, 8)
assert ts.search(' abc ') == (0, 0)
assert ts.find(' test') == 4
assert ts.find(' test ') == 4
assert ts.find(' abd ') == -1
assert ts.findall(' test test ') == [(4, 8), (10, 14)]
assert ts.findall(' abc def ') == []
if HAVE_UNICODE:
print 'TextSearch() object (Trivial; Unicode)'
assert ts.search(unicode(' test')) == (4, 8)
assert ts.search(unicode(' test ')) == (4, 8)
assert ts.search(unicode(' abc ')) == (0, 0)
assert ts.find(unicode(' test')) == 4
assert ts.find(unicode(' test ')) == 4
assert ts.find(unicode(' abd ')) == -1
assert ts.findall(unicode(' test test ')) == [(4, 8), (10, 14)]
assert ts.findall(unicode(' abc def ')) == []
ts = TextSearch(unicode('test'), algorithm=TRIVIAL)
assert ts.search(' test') == (4, 8)
assert ts.search(' test ') == (4, 8)
assert ts.search(' abc ') == (0, 0)
assert ts.find(' test') == 4
assert ts.find(' test ') == 4
assert ts.find(' abd ') == -1
assert ts.findall(' test test ') == [(4, 8), (10, 14)]
assert ts.findall(' abc def ') == []
assert ts.search(unicode(' test')) == (4, 8)
assert ts.search(unicode(' test ')) == (4, 8)
assert ts.search(unicode(' abc ')) == (0, 0)
assert ts.find(unicode(' test')) == 4
assert ts.find(unicode(' test ')) == 4
assert ts.find(unicode(' abd ')) == -1
assert ts.findall(unicode(' test test ')) == [(4, 8), (10, 14)]
assert ts.findall(unicode(' abc def ')) == []
ts = TextSearch(unicode('test'))
assert ts.algorithm == TRIVIAL
print 'is_whitespace()'
assert is_whitespace(' \t\r') == 1
assert is_whitespace(' 123 ') == 0
if HAVE_UNICODE:
assert is_whitespace(unicode(' \t\r')) == 1
assert is_whitespace(unicode(' 123 ')) == 0
print 'collapse()'
assert collapse('a\nb\nc') == 'a b c'
assert collapse('a\nb\nc', '-') == 'a-b-c'
if HAVE_UNICODE:
assert collapse(unicode('a\nb\nä','latin-1')) == unicode('a b ä','latin-1')
assert collapse(unicode('a\nb\nä','latin-1'), '-') == unicode('a-b-ä','latin-1')
print 'splitwords()'
assert splitwords('a b c') == ['a', 'b', 'c']
if HAVE_UNICODE:
assert splitwords(unicode('a b ä','latin-1')) == [ua, ub, unicode('ä','latin-1')]
print 'countlines()'
assert countlines('a\nb\nc') == 3
assert countlines('a\nb\nc\n') == 3
if HAVE_UNICODE:
assert countlines(unicode('a\nb\nä','latin-1')) == 3
print 'splitlines()'
assert splitlines('a\nb\r\nc') == ['a', 'b', 'c']
assert splitlines('a\nb\r\nc\r') == ['a', 'b', 'c']
if HAVE_UNICODE:
assert splitlines(unicode('a\nb\r\nä\r','latin-1')) == [ua, ub, unicode('ä','latin-1')]
print 'replace()'
assert replace('a\nb\nc', '\n', ' ') == 'a b c'
assert replace('a\nb\nc', '\n', '-') == 'a-b-c'
if HAVE_UNICODE:
assert replace(unicode('a\nb\nä','latin-1'), '\n', ' ') == unicode('a b ä','latin-1')
assert replace(unicode('a\nb\nä','latin-1'), '\n', '-') == unicode('a-b-ä','latin-1')
print 'multireplace()'
assert multireplace('a\nb\nc', [(' ', 1, 2)]) == 'a b\nc'
assert multireplace('a\nb\nc', [('-', 1, 2), ('-', 3, 4)]) == 'a-b-c'
if HAVE_UNICODE:
assert multireplace(unicode('a\nb\nä','latin-1'), [(' ', 1, 2)]) == unicode('a b\nä','latin-1')
assert multireplace(unicode('a\nb\nä','latin-1'), [('-', 1, 2), ('-', 3, 4)]) == unicode('a-b-ä','latin-1')
print 'quoted_split()'
assert quoted_split(' a, b ,\t c,d ,e ,"ab,cd,de" ,\'a,b\'', ',') == \
['a', 'b', 'c', 'd', 'e', 'ab,cd,de', 'a,b']
# twice to test table cache
assert quoted_split(' a, b ,\t c,d ,e ,"ab,cd,de" ,\'a,b\'', ',') == \
['a', 'b', 'c', 'd', 'e', 'ab,cd,de', 'a,b']
assert quoted_split(' a b \t c d e "ab cd de" \'a b\'') == \
['a', 'b', 'c', 'd', 'e', 'ab cd de', 'a b']
assert quoted_split(',,a', ',') == ['', '', 'a']
assert quoted_split(',,a,', ',') == ['', '', 'a', '']
if HAVE_UNICODE:
assert quoted_split(unicode(' a, b ,\t c,d ,e ,"ab,cd,de" ,\'a,b\''), ',') == \
[ua, ub, uc, ud, ue, unicode('ab,cd,de'), unicode('a,b')]
assert quoted_split(unicode(',,a'), ',') == [uempty, uempty, ua]
assert quoted_split(unicode(',,a,'), ',') == [uempty, uempty, ua, uempty]
# Clear the TagTable cache
tagtable_cache.clear()
break
print
print 'Works.'
|