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
|
# Test cases for strings (compile and run)
[case testStrBasics]
from typing import Tuple
class A:
def __str__(self) -> str:
return "A-str"
def __repr__(self) -> str:
return "A-repr"
def f() -> str:
return 'some string'
def g() -> str:
return 'some\a \v \t \x7f " \n \0string π'
def tostr(x: int) -> str:
return str(x)
def booltostr(x: bool) -> str:
return str(x)
def clstostr(x: A) -> str:
return str(x)
def torepr(x: int) -> str:
return repr(x)
def booltorepr(x: bool) -> str:
return repr(x)
def clstorepr(x: A) -> str:
return repr(x)
def concat(x: str, y: str) -> str:
return x + y
def eq(x: str) -> int:
if x == 'foo':
return 0
elif x != 'bar':
return 1
return 2
def match(x: str, y: str) -> Tuple[bool, bool]:
return (x.startswith(y), x.endswith(y))
def match_tuple(x: str, y: Tuple[str, ...]) -> Tuple[bool, bool]:
return (x.startswith(y), x.endswith(y))
def match_tuple_literal_args(x: str, y: str, z: str) -> Tuple[bool, bool]:
return (x.startswith((y, z)), x.endswith((y, z)))
def remove_prefix_suffix(x: str, y: str) -> Tuple[str, str]:
return (x.removeprefix(y), x.removesuffix(y))
[file driver.py]
from native import (
f, g, A, tostr, booltostr, clstostr, concat, eq, match, match_tuple,
match_tuple_literal_args, remove_prefix_suffix,
torepr, booltorepr, clstorepr
)
import sys
from testutil import assertRaises
assert f() == 'some string'
assert f() is sys.intern('some string')
assert g() == 'some\a \v \t \x7f " \n \0string π'
assert tostr(57) == '57'
assert concat('foo', 'bar') == 'foobar'
assert booltostr(True) == 'True'
assert booltostr(False) == 'False'
assert clstostr(A()) == "A-str"
assert eq('foo') == 0
assert eq('zar') == 1
assert eq('bar') == 2
assert torepr(57) == '57'
assert booltorepr(True) == 'True'
assert booltorepr(False) == 'False'
assert clstorepr(A()) == "A-repr"
assert int(tostr(0)) == 0
assert int(tostr(20)) == 20
assert int(torepr(0)) == 0
assert int(torepr(20)) == 20
assert match('', '') == (True, True)
assert match('abc', '') == (True, True)
assert match('abc', 'a') == (True, False)
assert match('abc', 'c') == (False, True)
assert match('', 'abc') == (False, False)
assert match_tuple('abc', ('d', 'e')) == (False, False)
assert match_tuple('abc', ('a', 'c')) == (True, True)
assert match_tuple('abc', ('a',)) == (True, False)
assert match_tuple('abc', ('c',)) == (False, True)
assert match_tuple('abc', ('x', 'y', 'z')) == (False, False)
assert match_tuple('abc', ('x', 'y', 'z', 'a', 'c')) == (True, True)
with assertRaises(TypeError, "tuple for startswith must only contain str"):
assert match_tuple('abc', (None,))
with assertRaises(TypeError, "tuple for endswith must only contain str"):
assert match_tuple('abc', ('a', None))
assert match_tuple_literal_args('abc', 'z', 'a') == (True, False)
assert match_tuple_literal_args('abc', 'z', 'c') == (False, True)
assert remove_prefix_suffix('', '') == ('', '')
assert remove_prefix_suffix('abc', 'a') == ('bc', 'abc')
assert remove_prefix_suffix('abc', 'c') == ('abc', 'ab')
[case testStringEquality]
def eq(a: str, b: str) -> bool:
return a == b
def ne(a: str, b: str) -> bool:
return a != b
def test_basic() -> None:
xy = "xy"
xy2 = str().join(["x", "y"])
xx = "xx"
yy = "yy"
xxx = "xxx"
assert eq("", str())
assert not ne("", str())
assert eq("x", "x" + str())
assert ne("x", "y")
assert eq(xy, xy)
assert eq(xy, xy2)
assert not eq(xy, yy)
assert ne(xy, xx)
assert not ne(xy, xy)
assert not ne(xy, xy2)
assert ne(xx, xxx)
assert ne(xxx, xx)
assert ne("x", "")
assert ne("", "x")
assert ne("XX", xx)
assert ne(yy, xy)
def test_unicode() -> None:
assert eq(chr(200), chr(200) + str())
assert ne(chr(200), chr(201))
assert eq(chr(1234), chr(1234) + str())
assert ne(chr(1234), chr(1235))
assert eq("\U0001f4a9", "\U0001f4a9" + str())
assert eq("\U0001f4a9", "\U0001F4A9" + str())
assert ne("\U0001f4a9", "\U0002f4a9" + str())
assert ne("\U0001f4a9", "\U0001f5a9" + str())
assert ne("\U0001f4a9", "\U0001f4a8" + str())
assert eq("foobar\u1234", "foobar\u1234" + str())
assert eq("\u1234foobar", "\u1234foobar" + str())
assert ne("foobar\uf234", "foobar\uf235")
assert ne("foobar\uf234", "foobar\uf334")
assert ne("foobar\u1234", "Foobar\u1234" + str())
assert eq("foo\U0001f4a9", "foo\U0001f4a9" + str())
assert eq("\U0001f4a9foo", "\U0001f4a9foo" + str())
assert ne("foo\U0001f4a9", "foo\U0001f4a8" + str())
assert ne("\U0001f4a9foo", "\U0001f4a8foo" + str())
[case testStringOps]
from typing import List, Optional, Tuple
from testutil import assertRaises
def do_split(s: str, sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
if sep is not None:
if max_split is not None:
return s.split(sep, max_split)
else:
return s.split(sep)
return s.split()
def do_rsplit(s: str, sep: Optional[str] = None, max_split: Optional[int] = None) -> List[str]:
if sep is not None:
if max_split is not None:
return s.rsplit(sep, max_split)
else:
return s.rsplit(sep)
return s.rsplit()
ss = "abc abcd abcde abcdef"
def test_split() -> None:
assert do_split(ss) == ["abc", "abcd", "abcde", "abcdef"]
assert do_split(ss, " ") == ["abc", "abcd", "abcde", "abcdef"]
assert do_split(ss, "-") == ["abc abcd abcde abcdef"]
assert do_split(ss, " ", -1) == ["abc", "abcd", "abcde", "abcdef"]
assert do_split(ss, " ", 0) == ["abc abcd abcde abcdef"]
assert do_split(ss, " ", 1) == ["abc", "abcd abcde abcdef"]
assert do_split(ss, " ", 2) == ["abc", "abcd", "abcde abcdef"]
def test_rsplit() -> None:
assert do_rsplit(ss) == ["abc", "abcd", "abcde", "abcdef"]
assert do_rsplit(ss, " ") == ["abc", "abcd", "abcde", "abcdef"]
assert do_rsplit(ss, "-") == ["abc abcd abcde abcdef"]
assert do_rsplit(ss, " ", -1) == ["abc", "abcd", "abcde", "abcdef"]
assert do_rsplit(ss, " ", 0) == ["abc abcd abcde abcdef"]
assert do_rsplit(ss, " ", 1) == ["abc abcd abcde", "abcdef"] # different to do_split
assert do_rsplit(ss, " ", 2) == ["abc abcd", "abcde", "abcdef"] # different to do_split
def splitlines(s: str, keepends: Optional[bool] = None) -> List[str]:
if keepends is not None:
return s.splitlines(keepends)
return s.splitlines()
s_text = "This\nis\n\nsome\nlong\ntext.\n"
def test_splitlines() -> None:
assert splitlines(s_text) == ["This", "is", "", "some", "long", "text."]
assert splitlines(s_text, False) == ["This", "is", "", "some", "long", "text."]
assert splitlines(s_text, True) == ["This\n", "is\n", "\n", "some\n", "long\n", "text.\n"]
s_partition = "Some long text"
def partition(s: str, sep: str) -> Tuple[str, str, str]:
return s.partition(sep)
def rpartition(s: str, sep: str) -> Tuple[str, str, str]:
return s.rpartition(sep)
def test_partition() -> None:
assert partition(s_partition, " ") == ("Some", " ", "long text")
assert partition(s_partition, "Hello") == ("Some long text", "", "")
assert rpartition(s_partition, " ") == ("Some long", " ", "text")
assert rpartition(s_partition, "Hello") == ("", "", "Some long text")
with assertRaises(ValueError, "empty separator"):
partition(s_partition, "")
with assertRaises(ValueError, "empty separator"):
rpartition(s_partition, "")
def contains(s: str, o: str) -> bool:
return o in s
def getitem(s: str, index: int) -> str:
return s[index]
def find(s: str, substr: str, start: Optional[int] = None, end: Optional[int] = None) -> int:
if start is not None:
if end is not None:
return s.find(substr, start, end)
return s.find(substr, start)
return s.find(substr)
def rfind(s: str, substr: str, start: Optional[int] = None, end: Optional[int] = None) -> int:
if start is not None:
if end is not None:
return s.rfind(substr, start, end)
return s.rfind(substr, start)
return s.rfind(substr)
s = "abc"
def test_contains() -> None:
assert contains(s, "a") is True
assert contains(s, "abc") is True
assert contains(s, "Hello") is False
assert contains(s, "bc") is True
assert contains(s, "abcd") is False
assert contains(s, "bb") is False
assert contains(s, "") is True
assert contains(s, " ") is False
def test_getitem() -> None:
assert getitem(s, 0) == "a"
assert getitem(s, 1) == "b"
assert getitem(s, 2) == "c"
assert getitem(s, -3) == "a"
assert getitem(s, -2) == "b"
assert getitem(s, -1) == "c"
with assertRaises(IndexError, "string index out of range"):
getitem(s, 4)
with assertRaises(IndexError, "string index out of range"):
getitem(s, -4)
def test_find() -> None:
s = "abcab"
assert find(s, "Hello") == -1
assert find(s, "abc") == 0
assert find(s, "b") == 1
assert find(s, "b", 1) == 1
assert find(s, "b", 1, 2) == 1
assert find(s, "b", 3) == 4
assert find(s, "b", 3, 5) == 4
assert find(s, "b", 3, 4) == -1
assert rfind(s, "Hello") == -1
assert rfind(s, "abc") == 0
assert rfind(s, "b") == 4
assert rfind(s, "b", 1) == 4
assert rfind(s, "b", 1, 2) == 1
assert rfind(s, "b", 3) == 4
assert rfind(s, "b", 3, 5) == 4
assert rfind(s, "b", 3, 4) == -1
def str_to_int(s: str, base: Optional[int] = None) -> int:
if base:
return int(s, base)
else:
return int(s)
def test_str_to_int() -> None:
assert str_to_int("1") == 1
assert str_to_int("10") == 10
assert str_to_int("a", 16) == 10
assert str_to_int("1a", 16) == 26
with assertRaises(ValueError, "invalid literal for int() with base 10: 'xyz'"):
str_to_int("xyz")
def test_slicing() -> None:
# Use dummy adds to avoid constant folding
zero = int()
two = zero + 2
s = "foobar" + str()
assert s[two:] == "obar"
assert s[:two] == "fo"
assert s[two:-two] == "ob"
assert s[two:two] == ""
assert s[two:two + 1] == "o"
assert s[-two:] == "ar"
assert s[:-two] == "foob"
assert s[:] == "foobar"
assert s[two:333] == "obar"
assert s[333:two] == ""
assert s[two:-333] == ""
assert s[-333:two] == "fo"
big_int: int = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
assert s[1:big_int] == "oobar"
assert s[big_int:] == ""
assert s[-big_int:-1] == "fooba"
def test_str_replace() -> None:
a = "foofoofoo"
assert a.replace("foo", "bar") == "barbarbar"
assert a.replace("foo", "bar", -1) == "barbarbar"
assert a.replace("foo", "bar", 1) == "barfoofoo"
assert a.replace("foo", "bar", 4) == "barbarbar"
assert a.replace("aaa", "bar") == "foofoofoo"
assert a.replace("ofo", "xyzw") == "foxyzwxyzwo"
def is_true(x: str) -> bool:
if x:
return True
else:
return False
def is_true2(x: str) -> bool:
return bool(x)
def is_false(x: str) -> bool:
if not x:
return True
else:
return False
def test_str_to_bool() -> None:
assert is_false('')
assert not is_true('')
assert not is_true2('')
for x in 'a', 'foo', 'bar', 'some string':
assert is_true(x)
assert is_true2(x)
assert not is_false(x)
def test_str_min_max() -> None:
x: str = 'aaa'
y: str = 'bbb'
z: str = 'aa'
assert min(x, y) == 'aaa'
assert min(x, z) == 'aa'
assert max(x, y) == 'bbb'
assert max(x, z) == 'aaa'
[case testStringFormattingCStyle]
from typing import Tuple
var = 'mypyc'
num = 20
def test_basics() -> None:
assert 'Hello %s, this is a test' % var == "Hello mypyc, this is a test"
assert 'Hello %s %d, this is a test' % (var, num) == "Hello mypyc 20, this is a test"
t: Tuple[str, int] = (var, num)
assert 'Hello %s %d, this is a test' % t == "Hello mypyc 20, this is a test"
large_num = 2**65
assert 'number: %d' % large_num == 'number: 36893488147419103232'
neg_num = -3
assert 'negative integer: %d' % neg_num == 'negative integer: -3'
assert 'negative integer: %d' % (-large_num) == 'negative integer: -36893488147419103232'
bool_var1 = True
bool_var2 = False
assert 'bool: %s, %s' % (bool_var1, bool_var2) == 'bool: True, False'
float_num = 123.4
assert '%f' % float_num == '123.400000'
assert '%.2f' % float_num == '123.40'
assert '%.5f' % float_num == '123.40000'
assert '%10.2f' % float_num == ' 123.40'
assert '%10.5f' % float_num == ' 123.40000'
assert '%010.5f' % float_num == '0123.40000'
assert '%015.5f' % float_num == '000000123.40000'
assert '%e' % float_num == '1.234000e+02'
large_float = 1.23e30
large_float2 = 1234123412341234123400000000000000000
small_float = 1.23e-20
assert '%f, %f, %f' % (small_float, large_float, large_float2) == \
'0.000000, 1229999999999999959718843908096.000000, 1234123412341234169005079998930878464.000000'
assert '%s, %s, %s' % (small_float, large_float, large_float2) == \
'1.23e-20, 1.23e+30, 1234123412341234123400000000000000000'
assert '%d, %d, %d' % (small_float, large_float, large_float2) == \
'0, 1229999999999999959718843908096, 1234123412341234123400000000000000000'
nan_num = float('nan')
inf_num = float('inf')
assert '%s, %s' % (nan_num, inf_num) == 'nan, inf'
assert '%f, %f' % (nan_num, inf_num) == 'nan, inf'
[typing fixtures/typing-full.pyi]
[case testFStrings]
import decimal
from datetime import datetime
from typing import Final
var = 'mypyc'
num = 20
final_known_at_compile_time: Final = 'hello'
def final_value_setter() -> str:
return 'goodbye'
final_unknown_at_compile_time: Final = final_value_setter()
def test_fstring_basics() -> None:
assert f'Hello {var}, this is a test' == "Hello mypyc, this is a test"
large_num = 2**65
assert f'number: {large_num}' == 'number: 36893488147419103232'
neg_num = -3
assert f'negative integer: {neg_num}' == 'negative integer: -3'
assert f'negative integer: {-large_num}' == 'negative integer: -36893488147419103232'
bool_var1 = True
bool_var2 = False
assert f'bool: {bool_var1}, {bool_var2}' == 'bool: True, False'
x = bytes([1, 2, 3, 4])
# assert f'bytes: {x}' == "bytes: b'\\x01\\x02\\x03\\x04'"
# error: If x = b'abc' then f"{x}" or "{}".format(x) produces "b'abc'", not "abc". If this is desired behavior, use f"{x!r}" or "{!r}".format(x). Otherwise, decode the bytes
float_num = 123.4
assert f'{float_num}' == '123.4'
assert f'{float_num:.2f}' == '123.40'
assert f'{float_num:.5f}' == '123.40000'
assert f'{float_num:>10.2f}' == ' 123.40'
assert f'{float_num:>10.5f}' == ' 123.40000'
assert f'{float_num:>010.5f}' == '0123.40000'
assert f'{float_num:>015.5f}' == '000000123.40000'
assert f'{float_num:e}' == '1.234000e+02'
large_float = 1.23e30
large_float2 = 1234123412341234123400000000000000000
small_float = 1.23e-20
assert f'{small_float}, {large_float}, {large_float2}' == '1.23e-20, 1.23e+30, 1234123412341234123400000000000000000'
nan_num = float('nan')
inf_num = float('inf')
assert f'{nan_num}, {inf_num}' == 'nan, inf'
assert f'{final_known_at_compile_time} {final_unknown_at_compile_time}' == 'hello goodbye'
# F-strings would be translated into ''.join[string literals, format method call, ...] in mypy AST.
# Currently we are using a str.join specializer for f-string speed up. We might not cover all cases
# and the rest ones should fall back to a normal str.join method call.
# TODO: Once we have a new pipeline for f-strings, this test case can be moved to testStringOps.
def test_str_join() -> None:
var = 'mypyc'
num = 10
assert ''.join(['a', 'b', '{}'.format(var), 'c']) == 'abmypycc'
assert ''.join(['a', 'b', '{:{}}'.format(var, ''), 'c']) == 'abmypycc'
assert ''.join(['a', 'b', '{:{}}'.format(var, '>10'), 'c']) == 'ab mypycc'
assert ''.join(['a', 'b', '{:{}}'.format(var, '>{}'.format(num)), 'c']) == 'ab mypycc'
assert var.join(['a', '{:{}}'.format(var, ''), 'b']) == 'amypycmypycmypycb'
assert ','.join(['a', '{:{}}'.format(var, ''), 'b']) == 'a,mypyc,b'
assert ''.join(['x', var]) == 'xmypyc'
class A:
def __init__(self, name, age):
self.name = name
self.age = age
def __repr__(self):
return f'{self.name} is {self.age} years old.'
def test_fstring_datatype() -> None:
u = A('John Doe', 14)
assert f'{u}' == 'John Doe is 14 years old.'
d = {'name': 'John Doe', 'age': 14}
assert f'{d}' == "{'name': 'John Doe', 'age': 14}"
def test_fstring_escape() -> None:
assert f"{'inside'}" == 'inside'
assert f'{"inside"}' == 'inside'
assert f"""inside""" == 'inside'
assert f'''inside''' == 'inside'
assert f"\"{'inside'}\"" == '"inside"'
assert f'\'{"inside"}\'' == "'inside'"
assert f'{{10}}' == '{10}'
assert f'{{10 + 10}}' == '{10 + 10}'
assert f'{{{10 + 10}}}' == '{20}'
assert f'{{{{10 + 10}}}}' == '{{10 + 10}}'
def test_fstring_conversion() -> None:
assert f'Hello {var!r}' == "Hello 'mypyc'"
# repr() is equivalent to !r
assert f'Hello {repr(var)}' == "Hello 'mypyc'"
assert f'Hello {var!a}' == "Hello 'mypyc'"
# ascii() is equivalent to !a
assert f'Hello {ascii(var)}' == "Hello 'mypyc'"
tmp_str = """this
is a new line."""
assert f'Test: {tmp_str!a}' == "Test: 'this\\n is a new line.'"
s = 'test: ΔΔΔΔΔΔ..Ε‘Ε Ε«ΕͺΕΎΕ½'
assert f'{s}' == 'test: ΔΔΔΔΔΔ..Ε‘Ε Ε«ΕͺΕΎΕ½'
assert f'{s!a}' == "'test: \\u0101\\u0100\\u0113\\u0112\\u010d\\u010c..\\u0161\\u0160\\u016b\\u016a\\u017e\\u017d'"
assert f'Hello {var!s}' == 'Hello mypyc'
assert f'Hello {num!s}' == 'Hello 20'
def test_fstring_align() -> None:
assert f'Hello {var:>20}' == "Hello mypyc"
assert f'Hello {var!r:>20}' == "Hello 'mypyc'"
assert f'Hello {var:>{num}}' == "Hello mypyc"
assert f'Hello {var!r:>{num}}' == "Hello 'mypyc'"
def test_fstring_multi() -> None:
assert f'Hello {var}, hello again {var}' == "Hello mypyc, hello again mypyc"
a = 'py'
s = f'my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}my{a}'
assert s == 'mypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypymypy'
def test_fstring_python_doc() -> None:
name = 'Fred'
assert f"He said his name is {name!r}." == "He said his name is 'Fred'."
assert f"He said his name is {repr(name)}." == "He said his name is 'Fred'."
width = 10
precision = 4
value = decimal.Decimal('12.34567')
assert f'result: {value:{width}.{precision}}' == 'result: 12.35' # nested field
today = datetime(year=2017, month=1, day=27)
assert f'{today:%B %d, %Y}' == 'January 27, 2017' # using date format specifier
number = 1024
assert f'{number:#0x}' == '0x400' # using integer format specifier
[case testStringFormatMethod]
from typing import Tuple
def test_format_method_basics() -> None:
x = str()
assert 'x{}'.format(x) == 'x'
assert 'Δ{}'.format(x) == 'Δ'
assert 'π{}'.format(x) == 'π'
assert ''.format() == ''
assert 'abc'.format() == 'abc'
assert '{}{}'.format(1, 2) == '12'
name = 'Eric'
age = 14
assert "My name is {name}, I'm {age}.".format(name=name, age=age) == "My name is Eric, I'm 14."
assert "My name is {A}, I'm {B}.".format(A=name, B=age) == "My name is Eric, I'm 14."
assert "My name is {}, I'm {B}.".format(name, B=age) == "My name is Eric, I'm 14."
bool_var1 = True
bool_var2 = False
assert 'bool: {}, {}'.format(bool_var1, bool_var2) == 'bool: True, False'
def test_format_method_empty_braces() -> None:
name = 'Eric'
age = 14
assert 'Hello, {}!'.format(name) == 'Hello, Eric!'
assert '{}'.format(name) == 'Eric'
assert '{}! Hi!'.format(name) == 'Eric! Hi!'
assert '{}, Hi, {}'.format(name, name) == 'Eric, Hi, Eric'
assert 'Hi! {}'.format(name) == 'Hi! Eric'
assert "Hi, I'm {}. I'm {}.".format(name, age) == "Hi, I'm Eric. I'm 14."
assert '{{}}'.format() == '{}'
assert '{{{{}}}}'.format() == '{{}}'
assert '{{}}{}'.format(name) == '{}Eric'
assert 'Hi! {{{}}}'.format(name) == 'Hi! {Eric}'
assert 'Hi! {{ {}'.format(name) == 'Hi! { Eric'
assert 'Hi! {{ {} }}}}'.format(name) == 'Hi! { Eric }}'
def test_format_method_numbers() -> None:
s = 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(-233)
assert s == 'int: -233; hex: -e9; oct: -351; bin: -11101001'
num = 2**65
s = 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(num)
assert s == 'int: 36893488147419103232; hex: 20000000000000000; oct: 4000000000000000000000; bin: 100000000000000000000000000000000000000000000000000000000000000000'
s = 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(-num)
assert s == 'int: -36893488147419103232; hex: -20000000000000000; oct: -4000000000000000000000; bin: -100000000000000000000000000000000000000000000000000000000000000000'
large_num = 2**65
assert 'number: {}'.format(large_num) == 'number: 36893488147419103232'
neg_num = -3
assert 'negative integer: {}'.format(neg_num) == 'negative integer: -3'
assert 'negative integer: {}'.format(-large_num) == 'negative integer: -36893488147419103232'
large_float = 1.23e30
large_float2 = 1234123412341234123400000000000000000
small_float = 1.23e-20
assert '{}, {}, {}'.format(small_float, large_float, large_float2) == '1.23e-20, 1.23e+30, 1234123412341234123400000000000000000'
nan_num = float('nan')
inf_num = float('inf')
assert '{}, {}'.format(nan_num, inf_num) == 'nan, inf'
def format_args(*args: int) -> str:
return 'x{}y{}'.format(*args)
def format_kwargs(**kwargs: int) -> str:
return 'c{x}d{y}'.format(**kwargs)
def format_args_self(*args: int) -> str:
return '{}'.format(args)
def format_kwargs_self(**kwargs: int) -> str:
return '{}'.format(kwargs)
def test_format_method_args() -> None:
assert format_args(10, 2) == 'x10y2'
assert format_args_self(10, 2) == '(10, 2)'
assert format_kwargs(x=10, y=2) == 'c10d2'
assert format_kwargs(x=10, y=2, z=1) == 'c10d2'
assert format_kwargs_self(x=10, y=2, z=1) == "{'x': 10, 'y': 2, 'z': 1}"
def test_format_method_different_kind() -> None:
s1 = "Literal['π']"
assert 'Revealed type is {}'.format(s1) == "Revealed type is Literal['π']"
s2 = "Revealed type is"
assert "{} Literal['π']".format(s2) == "Revealed type is Literal['π']"
s3 = "ζ΅θ―οΌ"
assert "{}{} {}".format(s3, s2, s1) == "ζ΅θ―οΌRevealed type is Literal['π']"
assert "Test: {}{}".format(s3, s1) == "Test: ζ΅θ―οΌLiteral['π']"
assert "Test: {}{}".format(s3, s2) == "Test: ζ΅θ―οΌRevealed type is"
def test_format_method_nested() -> None:
var = 'mypyc'
num = 10
assert '{:{}}'.format(var, '') == 'mypyc'
assert '{:{}}'.format(var, '>10') == ' mypyc'
assert '{:{}}'.format(var, '>{}'.format(num)) == ' mypyc'
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __str__(self):
return 'Point({self.x}, {self.y})'.format(self=self)
# Format examples from Python doc
# https://docs.python.org/3/library/string.html#formatexamples
def test_format_method_python_doc() -> None:
# Accessing arguments by position:
assert '{0}, {1}, {2}'.format('a', 'b', 'c') == 'a, b, c'
assert '{}, {}, {}'.format('a', 'b', 'c') == 'a, b, c'
assert '{2}, {1}, {0}'.format('a', 'b', 'c') == 'c, b, a'
assert '{2}, {1}, {0}'.format(*'abc') == 'c, b, a' # unpacking argument sequence
# assert '{0}{1}{0}'.format('abra', 'cad') = 'abracadabra' # arguments' indices can be repeated
# Accessing arguments by name:
s = 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
assert s == 'Coordinates: 37.24N, -115.81W'
coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
assert 'Coordinates: {latitude}, {longitude}'.format(**coord) == 'Coordinates: 37.24N, -115.81W'
# Accessing argumentsβ attributes:
assert str(Point(4, 2)) == 'Point(4, 2)'
# Accessing argumentsβ items:
coord2 = (3, 5)
assert 'X: {0[0]}; Y: {0[1]}'.format(coord2) == 'X: 3; Y: 5'
# Replacing %s and %r:
s = "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
assert s == "repr() shows quotes: 'test1'; str() doesn't: test2"
# Aligning the text and specifying a width:
assert '{:<30}'.format('left aligned') == 'left aligned '
assert '{:>30}'.format('right aligned') == ' right aligned'
assert '{:^30}'.format('centered') == ' centered '
assert '{:*^30}'.format('centered') == '***********centered***********' # use '*' as a fill char
# Replacing %+f, %-f, and % f and specifying a sign:
assert '{:+f}; {:+f}'.format(3.14, -3.14) == '+3.140000; -3.140000' # show it always
assert '{: f}; {: f}'.format(3.14, -3.14) == ' 3.140000; -3.140000' # show a space for positive numbers
assert '{:-f}; {:-f}'.format(3.14, -3.14) == '3.140000; -3.140000' # show only the minus -- same as '{:f}; {:f}'
# Replacing %x and %o and converting the value to different bases:
s = 'int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}'.format(42) # format also supports binary numbers
assert s == 'int: 42; hex: 2a; oct: 52; bin: 101010'
s = 'int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}'.format(42) # with 0x, 0o, or 0b as prefix:
assert s == 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'
# Using the comma as a thousands separator:
assert '{:,}'.format(1234567890) == '1,234,567,890'
# Expressing a percentage:
points = 19.0
total = 22.0
assert 'Correct answers: {:.2%}'.format(points/total) == 'Correct answers: 86.36%'
# Using type-specific formatting:
import datetime
d = datetime.datetime(2010, 7, 4, 12, 15, 58)
assert '{:%Y-%m-%d %H:%M:%S}'.format(d) == '2010-07-04 12:15:58'
# Nesting arguments and more complex examples:
tmp_strs = []
for align, text in zip('<^>', ['left', 'center', 'right']):
tmp_strs.append('{0:{fill}{align}16}'.format(text, fill=align, align=align))
assert tmp_strs == ['left<<<<<<<<<<<<', '^^^^^center^^^^^', '>>>>>>>>>>>right']
octets = [192, 168, 0, 1]
assert '{:02X}{:02X}{:02X}{:02X}'.format(*octets) == 'C0A80001'
width = 5
tmp_strs = []
for num in range(5,12):
tmp_str = ''
for base in 'dXob':
tmp_str += ('{0:{width}{base}}'.format(num, base=base, width=width))
tmp_strs.append(tmp_str)
assert tmp_strs == [' 5 5 5 101',\
' 6 6 6 110',\
' 7 7 7 111',\
' 8 8 10 1000',\
' 9 9 11 1001',\
' 10 A 12 1010',\
' 11 B 13 1011']
[case testChr]
# Some test cases are from https://docs.python.org/3/howto/unicode.html
def try_invalid(x: int) -> bool:
try:
chr(x + int())
return False
except ValueError:
return True
def test_chr() -> None:
assert chr(57344) == '\ue000'
assert chr(0) == '\x00'
assert chr(65) == 'A'
assert chr(150) == '\x96'
try:
chr(-1)
assert False
except ValueError:
pass
try:
chr(1114112)
assert False
except ValueError:
pass
assert chr(1114111) == '\U0010ffff'
x = 0
assert chr(x + int()) == '\x00'
x = 100
assert chr(x + int()) == 'd'
x = 150
assert chr(x + int()) == '\x96'
x = 257
assert chr(x + int()) == 'Δ'
x = 65537
assert chr(x + int()) == 'π'
assert try_invalid(-1)
assert try_invalid(1114112)
[case testOrd]
from testutil import assertRaises
def test_ord() -> None:
assert ord(' ') == 32
assert ord(' ' + str()) == 32
assert ord('\x00') == 0
assert ord('\x00' + str()) == 0
assert ord('\ue000') == 57344
assert ord('\ue000' + str()) == 57344
s = "a\xac\u1234\u20ac\U00010000"
# ^^^^ two-digit hex escape
# ^^^^^^ four-digit Unicode escape
# ^^^^^^^^^^ eight-digit Unicode escape
l1 = [ord(c) for c in s]
assert l1 == [97, 172, 4660, 8364, 65536]
u = 'abcdΓ©'
assert ord(u[-1]) == 233
assert ord(b'a') == 97
assert ord(b'a' + bytes()) == 97
u2 = '\U0010ffff' + str()
assert ord(u2) == 1114111
assert ord('\U0010ffff') == 1114111
with assertRaises(TypeError, "ord() expected a character, but a string of length 2 found"):
ord('aa')
with assertRaises(TypeError):
ord('')
[case testDecode]
from testutil import assertRaises
def test_decode() -> None:
assert "\N{GREEK CAPITAL LETTER DELTA}" == '\u0394'
assert "\u0394" == "\u0394"
assert "\U00000394" == '\u0394'
assert b'\x80abc'.decode('utf-8', 'replace') == '\ufffdabc'
assert b'\x80abc'.decode('utf-8', 'backslashreplace') == '\\x80abc'
assert b''.decode() == ''
assert b'a'.decode() == 'a'
assert b'abc'.decode() == 'abc'
assert b'abc'.decode('utf-8') == 'abc'
assert b'abc'.decode('utf-8' + str()) == 'abc'
assert b'abc\x00\xce'.decode('latin-1') == 'abc\x00\xce'
assert b'abc\x00\xce'.decode('latin-1' + str()) == 'abc\x00\xce'
assert b'abc\x00\x7f'.decode('ascii') == 'abc\x00\x7f'
assert b'abc\x00\x7f'.decode('ascii' + str()) == 'abc\x00\x7f'
assert b'\x80abc'.decode('utf-8', 'ignore') == 'abc'
assert b'\x80abc'.decode('UTF-8', 'ignore') == 'abc'
assert b'\x80abc'.decode('Utf-8', 'ignore') == 'abc'
assert b'\x80abc'.decode('utf_8', 'ignore') == 'abc'
assert b'\x80abc'.decode('latin1', 'ignore') == '\x80abc'
assert b'\xd2\xbb\xb6\xfe\xc8\xfd'.decode('gbk', 'ignore') == 'δΈδΊδΈ'
assert b'\xd2\xbb\xb6\xfe\xc8\xfd'.decode('latin1', 'ignore') == 'Γ»¢þΓΓ½'
assert b'Z\xc3\xbcrich'.decode("utf-8") == 'ZΓΌrich'
assert b'Z\xc3\xbcrich'.decode("utf-8" + str()) == 'ZΓΌrich'
assert bytearray(range(5)).decode() == '\x00\x01\x02\x03\x04'
b = bytearray(b'\xe4\xbd\xa0\xe5\xa5\xbd')
assert b.decode() == 'δ½ ε₯½'
assert b.decode('gbk') == 'ζ΅£η²γ½'
assert b.decode('latin1') == 'À½\xa0Γ₯Β₯Β½'
assert b.decode('latin1' + str()) == 'À½\xa0Γ₯Β₯Β½'
def test_decode_error() -> None:
try:
b'Z\xc3\xbcrich'.decode('ascii')
assert False
except UnicodeDecodeError:
pass
try:
b'Z\xc3\xbcrich'.decode('ascii' + str())
assert False
except UnicodeDecodeError:
pass
try:
b'Z\xc3y'.decode('utf8')
assert False
except UnicodeDecodeError:
pass
try:
b'Z\xc3y'.decode('utf8' + str())
assert False
except UnicodeDecodeError:
pass
def test_decode_bytearray() -> None:
b: bytes = bytearray(b'foo\x00bar')
assert b.decode() == 'foo\x00bar'
assert b.decode('utf-8') == 'foo\x00bar'
assert b.decode('latin-1') == 'foo\x00bar'
assert b.decode('ascii') == 'foo\x00bar'
assert b.decode('utf-8' + str()) == 'foo\x00bar'
assert b.decode('latin-1' + str()) == 'foo\x00bar'
assert b.decode('ascii' + str()) == 'foo\x00bar'
b2: bytes = bytearray(b'foo\x00bar\xbe')
assert b2.decode('latin-1') == 'foo\x00bar\xbe'
with assertRaises(UnicodeDecodeError):
b2.decode('ascii')
with assertRaises(UnicodeDecodeError):
b2.decode('ascii' + str())
with assertRaises(UnicodeDecodeError):
b2.decode('utf-8')
with assertRaises(UnicodeDecodeError):
b2.decode('utf-8' + str())
b3: bytes = bytearray(b'Z\xc3\xbcrich')
assert b3.decode("utf-8") == 'ZΓΌrich'
def test_invalid_encoding() -> None:
try:
b"foo".decode("ut-f-8")
assert False
except Exception as e:
assert repr(e).startswith("LookupError")
try:
encoding = "ut-f-8"
b"foo".decode(encoding)
assert False
except Exception as e:
assert repr(e).startswith("LookupError")
[case testEncode]
from testutil import assertRaises
def test_encode() -> None:
u = chr(40960) + 'abcd' + chr(1972)
assert u.encode() == b'\xea\x80\x80abcd\xde\xb4'
assert u.encode('utf-8') == b'\xea\x80\x80abcd\xde\xb4'
with assertRaises(UnicodeEncodeError):
u.encode('ascii')
with assertRaises(LookupError):
u.encode('aaa')
assert u.encode('utf-8', 'aaaaaa') == b'\xea\x80\x80abcd\xde\xb4'
assert u.encode('ascii', 'ignore') == b'abcd'
assert u.encode('ASCII', 'ignore') == b'abcd'
assert u.encode('ascii', 'replace') == b'?abcd?'
assert u.encode('ascii', 'xmlcharrefreplace') == b'ꀀabcd޴'
assert u.encode('ascii', 'backslashreplace') == b'\\ua000abcd\\u07b4'
assert u.encode('ascii', 'namereplace') == b'\\N{YI SYLLABLE IT}abcd\\u07b4'
assert 'pythΓΆn!'.encode() == b'pyth\xc3\xb6n!'
assert 'δΈδΊδΈ'.encode('gbk') == b'\xd2\xbb\xb6\xfe\xc8\xfd'
assert u.encode('UTF-8', 'ignore') == b'\xea\x80\x80abcd\xde\xb4'
assert u.encode('Utf_8') == b'\xea\x80\x80abcd\xde\xb4'
assert u.encode('UTF_8') == b'\xea\x80\x80abcd\xde\xb4'
assert u'\u00E1'.encode('latin1') == b'\xe1'
with assertRaises(UnicodeEncodeError):
u.encode('latin1')
[case testUnicodeSurrogate]
def f() -> str:
return "\ud800"
def test_surrogate() -> None:
assert ord(f()) == 0xd800
assert ord("\udfff") == 0xdfff
assert repr("foobar\x00\xab\ud912\U00012345") == r"'foobar\x00Β«\ud912π
'"
[case testStrip]
def test_all_strips_default() -> None:
s = " a1\t"
assert s.lstrip() == "a1\t"
assert s.strip() == "a1"
assert s.rstrip() == " a1"
def test_all_strips() -> None:
s = "xxb2yy"
assert s.lstrip("xy") == "b2yy"
assert s.strip("xy") == "b2"
assert s.rstrip("xy") == "xxb2"
def test_unicode_whitespace() -> None:
assert "\u200A\u000D\u2009\u2020\u000Dtt\u0085\u000A".strip() == "\u2020\u000Dtt"
def test_unicode_range() -> None:
assert "\u2029 \U00107581 ".lstrip() == "\U00107581 "
assert "\u2029 \U0010AAAA\U00104444B\u205F ".strip() == "\U0010AAAA\U00104444B"
assert " \u3000\u205F ".strip() == ""
assert "\u2029 \U00102865\u205F ".rstrip() == "\u2029 \U00102865"
[case testCount]
# mypy: disable-error-code="attr-defined"
def test_count() -> None:
string = "abcbcb"
assert string.count("a") == 1
assert string.count("b") == 3
assert string.count("c") == 2
def test_count_start() -> None:
string = "abcbcb"
assert string.count("a", 2) == string.count("a", -4) == 0, (string.count("a", 2), string.count("a", -4))
assert string.count("b", 2) == string.count("b", -4) == 2, (string.count("b", 2), string.count("b", -4))
assert string.count("c", 2) == string.count("c", -4) == 2, (string.count("c", 2), string.count("c", -4))
# out of bounds
assert string.count("a", 8) == 0
assert string.count("a", -8) == 1
assert string.count("b", 8) == 0
assert string.count("b", -8) == 3
assert string.count("c", 8) == 0
assert string.count("c", -8) == 2
def test_count_start_end() -> None:
string = "abcbcb"
assert string.count("a", 0, 4) == 1, string.count("a", 0, 4)
assert string.count("b", 0, 4) == 2, string.count("b", 0, 4)
assert string.count("c", 0, 4) == 1, string.count("c", 0, 4)
def test_count_multi() -> None:
string = "aaabbbcccbbbcccbbb"
assert string.count("aaa") == 1, string.count("aaa")
assert string.count("bbb") == 3, string.count("bbb")
assert string.count("ccc") == 2, string.count("ccc")
def test_count_multi_start() -> None:
string = "aaabbbcccbbbcccbbb"
assert string.count("aaa", 6) == string.count("aaa", -12) == 0, (string.count("aaa", 6), string.count("aaa", -12))
assert string.count("bbb", 6) == string.count("bbb", -12) == 2, (string.count("bbb", 6), string.count("bbb", -12))
assert string.count("ccc", 6) == string.count("ccc", -12) == 2, (string.count("ccc", 6), string.count("ccc", -12))
# out of bounds
assert string.count("aaa", 20) == 0
assert string.count("aaa", -20) == 1
assert string.count("bbb", 20) == 0
assert string.count("bbb", -20) == 3
assert string.count("ccc", 20) == 0
assert string.count("ccc", -20) == 2
def test_count_multi_start_end() -> None:
string = "aaabbbcccbbbcccbbb"
assert string.count("aaa", 0, 12) == 1, string.count("aaa", 0, 12)
assert string.count("bbb", 0, 12) == 2, string.count("bbb", 0, 12)
assert string.count("ccc", 0, 12) == 1, string.count("ccc", 0, 12)
def test_count_emoji() -> None:
string = "π΄πΓ±πΓ±π"
assert string.count("π΄") == 1, string.count("π΄")
assert string.count("π") == 3, string.count("π")
assert string.count("Γ±") == 2, string.count("Γ±")
def test_count_start_emoji() -> None:
string = "π΄πΓ±πΓ±π"
assert string.count("π΄", 2) == string.count("π΄", -4) == 0, (string.count("π΄", 2), string.count("π΄", -4))
assert string.count("π", 2) == string.count("π", -4) == 2, (string.count("π", 2), string.count("π", -4))
assert string.count("Γ±", 2) == string.count("Γ±", -4) == 2, (string.count("Γ±", 2), string.count("Γ±", -4))
# Out of bounds
assert string.count("π΄", 8) == 0, string.count("π΄", 8)
assert string.count("π΄", -8) == 1, string.count("π΄", -8)
assert string.count("π", 8) == 0, string.count("π", 8)
assert string.count("π", -8) == 3, string.count("π", -8)
assert string.count("Γ±", 8) == 0, string.count("Γ±", 8)
assert string.count("Γ±", -8) == 2, string.count("Γ±", -8)
def test_count_start_end_emoji() -> None:
string = "π΄πΓ±πΓ±π"
assert string.count("π΄", 0, 4) == 1, string.count("π΄", 0, 4)
assert string.count("π", 0, 4) == 2, string.count("π", 0, 4)
assert string.count("Γ±", 0, 4) == 1, string.count("Γ±", 0, 4)
def test_count_multi_emoji() -> None:
string = "π΄π΄π΄πππΓ±Γ±Γ±πππΓ±Γ±Γ±πππ"
assert string.count("π΄π΄π΄") == 1, string.count("π΄π΄π΄")
assert string.count("πππ") == 3, string.count("πππ")
assert string.count("Γ±Γ±Γ±") == 2, string.count("Γ±Γ±Γ±")
def test_count_multi_start_emoji() -> None:
string = "π΄π΄π΄πππΓ±Γ±Γ±πππΓ±Γ±Γ±πππ"
assert string.count("π΄π΄π΄", 6) == string.count("π΄π΄π΄", -12) == 0, (string.count("π΄π΄π΄", 6), string.count("π΄π΄π΄", -12))
assert string.count("πππ", 6) == string.count("πππ", -12) == 2, (string.count("πππ", 6), string.count("πππ", -12))
assert string.count("Γ±Γ±Γ±", 6) == string.count("Γ±Γ±Γ±", -12) == 2, (string.count("Γ±Γ±Γ±", 6), string.count("Γ±Γ±Γ±", -12))
# Out of bounds
assert string.count("π΄π΄π΄", 20) == 0, string.count("π΄π΄π΄", 20)
assert string.count("π΄π΄π΄", -20) == 1, string.count("π΄π΄π΄", -20)
assert string.count("πππ", 20) == 0, string.count("πππ", 20)
assert string.count("πππ", -20) == 3, string.count("πππ", -20)
assert string.count("Γ±Γ±Γ±", 20) == 0, string.count("Γ±Γ±Γ±", 20)
assert string.count("Γ±Γ±Γ±", -20) == 2, string.count("Γ±Γ±Γ±", -20)
def test_count_multi_start_end_emoji() -> None:
string = "π΄π΄π΄πππΓ±Γ±Γ±πππΓ±Γ±Γ±πππ"
assert string.count("π΄π΄π΄", 0, 12) == 1, string.count("π΄π΄π΄", 0, 12)
assert string.count("πππ", 0, 12) == 2, string.count("πππ", 0, 12)
assert string.count("Γ±Γ±Γ±", 0, 12) == 1, string.count("Γ±Γ±Γ±", 0, 12)
[case testIsInstance]
from copysubclass import subc
from typing import Any
def test_built_in() -> None:
s: Any = str()
assert isinstance(s, str)
assert isinstance(s + "test", str)
assert isinstance(s + "Γ±Γ±Γ±", str)
assert isinstance(subc(), str)
assert isinstance(subc("test"), str)
assert isinstance(subc("Γ±Γ±Γ±"), str)
assert not isinstance(set(), str)
assert not isinstance((), str)
assert not isinstance(('a','b'), str)
assert not isinstance({'a','b'}, str)
assert not isinstance(int() + 1, str)
assert not isinstance(['a','b'], str)
def test_user_defined() -> None:
from userdefinedstr import str
s: Any = "str"
assert isinstance(str(), str)
assert not isinstance(s, str)
[file copysubclass.py]
from typing import Any
class subc(str):
pass
[file userdefinedstr.py]
class str:
pass
[case testStrOptionalEquality]
from __future__ import annotations
def eq_s_opt_s_opt(x: str | None, y: str | None) -> bool:
return x == y
def ne_s_opt_s_opt(x: str | None, y: str | None) -> bool:
return x != y
def test_optional_eq() -> None:
s = 'x'
assert eq_s_opt_s_opt(s, s)
assert eq_s_opt_s_opt(s + str(int()), s + str(int()))
assert eq_s_opt_s_opt(None, None)
assert not eq_s_opt_s_opt('x', 'y')
assert not eq_s_opt_s_opt('y', 'x')
assert not eq_s_opt_s_opt(None, 'x')
assert not eq_s_opt_s_opt('x', None)
def test_optional_ne() -> None:
s = 'x'
assert not ne_s_opt_s_opt(s, s)
assert not ne_s_opt_s_opt(s + str(int()), s+ str(int()))
assert not ne_s_opt_s_opt(None, None)
assert ne_s_opt_s_opt('x', 'y')
assert ne_s_opt_s_opt('y', 'x')
assert ne_s_opt_s_opt(None, 'x')
assert ne_s_opt_s_opt('x', None)
|