@@ -911,10 +911,10 @@ def test_startswith(self):
911
911
self .checkequal (True , 'hello' , 'startswith' , 'o' , - 1 )
912
912
self .checkequal (True , 'hello' , 'startswith' , '' , - 3 , - 3 )
913
913
self .checkequal (False , 'hello' , 'startswith' , 'lo' , - 9 )
914
-
914
+
915
915
self .checkraises (TypeError , 'hello' , 'startswith' )
916
916
#self.checkraises(TypeError, 'hello', 'startswith', 42)
917
-
917
+
918
918
# test tuple arguments
919
919
self .checkequal (True , 'hello' , 'startswith' , ('he' , 'ha' ))
920
920
self .checkequal (False , 'hello' , 'startswith' , ('lo' , 'llo' ))
@@ -927,11 +927,82 @@ def test_startswith(self):
927
927
self .checkequal (True , 'hello' , 'startswith' , ('lo' , 'he' ), 0 , - 1 )
928
928
self .checkequal (False , 'hello' , 'startswith' , ('he' , 'hel' ), 0 , 1 )
929
929
self .checkequal (True , 'hello' , 'startswith' , ('he' , 'hel' ), 0 , 2 )
930
-
930
+
931
931
self .checkraises (TypeError , 'hello' , 'startswith' , (42 ,))
932
932
self .checkequal (True , 'hello' , 'startswith' , ('he' , 42 ))
933
933
self .checkraises (TypeError , 'hello' , 'startswith' , ('ne' , 42 ,))
934
934
935
+ def test_rsplit (self ):
936
+ # by a char
937
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a|b|c|d' , 'rsplit' , '|' )
938
+ self .checkequal (['a|b|c' , 'd' ], 'a|b|c|d' , 'rsplit' , '|' , 1 )
939
+ self .checkequal (['a|b' , 'c' , 'd' ], 'a|b|c|d' , 'rsplit' , '|' , 2 )
940
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a|b|c|d' , 'rsplit' , '|' , 3 )
941
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a|b|c|d' , 'rsplit' , '|' , 4 )
942
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a|b|c|d' , 'rsplit' , '|' ,
943
+ sys .maxsize - 100 )
944
+ self .checkequal (['a|b|c|d' ], 'a|b|c|d' , 'rsplit' , '|' , 0 )
945
+ self .checkequal (['a||b||c' , '' , 'd' ], 'a||b||c||d' , 'rsplit' , '|' , 2 )
946
+ self .checkequal (['abcd' ], 'abcd' , 'rsplit' , '|' )
947
+ self .checkequal (['' ], '' , 'rsplit' , '|' )
948
+ self .checkequal (['' , ' begincase' ], '| begincase' , 'rsplit' , '|' )
949
+ self .checkequal (['endcase ' , '' ], 'endcase |' , 'rsplit' , '|' )
950
+ self .checkequal (['' , 'bothcase' , '' ], '|bothcase|' , 'rsplit' , '|' )
951
+
952
+ self .checkequal (['a\x00 \x00 b' , 'c' , 'd' ], 'a\x00 \x00 b\x00 c\x00 d' , 'rsplit' , '\x00 ' , 2 )
953
+
954
+ self .checkequal (['a' ]* 20 , ('a|' * 20 )[:- 1 ], 'rsplit' , '|' )
955
+ self .checkequal (['a|a|a|a|a' ]+ ['a' ]* 15 ,
956
+ ('a|' * 20 )[:- 1 ], 'rsplit' , '|' , 15 )
957
+
958
+ # by string
959
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a//b//c//d' , 'rsplit' , '//' )
960
+ self .checkequal (['a//b//c' , 'd' ], 'a//b//c//d' , 'rsplit' , '//' , 1 )
961
+ self .checkequal (['a//b' , 'c' , 'd' ], 'a//b//c//d' , 'rsplit' , '//' , 2 )
962
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a//b//c//d' , 'rsplit' , '//' , 3 )
963
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a//b//c//d' , 'rsplit' , '//' , 4 )
964
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a//b//c//d' , 'rsplit' , '//' ,
965
+ sys .maxsize - 5 )
966
+ self .checkequal (['a//b//c//d' ], 'a//b//c//d' , 'rsplit' , '//' , 0 )
967
+ self .checkequal (['a////b////c' , '' , 'd' ], 'a////b////c////d' , 'rsplit' , '//' , 2 )
968
+ self .checkequal (['' , ' begincase' ], 'test begincase' , 'rsplit' , 'test' )
969
+ self .checkequal (['endcase ' , '' ], 'endcase test' , 'rsplit' , 'test' )
970
+ self .checkequal (['' , ' bothcase ' , '' ], 'test bothcase test' ,
971
+ 'rsplit' , 'test' )
972
+ self .checkequal (['ab' , 'c' ], 'abbbc' , 'rsplit' , 'bb' )
973
+ self .checkequal (['' , '' ], 'aaa' , 'rsplit' , 'aaa' )
974
+ self .checkequal (['aaa' ], 'aaa' , 'rsplit' , 'aaa' , 0 )
975
+ self .checkequal (['ab' , 'ab' ], 'abbaab' , 'rsplit' , 'ba' )
976
+ self .checkequal (['aaaa' ], 'aaaa' , 'rsplit' , 'aab' )
977
+ self .checkequal (['' ], '' , 'rsplit' , 'aaa' )
978
+ self .checkequal (['aa' ], 'aa' , 'rsplit' , 'aaa' )
979
+ self .checkequal (['bbob' , 'A' ], 'bbobbbobbA' , 'rsplit' , 'bbobb' )
980
+ self .checkequal (['' , 'B' , 'A' ], 'bbobbBbbobbA' , 'rsplit' , 'bbobb' )
981
+
982
+ self .checkequal (['a' ]* 20 , ('aBLAH' * 20 )[:- 4 ], 'rsplit' , 'BLAH' )
983
+ self .checkequal (['a' ]* 20 , ('aBLAH' * 20 )[:- 4 ], 'rsplit' , 'BLAH' , 19 )
984
+ self .checkequal (['aBLAHa' ] + ['a' ]* 18 , ('aBLAH' * 20 )[:- 4 ],
985
+ 'rsplit' , 'BLAH' , 18 )
986
+
987
+ # with keyword args
988
+ self .checkequal (['a' , 'b' , 'c' , 'd' ], 'a|b|c|d' , 'rsplit' , sep = '|' )
989
+ self .checkequal (['a|b|c' , 'd' ],
990
+ 'a|b|c|d' , 'rsplit' , '|' , maxsplit = 1 )
991
+ self .checkequal (['a|b|c' , 'd' ],
992
+ 'a|b|c|d' , 'rsplit' , sep = '|' , maxsplit = 1 )
993
+ self .checkequal (['a|b|c' , 'd' ],
994
+ 'a|b|c|d' , 'rsplit' , maxsplit = 1 , sep = '|' )
995
+ self .checkequal (['a b c' , 'd' ],
996
+ 'a b c d' , 'rsplit' , maxsplit = 1 )
997
+
998
+ # argument type
999
+ self .checkraises (TypeError , 'hello' , 'rsplit' , 42 , 42 , 42 )
1000
+
1001
+ # null case
1002
+ self .checkraises (ValueError , 'hello' , 'rsplit' , '' )
1003
+ self .checkraises (ValueError , 'hello' , 'rsplit' , '' , 0 )
1004
+
1005
+
935
1006
def test_same_id ():
936
1007
empty_ids = set ([id (str ()) for i in range (100 )])
937
1008
assert len (empty_ids ) == 1
@@ -957,7 +1028,7 @@ def test_translate_from_byte_table():
957
1028
assert "ahoj" .translate (table ) == "AHOJ"
958
1029
assert "ahoj" .translate (bytearray (table )) == "AHOJ"
959
1030
assert "ahoj" .translate (memoryview (table )) == "AHOJ"
960
-
1031
+
961
1032
def test_tranlslate_from_short_table ():
962
1033
table = b'\x00 \x01 \x02 \x03 \x04 \x05 \x06 \x07 \x08 \t \n \x0b \x0c \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \x1b \x1c \x1d \x1e \x1f !"#$%&\' ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\ ]^_`ABCDEFGH'
963
1034
assert "ahoj" .translate (table ) == "AHoj"
@@ -983,4 +1054,3 @@ def test_literals():
983
1054
assert "hello\[world\]" [6 ] == "["
984
1055
assert "hello\[world\]" [12 ] == "\\ "
985
1056
assert "hello\[world\]" [13 ] == "]"
986
-
0 commit comments