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 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
|
# vim:fileencoding=utf-8
# Copyright (c) 2006-2021 Andrey Golovizin
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import absolute_import, unicode_literals
import re
from abc import ABCMeta, abstractmethod
from unittest import TestCase
import pytest
from pybtex import textutils
from pybtex.richtext import HRef, Protected, String, Symbol, Tag, Text, nbsp
class TextTestMixin(object):
__metaclass__ = ABCMeta
@abstractmethod
def test__init__(self):
raise NotImplementedError
@abstractmethod
def test__str__(self):
raise NotImplementedError
@abstractmethod
def test__eq__(self):
raise NotImplementedError
@abstractmethod
def test__len__(self):
raise NotImplementedError
@abstractmethod
def test__contains__(self):
raise NotImplementedError
@abstractmethod
def test__getitem__(self):
raise NotImplementedError
@abstractmethod
def test__add__(self):
raise NotImplementedError
@abstractmethod
def test_append(self):
raise NotImplementedError
@abstractmethod
def test_join(self):
raise NotImplementedError
@abstractmethod
def test_split(self):
raise NotImplementedError
@abstractmethod
def test_startswith(self):
raise NotImplementedError
@abstractmethod
def test_endswith(self):
raise NotImplementedError
@abstractmethod
def test_isalpha(self):
raise NotImplementedError
@abstractmethod
def test_capfirst(self):
raise NotImplementedError
@abstractmethod
def test_capitalize(self):
raise NotImplementedError
@abstractmethod
def test_add_period(self):
raise NotImplementedError
@abstractmethod
def test_lower(self):
raise NotImplementedError
@abstractmethod
def test_upper(self):
raise NotImplementedError
@abstractmethod
def test_render_as(self):
raise NotImplementedError
class TestText(TextTestMixin, TestCase):
def test__init__(self):
assert str(Text('a', '', 'c')) == 'ac'
assert str(Text('a', Text(), 'c')) == 'ac'
text = Text(Text(), Text('mary ', 'had ', 'a little lamb'))
assert str(text) == 'mary had a little lamb'
text = str(Text('a', Text('b', 'c'), Tag('em', 'x'), Symbol('nbsp'), 'd'))
assert text == 'abcx<nbsp>d'
with pytest.raises(ValueError):
Text({})
with pytest.raises(ValueError):
Text(0, 0)
def test__eq__(self):
assert Text() == Text()
assert not (Text() != Text())
assert Text('Cat') == Text('Cat')
assert not (Text('Cat') != Text('Cat'))
assert Text('Cat', ' tail') == Text('Cat tail')
assert not (Text('Cat', ' tail') != Text('Cat tail'))
assert Text('Cat') != Text('Dog')
assert not (Text('Cat') == Text('Dog'))
def test__len__(self):
assert len(Text()) == 0
assert len(Text('Never', ' ', 'Knows', ' ', 'Best')) == len('Never Knows Best')
assert len(Text('Never', ' ', Tag('em', 'Knows', ' '), 'Best')) == len('Never Knows Best')
assert len(Text('Never', ' ', Tag('em', HRef('/', 'Knows'), ' '), 'Best')) == len('Never Knows Best')
def test__str__(self):
assert str(Text()) == ''
assert str(Text(u'Чудаки украшают мир')) == u'Чудаки украшают мир'
def test__contains__(self):
text = Text('mary ', 'had ', 'a little lamb')
assert 'mary' in text
assert not 'Mary' in text
assert 'had a little' in text
text = Text('a', 'b', 'c')
assert 'abc' in text
def test_capfirst(self):
text = Text('dear ', 'Alice')
assert str(text.capfirst()) == 'Dear Alice'
def test_capitalize(self):
text = Text('mary ', 'had ', 'a Little Lamb')
assert str(text.capitalize()) == 'Mary had a little lamb'
def test__add__(self):
t = Text('a')
assert str(t + 'b') == 'ab'
assert str(t + t) == 'aa'
assert str(t) == 'a'
def test__getitem__(self):
t = Text('123', Text('456', Text('78'), '9'), '0')
with pytest.raises(TypeError):
1 in t
assert t == Text('1234567890')
assert t[:0] == Text('')
assert t[:1] == Text('1')
assert t[:3] == Text('123')
assert t[:5] == Text('12345')
assert t[:7] == Text('1234567')
assert t[:10] == Text('1234567890')
assert t[:100] == Text('1234567890')
assert t[:-100] == Text('')
assert t[:-10] == Text('')
assert t[:-9] == Text('1')
assert t[:-7] == Text('123')
assert t[:-5] == Text('12345')
assert t[:-3] == Text('1234567')
assert t[-100:] == Text('1234567890')
assert t[-10:] == Text('1234567890')
assert t[-9:] == Text('234567890')
assert t[-7:] == Text('4567890')
assert t[-5:] == Text('67890')
assert t[-3:] == Text('890')
assert t[1:] == Text('234567890')
assert t[3:] == Text('4567890')
assert t[5:] == Text('67890')
assert t[7:] == Text('890')
assert t[10:] == Text('')
assert t[100:] == Text('')
assert t[0:10] == Text('1234567890')
assert t[0:100] == Text('1234567890')
assert t[2:3] == Text('3')
assert t[2:4] == Text('34')
assert t[3:7] == Text('4567')
assert t[4:7] == Text('567')
assert t[4:7] == Text('567')
assert t[7:9] == Text('89')
assert t[100:200] == Text('')
t = Text('123', Tag('em', '456', HRef('/', '789')), '0')
assert t[:3] == Text('123')
assert t[:5] == Text('123', Tag('em', '45'))
assert t[:7] == Text('123', Tag('em', '456', HRef('/', '7')))
assert t[:10] == Text('123', Tag('em', '456', HRef('/', '789')), '0')
assert t[:100] == Text('123', Tag('em', '456', HRef('/', '789')), '0')
assert t[:-7] == Text('123')
assert t[:-5] == Text('123', Tag('em', '45'))
assert t[:-3] == Text('123', Tag('em', '456', HRef('/', '7')))
def test_append(self):
text = Tag('strong', 'Chuck Norris')
assert (text + ' wins!').render_as('html') == '<strong>Chuck Norris</strong> wins!'
assert text.append(' wins!').render_as('html') == '<strong>Chuck Norris wins!</strong>'
text = HRef('/', 'Chuck Norris')
assert (text + ' wins!').render_as('html') == '<a href="/">Chuck Norris</a> wins!'
assert text.append(' wins!').render_as('html') == '<a href="/">Chuck Norris wins!</a>'
def test_upper(self):
text = Text('mary ', 'had ', 'a little lamb')
assert str(text.upper()) == 'MARY HAD A LITTLE LAMB'
def test_lower(self):
text = Text('mary ', 'had ', 'a little lamb')
assert str(text.lower()) == 'mary had a little lamb'
def test_startswith(self):
assert not Text().startswith('.')
assert not Text().startswith(('.', '!'))
text = Text('mary ', 'had ', 'a little lamb')
assert not text.startswith('M')
assert text.startswith('m')
text = Text('a', 'b', 'c')
assert text.startswith('ab')
assert Text('This is good').startswith(('This', 'That'))
assert not Text('This is good').startswith(('That', 'Those'))
def test_endswith(self):
assert not Text().endswith('.')
assert not Text().endswith(('.', '!'))
text = Text('mary ', 'had ', 'a little lamb')
assert not text.endswith('B')
assert text.endswith('b')
text = Text('a', 'b', 'c')
assert text.endswith('bc')
assert Text('This is good').endswith(('good', 'wonderful'))
assert not Text('This is good').endswith(('bad', 'awful'))
def test_isalpha(self):
assert not Text().isalpha()
assert not Text('a b c').isalpha()
assert Text('abc').isalpha()
assert Text(u'文字').isalpha()
assert Text('ab', Tag('em', 'cd'), 'ef').isalpha()
assert not Text('ab', Tag('em', '12'), 'ef').isalpha()
def test_join(self):
assert str(Text(' ').join(['a', Text('b c')])) == 'a b c'
assert str(Text(nbsp).join(['a', 'b', 'c'])) == 'a<nbsp>b<nbsp>c'
assert str(nbsp.join(['a', 'b', 'c'])) == 'a<nbsp>b<nbsp>c'
assert str(String('-').join(['a', 'b', 'c'])) == 'a-b-c'
result = Tag('em', ' and ').join(['a', 'b', 'c']).render_as('html')
assert result == 'a<em> and </em>b<em> and </em>c'
result = HRef('/', ' and ').join(['a', 'b', 'c']).render_as('html')
assert result == 'a<a href="/"> and </a>b<a href="/"> and </a>c'
def test_split(self):
assert Text().split() == []
assert Text().split('abc') == [Text()]
assert Text('a').split() == [Text('a')]
assert Text('a ').split() == [Text('a')]
assert Text(' a ').split() == [Text('a')]
assert Text('a + b').split() == [Text('a'), Text('+'), Text('b')]
assert Text('a + b').split(' + ') == [Text('a'), Text('b')]
assert Text('a + b').split(re.compile(r'\s')) == [Text('a'), Text('+'), Text('b')]
assert Text('abc').split('xyz') == [Text('abc')]
assert Text('---').split('--') == [Text(), Text('-')]
assert Text('---').split('-') == [Text(), Text(), Text(), Text()]
def test_add_period(self):
assert Text().endswith(('.', '!', '?')) == False
assert textutils.is_terminated(Text()) == False
assert str(Text().add_period()) == ''
text = Text("That's all, folks")
assert str(text.add_period()) == "That's all, folks."
def test_render_as(self):
string = Text(u'Detektivbyrån & friends')
assert string.render_as('text') == u'Detektivbyrån & friends'
assert string.render_as('html') == u'Detektivbyrån & friends'
class TestString(TextTestMixin, TestCase):
def test__init__(self):
assert String().value == ''
assert String('').value == ''
assert String('Wa', '', 'ke', ' ', 'up!').value == 'Wake up!'
def test__eq__(self):
assert String() != ''
assert String('') != ''
assert String() == String()
assert String('') == String()
assert String('', '', '') == String()
assert String('Wa', '', 'ke', ' ', 'up!') == String('Wake up!')
def test__len__(self):
assert len(String()) == len(String('')) == 0
val = 'test string'
assert len(String(val)) == len(val)
def test__str__(self):
val = u'Detektivbyrån'
assert str(String(val)) == val
def test__contains__(self):
assert '' in String()
assert 'abc' not in String()
assert '' in String(' ')
assert ' + ' in String('2 + 2')
def test__getitem__(self):
digits = String('0123456789')
assert digits[0] != '0'
assert digits[0] == String('0')
def test__add__(self):
assert String('Python') + String(' 3') != 'Python 3'
assert String('Python') + String(' 3') == Text('Python 3')
assert String('A').lower() == String('a')
assert str(String('Python') + String(' ') + String('3')) == 'Python 3'
assert str(String('Python') + Text(' ') + String('3')) == 'Python 3'
assert str(String('Python') + ' ' + '3') == 'Python 3'
assert str(String('Python').append(' 3')) == 'Python 3'
def test_startswith(self):
assert not String().startswith('n')
assert not String('').startswith('n')
assert not String().endswith('n')
assert not String('').endswith('n')
assert not String('November.').startswith('n')
assert String('November.').startswith('N')
def test_endswith(self):
assert not String().endswith('.')
assert not String().endswith(('.', '!'))
assert not String('November.').endswith('r')
assert String('November.').endswith('.')
assert String('November.').endswith(('.', '!'))
assert not String('November.').endswith(('?', '!'))
def test_isalpha(self):
assert not String().isalpha()
assert not String('a b c').isalpha()
assert String('abc').isalpha()
assert String(u'文字').isalpha()
def test_append(self):
assert String().append('') == Text()
text = String('The').append(' Adventures of ').append('Tom Sawyer')
assert text == Text('The Adventures of Tom Sawyer')
def test_lower(self):
assert String('').lower() == String()
assert String('A').lower() == String('a')
assert String('November').lower() == String('november')
def test_upper(self):
assert String('').upper() == String()
assert String('a').upper() == String('A')
assert String('November').upper() == String('NOVEMBER')
def test_split(self):
assert String().split() == []
assert String().split('abc') == [String('')]
assert String('a').split() == [String('a')]
assert String('a ').split() == [String('a')]
assert String('a + b').split() == [String('a'), String('+'), String('b')]
assert String('a + b').split(' + ') == [String('a'), String('b')]
assert String('a + b').split(re.compile(r'\s')) == [String('a'), String('+'), String('b')]
def test_join(self):
assert String().join([]) == Text()
assert String('nothing to see here').join([]) == Text()
assert String().join(['a', 'b', 'c']) == Text('abc')
assert String(', ').join(['tomatoes']) == Text('tomatoes')
assert String(', ').join(['tomatoes', 'cucumbers']) == Text('tomatoes, cucumbers')
assert String(', ').join(['tomatoes', 'cucumbers', 'lemons']) == Text('tomatoes, cucumbers, lemons')
def test_capfirst(self):
assert str(String('').capitalize()) == ''
assert str(String('november december').capitalize()) == 'November december'
assert str(String('November December').capitalize()) == 'November december'
assert str(String('NOVEMBER DECEMBER').capitalize()) == 'November december'
def test_capitalize(self):
assert str(String('').capfirst()) == ''
assert str(String('november').capfirst()) == 'November'
assert str(String('November').capfirst()) == 'November'
assert str(String('november december').capfirst()) == 'November december'
assert str(String('November December').capfirst()) == 'November December'
assert str(String('NOVEMBER DECEMBER').capfirst()) == 'NOVEMBER DECEMBER'
def test_add_period(self):
assert str(String('').add_period()) == ''
assert str(String('').add_period('!')) == ''
assert str(String('').add_period().add_period()) == ''
assert str(String('').add_period().add_period('!')) == ''
assert str(String('').add_period('!').add_period()) == ''
str(String('November').add_period()) == 'November.'
result = str(String('November').add_period().add_period())
assert result == 'November.'
def test_render_as(self):
string = String(u'Detektivbyrån & friends')
assert string.render_as('text') == u'Detektivbyrån & friends'
assert string.render_as('html') == u'Detektivbyrån & friends'
class TestTag(TextTestMixin, TestCase):
def test__init__(self):
empty = Tag('em')
assert str(empty) == ''
text = Text('This ', Tag('em', 'is'), ' good')
assert 'This is' in str(text)
assert str(text).startswith('This is')
assert str(text).endswith('is good')
def test__eq__(self):
assert Tag('em', '') != ''
assert Tag('em', '') != Text()
assert Tag('em', '') != Tag('strong', '')
assert Tag('em', '') == Tag('em', '')
assert Tag('em', 'good') != Tag('em', 'bad')
assert Tag('em', 'good') != Text('good')
assert Tag('em', 'good') == Tag('em', 'good')
assert not (Tag('em', 'good') != Tag('em', 'good'))
def test__len__(self):
val = 'Tomato apple!'
assert len(Tag('em', val)) == len(val)
def test__str__(self):
empty = Tag('em')
assert str(empty.lower()) == ''
assert str(empty.capitalize()) == ''
assert str(empty.add_period()) == ''
assert str(Tag('strong', u'ねここねこ')) == u'ねここねこ'
def test__contains__(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert 'mary' in tag
assert 'Mary' not in tag
assert 'had a little' in tag
text = Text('This ', Tag('em', 'is'), ' good')
assert not 'This is' in text
def test__getitem__(self):
t = Tag('em', '1234567890')
with pytest.raises(TypeError):
1 in t
assert t == Tag('em', '1234567890')
assert t[:] == t
assert t[:0] == Tag('em', '')
assert t[:1] == Tag('em', '1')
assert t[:3] == Tag('em', '123')
assert t[:5] == Tag('em', '12345')
assert t[:7] == Tag('em', '1234567')
assert t[:10] == Tag('em', '1234567890')
assert t[:100] == Tag('em', '1234567890')
assert t[:-100] == Tag('em', '')
assert t[:-10] == Tag('em', '')
assert t[:-9] == Tag('em', '1')
assert t[:-7] == Tag('em', '123')
assert t[:-5] == Tag('em', '12345')
assert t[:-3] == Tag('em', '1234567')
assert t[-100:] == Tag('em', '1234567890')
assert t[-10:] == Tag('em', '1234567890')
assert t[-9:] == Tag('em', '234567890')
assert t[-7:] == Tag('em', '4567890')
assert t[-5:] == Tag('em', '67890')
assert t[-3:] == Tag('em', '890')
assert t[1:] == Tag('em', '234567890')
assert t[3:] == Tag('em', '4567890')
assert t[5:] == Tag('em', '67890')
assert t[7:] == Tag('em', '890')
assert t[10:] == Tag('em', '')
assert t[100:] == Tag('em', '')
assert t[0:10] == Tag('em', '1234567890')
assert t[0:100] == Tag('em', '1234567890')
assert t[2:3] == Tag('em', '3')
assert t[2:4] == Tag('em', '34')
assert t[3:7] == Tag('em', '4567')
assert t[4:7] == Tag('em', '567')
assert t[4:7] == Tag('em', '567')
assert t[7:9] == Tag('em', '89')
assert t[100:200] == Tag('em', '')
t = Tag('strong', '123', Tag('em', '456', HRef('/', '789')), '0')
assert t[:3] == Tag('strong', '123')
assert t[:5] == Tag('strong', '123', Tag('em', '45'))
assert t[:7] == Tag('strong', '123', Tag('em', '456', HRef('/', '7')))
assert t[:10] == Tag('strong', '123', Tag('em', '456', HRef('/', '789')), '0')
assert t[:100] == Tag('strong', '123', Tag('em', '456', HRef('/', '789')), '0')
assert t[:-7] == Tag('strong', '123')
assert t[:-5] == Tag('strong', '123', Tag('em', '45'))
assert t[:-3] == Tag('strong', '123', Tag('em', '456', HRef('/', '7')))
def test__add__(self):
assert Tag('em', '') + Tag('em', '') == Text(Tag('em', ''))
assert Tag('em', '') + Tag('strong', '') == Text(Tag('em', ''), Tag('strong', ''))
assert Tag('em', 'Good') + Tag('em', '') == Text(Tag('em', 'Good'))
assert Tag('em', 'Good') + Tag('em', ' job!') == Text(Tag('em', 'Good job!'))
assert Tag('em', 'Good') + Tag('strong', ' job!') == Text(Tag('em', 'Good'), Tag('strong', ' job!'))
assert Tag('em', 'Good') + Text(' job!') == Text(Tag('em', 'Good'), ' job!')
assert Text('Good') + Tag('em', ' job!') == Text('Good', Tag('em', ' job!'))
def test_upper(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert tag.upper().render_as('html') == '<em>MARY HAD A LITTLE LAMB</em>'
def test_lower(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert tag.lower().render_as('html') == '<em>mary had a little lamb</em>'
def test_capfirst(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a Little Lamb'))
assert tag.capfirst().render_as('html') == '<em>Mary had a Little Lamb</em>'
def test_capitalize(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a Little Lamb'))
assert tag.capitalize().render_as('html') == '<em>Mary had a little lamb</em>'
def test_startswith(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert not tag.startswith('M')
assert tag.startswith('m')
tag = Tag('em', 'a', 'b', 'c')
assert tag.startswith('ab')
tag = Tag('em', 'This is good')
assert tag.startswith(('This', 'That'))
assert not tag.startswith(('That', 'Those'))
text = Text('This ', Tag('em', 'is'), ' good')
assert not text.startswith('This is')
def test_isalpha(self):
assert not Tag('em').isalpha()
assert not Tag('em', 'a b c').isalpha()
assert Tag('em', 'abc').isalpha()
assert Tag('em', u'文字').isalpha()
def test_endswith(self):
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert not tag.endswith('B')
assert tag.endswith('b')
tag = Tag('em', 'a', 'b', 'c')
assert tag.endswith('bc')
tag = Tag('em', 'This is good')
assert tag.endswith(('good', 'wonderful'))
assert not tag.endswith(('bad', 'awful'))
text = Text('This ', Tag('em', 'is'), ' good')
assert not text.endswith('is good')
def test_split(self):
empty = Tag('em')
assert empty.split() == []
assert empty.split('abc') == [Tag('em')]
em = Tag('em', 'Emphasized text')
assert em.split() == [Tag('em', 'Emphasized'), Tag('em', 'text')]
assert em.split(' ') == [Tag('em', 'Emphasized'), Tag('em', 'text')]
assert em.split('no such text') == [em]
text = Text('Bonnie ', Tag('em', 'and'), ' Clyde')
assert text.split('and') == [Text('Bonnie '), Text(' Clyde')]
assert text.split(' and ') == [text]
text = Text('Bonnie', Tag('em', ' and '), 'Clyde')
assert text.split('and') == [Text('Bonnie', Tag('em', ' ')), Text(Tag('em', ' '), 'Clyde')]
assert text.split(' and ') == [Text('Bonnie'), Text('Clyde')]
text = Text('From ', Tag('em', 'the very beginning'), ' of things')
assert text.split() == [
Text('From'), Text(Tag('em', 'the')), Text(Tag('em', 'very')),
Text(Tag('em', 'beginning')), Text('of'), Text('things'),
]
parts = text.split()
assert parts == [
Text('From'),
Text(Tag('em', 'the')),
Text(Tag('em', 'very')),
Text(Tag('em', 'beginning')),
Text('of'), Text('things'),
]
def test_join(self):
text = Text('From ', Tag('em', 'the very beginning'), ' of things')
dashified = String('-').join(text.split())
assert dashified == Text('From-', Tag('em', 'the'), '-', Tag('em', 'very'), '-', Tag('em', 'beginning'), '-of-things')
dashified = Tag('em', '-').join(text.split())
assert dashified == Text('From', Tag('em', '-the-very-beginning-'), 'of', Tag('em', '-'), 'things')
def test_append(self):
text = Tag('strong', 'Chuck Norris')
assert (text + ' wins!').render_as('html') == '<strong>Chuck Norris</strong> wins!'
assert text.append(' wins!').render_as('html') == '<strong>Chuck Norris wins!</strong>'
text = Tag('em', 'Look here')
assert (text + '!').render_as('html') == '<em>Look here</em>!'
assert text.append('!').render_as('html') == '<em>Look here!</em>'
def test_add_period(self):
text = Tag('em', Text("That's all, folks"))
assert text.add_period().render_as('html') == "<em>That's all, folks.</em>"
assert text.add_period().add_period().render_as('html') == "<em>That's all, folks.</em>"
text = Text("That's all, ", Tag('em', 'folks'))
assert text.add_period().render_as('html') == "That's all, <em>folks</em>."
assert text.add_period().add_period().render_as('html') == "That's all, <em>folks</em>."
text = Text("That's all, ", Tag('em', 'folks.'))
assert text.add_period().render_as('html') == "That's all, <em>folks.</em>"
text = Text("That's all, ", Tag('em', 'folks'))
assert text.add_period('!').render_as('html') == "That's all, <em>folks</em>!"
text = text.add_period('!').add_period('.').render_as('html')
assert text == "That's all, <em>folks</em>!"
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert tag.add_period().render_as('html') == '<em>mary had a little lamb.</em>'
assert tag.add_period().add_period().render_as('html') == '<em>mary had a little lamb.</em>'
def test_render_as(self):
empty = Tag('em')
assert empty.render_as('html') == ''
assert empty.render_as('latex') == ''
tag = Tag('em', 'a', 'b')
assert tag.render_as('html') == '<em>ab</em>'
assert tag.render_as('latex') == '\\emph{ab}'
em = Tag('em', 'Emphasized text')
assert em.render_as('latex') == '\\emph{Emphasized text}'
assert em.upper().render_as('latex') == '\\emph{EMPHASIZED TEXT}'
assert em.lower().render_as('latex') == '\\emph{emphasized text}'
assert em.render_as('html') == '<em>Emphasized text</em>'
t = Tag(u'em', u'123', Tag(u'em', u'456', Text(u'78'), u'9'), u'0')
assert t[:2].render_as('html') == '<em>12</em>'
assert t[2:4].render_as('html') == '<em>3<em>4</em></em>'
tag = Tag('em', Text(), Text('mary ', 'had ', 'a little lamb'))
assert tag.render_as('html') == '<em>mary had a little lamb</em>'
class TestHRef(TextTestMixin, TestCase):
def test__init__(self):
empty = HRef('/')
assert empty.url == '/'
assert empty.parts == []
def test__str__(self):
empty = HRef('/')
assert str(empty) == ''
text = Text('This ', HRef('/', 'is'), ' good')
str(text) == 'This is good'
def test__eq__(self):
assert HRef('/', '') != ''
assert HRef('/', '') != Text()
assert HRef('/', '') != HRef('', '')
assert HRef('/', '') == HRef('/', '')
assert HRef('/', 'good') != HRef('', 'bad')
assert HRef('/', 'good') != Text('good')
assert HRef('/', 'good') == HRef('/', 'good')
assert not (HRef('/', 'good') != HRef('/', 'good'))
assert HRef('strong', '') != Tag('strong', '')
def test__len__(self):
val = 'Tomato apple!'
assert len(HRef('index', val)) == len(val)
def test__contains__(self):
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert not 'mary' in tag
assert 'Mary' in tag
assert 'had a little' in tag
text = Text('This ', HRef('/', 'is'), ' good')
assert not 'This is' in text
def test__getitem__(self):
t = HRef('/', '1234567890')
with pytest.raises(TypeError):
1 in t
assert t == HRef('/', '1234567890')
assert t[:] == t
assert t[:0] == HRef('/', '')
assert t[:1] == HRef('/', '1')
assert t[:3] == HRef('/', '123')
assert t[:5] == HRef('/', '12345')
assert t[:7] == HRef('/', '1234567')
assert t[:10] == HRef('/', '1234567890')
assert t[:100] == HRef('/', '1234567890')
assert t[:-100] == HRef('/', '')
assert t[:-10] == HRef('/', '')
assert t[:-9] == HRef('/', '1')
assert t[:-7] == HRef('/', '123')
assert t[:-5] == HRef('/', '12345')
assert t[:-3] == HRef('/', '1234567')
assert t[-100:] == HRef('/', '1234567890')
assert t[-10:] == HRef('/', '1234567890')
assert t[-9:] == HRef('/', '234567890')
assert t[-7:] == HRef('/', '4567890')
assert t[-5:] == HRef('/', '67890')
assert t[-3:] == HRef('/', '890')
assert t[1:] == HRef('/', '234567890')
assert t[3:] == HRef('/', '4567890')
assert t[5:] == HRef('/', '67890')
assert t[7:] == HRef('/', '890')
assert t[10:] == HRef('/', '')
assert t[100:] == HRef('/', '')
assert t[0:10] == HRef('/', '1234567890')
assert t[0:100] == HRef('/', '1234567890')
assert t[2:3] == HRef('/', '3')
assert t[2:4] == HRef('/', '34')
assert t[3:7] == HRef('/', '4567')
assert t[4:7] == HRef('/', '567')
assert t[4:7] == HRef('/', '567')
assert t[7:9] == HRef('/', '89')
assert t[100:200] == HRef('/', '')
t = HRef('', '123', HRef('/', '456', HRef('/', '789')), '0')
assert t[:3] == HRef('', '123')
assert t[:5] == HRef('', '123', HRef('/', '45'))
assert t[:7] == HRef('', '123', HRef('/', '456', HRef('/', '7')))
assert t[:10] == HRef('', '123', HRef('/', '456', HRef('/', '789')), '0')
assert t[:100] == HRef('', '123', HRef('/', '456', HRef('/', '789')), '0')
assert t[:-7] == HRef('', '123')
assert t[:-5] == HRef('', '123', HRef('/', '45'))
assert t[:-3] == HRef('', '123', HRef('/', '456', HRef('/', '7')))
def test__add__(self):
assert HRef('/', '') + HRef('/', '') == Text(HRef('/', ''))
assert HRef('/', '') + HRef('strong', '') == Text(HRef('/', ''), HRef('strong', ''))
assert HRef('/', 'Good') + HRef('/', '') == Text(HRef('/', 'Good'))
assert HRef('/', 'Good') + HRef('/', ' job!') == Text(HRef('/', 'Good job!'))
assert HRef('/', 'Good') + HRef('strong', ' job!') == Text(HRef('/', 'Good'), HRef('strong', ' job!'))
assert HRef('/', 'Good') + Text(' job!') == Text(HRef('/', 'Good'), ' job!')
assert Text('Good') + HRef('/', ' job!') == Text('Good', HRef('/', ' job!'))
def test_append(self):
text = HRef('/', 'Chuck Norris')
assert (text + ' wins!').render_as('html') == '<a href="/">Chuck Norris</a> wins!'
assert text.append(' wins!').render_as('html') == '<a href="/">Chuck Norris wins!</a>'
def test_lower(self):
assert HRef('/').lower() == HRef('/')
href = HRef('http://www.example.com', 'Hyperlinked text.')
assert href.lower().render_as('latex') == '\\href{http://www.example.com}{hyperlinked text.}'
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert tag.lower().render_as('html') == '<a href="info.html">mary had a little lamb</a>'
def test_upper(self):
assert HRef('/').upper() == HRef('/')
href = HRef('http://www.example.com', 'Hyperlinked text.')
assert href.upper().render_as('latex') == '\\href{http://www.example.com}{HYPERLINKED TEXT.}'
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert tag.upper().render_as('html') == '<a href="info.html">MARY HAD A LITTLE LAMB</a>'
def test_capfirst(self):
assert HRef('/').capfirst() == Text(HRef('/'))
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a Little Lamb'))
assert tag.capfirst().render_as('html') == '<a href="info.html">Mary had a Little Lamb</a>'
assert tag.lower().capfirst().render_as('html') == '<a href="info.html">Mary had a little lamb</a>'
def test_capitalize(self):
assert HRef('/').capitalize() == Text(HRef('/'))
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a Little Lamb'))
assert tag.capitalize().render_as('html') == '<a href="info.html">Mary had a little lamb</a>'
assert tag.lower().capitalize().render_as('html') == '<a href="info.html">Mary had a little lamb</a>'
def test_add_period(self):
assert HRef('/').add_period() == HRef('/')
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert tag.add_period().render_as('html') == '<a href="info.html">Mary had a little lamb.</a>'
assert tag.add_period().add_period().render_as('html') == '<a href="info.html">Mary had a little lamb.</a>'
def test_split(self):
empty = HRef('/')
assert empty.split() == []
assert empty.split('abc') == [empty]
href = HRef('/', 'World Wide Web')
assert href.split() == [HRef('/', 'World'), HRef('/', 'Wide'), HRef('/', 'Web')]
result = Text('Estimated size of the ', href).split()
assert result == [
Text('Estimated'),
Text('size'),
Text('of'),
Text('the'),
Text(HRef('/', 'World')),
Text(HRef('/', 'Wide')),
Text(HRef('/', 'Web')),
]
text = Text(Tag('em', Text(Tag('strong', HRef('/', ' Very, very'), ' bad'), ' guys')), '! ')
assert text.render_as('html') == '<em><strong><a href="/"> Very, very</a> bad</strong> guys</em>! '
assert text.split(', ') == [
Text(Tag('em', Tag('strong', HRef('/', ' Very')))),
Text(Tag('em', Tag('strong', HRef('/', 'very'), ' bad'), ' guys'), '! '),
]
assert text.split(' ') == [
Text(),
Text(),
Text(Tag('em', Tag('strong', HRef('/', 'Very,')))),
Text(Tag('em', Tag('strong', HRef('/', 'very')))),
Text(Tag('em', Tag('strong', 'bad'))),
Text(Tag('em', 'guys'), '!'),
Text(),
]
assert text.split(' ', keep_empty_parts=False) == [
Text(Tag('em', Tag('strong', HRef('/', 'Very,')))),
Text(Tag('em', Tag('strong', HRef('/', 'very')))),
Text(Tag('em', Tag('strong', 'bad'))),
Text(Tag('em', 'guys'), '!'),
]
assert text.split() == [
Text(Tag('em', Tag('strong', HRef('/', 'Very,')))),
Text(Tag('em', Tag('strong', HRef('/', 'very')))),
Text(Tag('em', Tag('strong', 'bad'))),
Text(Tag('em', 'guys'), '!'),
]
assert text.split(keep_empty_parts=True) == [
Text(),
Text(Tag('em', Tag('strong', HRef('/', 'Very,')))),
Text(Tag('em', Tag('strong', HRef('/', 'very')))),
Text(Tag('em', Tag('strong', 'bad'))),
Text(Tag('em', 'guys'), '!'),
Text(),
]
text = Text(' A', Tag('em', ' big', HRef('/', ' ', Tag('strong', 'no-no'), '! ')))
assert text.render_as('html') == ' A<em> big<a href="/"> <strong>no-no</strong>! </a></em>'
assert text.split('-') == [
Text(' A', Tag('em', ' big', HRef('/', ' ', Tag('strong', 'no')))),
Text(Tag('em', HRef('/', Tag('strong', 'no'), '! '))),
]
assert text.split(' ') == [
Text(),
Text('A'),
Text(Tag('em', 'big')),
Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))),
Text(),
Text(),
]
assert text.split(' ', keep_empty_parts=False) == [
Text('A'),
Text(Tag('em', 'big')),
Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))),
]
assert text.split() == [
Text('A'),
Text(Tag('em', 'big')),
Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))),
]
assert text.split(keep_empty_parts=True) == [
Text(),
Text('A'),
Text(Tag('em', 'big')),
Text(Tag('em', HRef('/', Tag('strong', 'no-no'), '!'))),
Text(),
]
def test_join(self):
href = HRef('/', 'World Wide Web')
result = Text('-').join(Text('Estimated size of the ', href).split())
assert result == Text('Estimated-size-of-the-', HRef('/', 'World'), '-', HRef('/', 'Wide'), '-', HRef('/', 'Web'))
def test_startswith(self):
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert tag.startswith('')
assert tag.startswith('M')
assert tag.startswith('Mary')
assert not tag.startswith('m')
assert not tag.startswith('mary')
tag = HRef('/', 'a', 'b', 'c')
assert tag.startswith('ab')
tag = HRef('/', 'This is good')
assert tag.startswith(('This', 'That'))
assert not tag.startswith(('That', 'Those'))
text = Text('This ', HRef('/', 'is'), ' good')
assert not text.startswith('This is')
def test_endswith(self):
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert tag.endswith('')
assert not tag.endswith('B')
assert tag.endswith('b')
assert tag.endswith('lamb')
tag = HRef('/', 'a', 'b', 'c')
assert tag.endswith('bc')
tag = HRef('/', 'This is good')
assert tag.endswith(('good', 'wonderful'))
assert not tag.endswith(('bad', 'awful'))
text = Text('This ', HRef('/', 'is'), ' good')
assert not text.endswith('is good')
def test_isalpha(self):
assert not HRef('/').isalpha()
assert not HRef('/', 'a b c').isalpha()
assert HRef('/', 'abc').isalpha()
assert HRef('/', u'文字').isalpha()
def test_render_as(self):
href = HRef('http://www.example.com', 'Hyperlinked text.')
assert href.render_as('latex') == '\\href{http://www.example.com}{Hyperlinked text.}'
assert href.render_as('html') == '<a href="http://www.example.com">Hyperlinked text.</a>'
assert href.render_as('plaintext') == 'Hyperlinked text.'
tag = HRef('info.html', Text(), Text('Mary ', 'had ', 'a little lamb'))
assert tag.render_as('html') == '<a href="info.html">Mary had a little lamb</a>'
class TestProtected(TextTestMixin, TestCase):
def test__init__(self):
assert str(Protected('a', '', 'c')) == 'ac'
assert str(Protected('a', Text(), 'c')) == 'ac'
text = Protected(Protected(), Protected('mary ', 'had ', 'a little lamb'))
assert text == Protected(Protected('mary had a little lamb'))
assert str(text) == 'mary had a little lamb'
text = str(Protected('a', Protected('b', 'c'), Tag('em', 'x'), Symbol('nbsp'), 'd'))
assert text == 'abcx<nbsp>d'
with pytest.raises(ValueError):
Protected({})
with pytest.raises(ValueError):
Protected(0, 0)
def test__eq__(self):
assert Protected() == Protected()
assert not (Protected() != Protected())
assert Protected('Cat') == Protected('Cat')
assert not (Protected('Cat') != Protected('Cat'))
assert Protected('Cat', ' tail') == Protected('Cat tail')
assert not (Protected('Cat', ' tail') != Protected('Cat tail'))
assert Protected('Cat') != Protected('Dog')
assert not (Protected('Cat') == Protected('Dog'))
def test__len__(self):
assert len(Protected()) == 0
assert len(Protected('Never', ' ', 'Knows', ' ', 'Best')) == len('Never Knows Best')
assert len(Protected('Never', ' ', Tag('em', 'Knows', ' '), 'Best')) == len('Never Knows Best')
assert len(Protected('Never', ' ', Tag('em', HRef('/', 'Knows'), ' '), 'Best')) == len('Never Knows Best')
def test__str__(self):
assert str(Protected()) == ''
assert str(Protected(u'Чудаки украшают мир')) == u'Чудаки украшают мир'
def test__contains__(self):
text = Protected('mary ', 'had ', 'a little lamb')
assert 'mary' in text
assert 'Mary' not in text
assert 'had a little' in text
text = Protected('a', 'b', 'c')
assert 'abc' in text
def test_capfirst(self):
text = Protected('mary ', 'had ', 'a Little Lamb')
assert str(text.capitalize()) == 'mary had a Little Lamb'
def test_capitalize(self):
text = Protected('mary ', 'had ', 'a little lamb')
assert str(text.capitalize()) == 'mary had a little lamb'
def test__add__(self):
t = Protected('a')
assert t + 'b' == Text(Protected('a'), 'b')
assert t + t == Text(Protected('aa'))
def test__getitem__(self):
t = Protected('1234567890')
with pytest.raises(TypeError):
1 in t
assert t == Protected('1234567890')
assert t[:0] == Protected('')
assert t[:1] == Protected('1')
assert t[:3] == Protected('123')
assert t[:5] == Protected('12345')
assert t[:7] == Protected('1234567')
assert t[:10] == Protected('1234567890')
assert t[:100] == Protected('1234567890')
assert t[:-100] == Protected('')
assert t[:-10] == Protected('')
assert t[:-9] == Protected('1')
assert t[:-7] == Protected('123')
assert t[:-5] == Protected('12345')
assert t[:-3] == Protected('1234567')
assert t[-100:] == Protected('1234567890')
assert t[-10:] == Protected('1234567890')
assert t[-9:] == Protected('234567890')
assert t[-7:] == Protected('4567890')
assert t[-5:] == Protected('67890')
assert t[-3:] == Protected('890')
assert t[1:] == Protected('234567890')
assert t[3:] == Protected('4567890')
assert t[5:] == Protected('67890')
assert t[7:] == Protected('890')
assert t[10:] == Protected('')
assert t[100:] == Protected('')
assert t[0:10] == Protected('1234567890')
assert t[0:100] == Protected('1234567890')
assert t[2:3] == Protected('3')
assert t[2:4] == Protected('34')
assert t[3:7] == Protected('4567')
assert t[4:7] == Protected('567')
assert t[4:7] == Protected('567')
assert t[7:9] == Protected('89')
assert t[100:200] == Protected('')
t = Protected('123', Protected('456', Protected('789')), '0')
assert t[:3] == Protected('123')
assert t[:5] == Protected('123', Protected('45'))
assert t[:7] == Protected('123', Protected('456', Protected('7')))
assert t[:10] == Protected('123', Protected('456', Protected('789')), '0')
assert t[:100] == Protected('123', Protected('456', Protected('789')), '0')
assert t[:-7] == Protected('123')
assert t[:-5] == Protected('123', Protected('45'))
assert t[:-3] == Protected('123', Protected('456', Protected('7')))
def test_append(self):
text = Protected('Chuck Norris')
assert (text + ' wins!').render_as('latex') == '{Chuck Norris} wins!'
assert text.append(' wins!').render_as('latex') == '{Chuck Norris wins!}'
def test_upper(self):
text = Protected('Mary ', 'had ', 'a little lamb')
assert str(text.upper()) == 'Mary had a little lamb'
text = Protected('mary ', 'had ', 'a little lamb')
assert str(text.upper()) == 'mary had a little lamb'
def test_lower(self):
text = Protected('Mary ', 'had ', 'a little lamb')
assert str(text.lower()) == 'Mary had a little lamb'
text = Protected('MARY ', 'HAD ', 'A LITTLE LAMB')
assert str(text.lower()) == 'MARY HAD A LITTLE LAMB'
def test_startswith(self):
assert not Protected().startswith('.')
assert not Protected().startswith(('.', '!'))
text = Protected('mary ', 'had ', 'a little lamb')
assert not text.startswith('M')
assert text.startswith('m')
text = Protected('a', 'b', 'c')
assert text.startswith('ab')
assert Protected('This is good').startswith(('This', 'That'))
assert not Protected('This is good').startswith(('That', 'Those'))
def test_endswith(self):
assert not Protected().endswith('.')
assert not Protected().endswith(('.', '!'))
text = Protected('mary ', 'had ', 'a little lamb')
assert not text.endswith('B')
assert text.endswith('b')
text = Protected('a', 'b', 'c')
assert text.endswith('bc')
assert Protected('This is good').endswith(('good', 'wonderful'))
assert not Protected('This is good').endswith(('bad', 'awful'))
def test_isalpha(self):
assert not Protected().isalpha()
assert not Protected('a b c').isalpha()
assert Protected('abc').isalpha()
assert Protected(u'文字').isalpha()
def test_join(self):
assert Protected(' ').join(['a', Protected('b c')]).render_as('latex') == 'a{ b c}'
assert Protected(nbsp).join(['a', 'b', 'c']).render_as('latex') == 'a{~}b{~}c'
assert nbsp.join(['a', Protected('b'), 'c']).render_as('latex') == 'a~{b}~c'
assert String('-').join([Protected('a'), Protected('b'), Protected('c')]).render_as('latex') == '{a}-{b}-{c}'
result = Protected(' and ').join(['a', 'b', 'c']).render_as('latex')
assert result == 'a{ and }b{ and }c'
def test_split(self):
assert Protected().split() == [Protected()]
assert Protected().split('abc') == [Protected()]
assert Protected('a').split() == [Protected('a')]
assert Protected('a ').split() == [Protected('a ')]
assert Protected(' a ').split() == [Protected(' a ')]
assert Protected('a + b').split() == [Protected('a + b')]
assert Protected('a + b').split(' + ') == [Protected('a + b')]
assert Protected('abc').split('xyz') == [Protected('abc')]
assert Protected('---').split('--') == [Protected('---')]
assert Protected('---').split('-') == [Protected('---')]
def test_add_period(self):
assert not Protected().endswith(('.', '!', '?'))
assert not textutils.is_terminated(Protected())
assert Protected().add_period().render_as('latex') == '{}'
text = Protected("That's all, folks")
assert text.add_period().render_as('latex') == "{That's all, folks.}"
def test_render_as(self):
string = Protected('a < b')
assert string.render_as('latex') == '{a < b}'
assert string.render_as('html') == '<span class="bibtex-protected">a < b</span>'
class TestSymbol(TextTestMixin, TestCase):
def test__init__(self):
assert nbsp.name == 'nbsp'
def test__eq__(self):
assert Symbol('nbsp') == Symbol('nbsp')
assert not Symbol('nbsp') != Symbol('nbsp')
assert not Symbol('nbsp') == Symbol('ndash')
assert Symbol('nbsp') != Symbol('ndash')
assert Text(nbsp, nbsp) == Text(Symbol('nbsp'), Symbol('nbsp'))
def test__str__(self):
assert str(nbsp) == '<nbsp>'
def test__len__(self):
assert len(nbsp) == 1
def test__contains__(self):
assert not '' in nbsp
assert not 'abc' in nbsp
def test__getitem__(self):
symbol = Symbol('nbsp')
assert symbol[0] == Symbol('nbsp')
assert symbol[0:] == Symbol('nbsp')
assert symbol[0:5] == Symbol('nbsp')
assert symbol[1:] == String()
assert symbol[1:5] == String()
with pytest.raises(IndexError):
symbol[1]
def test__add__(self):
assert (nbsp + '.').render_as('html') == ' .'
def test_split(self):
assert nbsp.split() == [nbsp]
text = Text('F.', nbsp, 'Miller')
assert text.split() == [text]
def test_join(self):
assert nbsp.join(['S.', 'Jerusalem']) == Text('S.', nbsp, 'Jerusalem')
def test_upper(self):
assert nbsp.upper().render_as('html') == ' '
def test_lower(self):
assert nbsp.lower().render_as('html') == ' '
def test_capfirst(self):
assert Text(nbsp, nbsp).capfirst().render_as('html') == ' '
def test_capitalize(self):
assert Text(nbsp, nbsp).capitalize().render_as('html') == ' '
def test_add_period(self):
assert nbsp.add_period().render_as('html') == ' .'
assert nbsp.add_period().add_period().render_as('html') == ' .'
def test_append(self):
assert nbsp.append('.').render_as('html') == ' .'
def test_startswith(self):
assert not nbsp.startswith('.')
assert not nbsp.startswith(('.', '?!'))
def test_endswith(self):
assert not nbsp.endswith('.')
assert not nbsp.endswith(('.', '?!'))
def test_isalpha(self):
assert not nbsp.isalpha()
def test_render_as(self):
assert nbsp.render_as('latex') == '~'
assert nbsp.render_as('html') == ' '
assert Text(nbsp, nbsp).render_as('html') == ' '
|