@@ -259,6 +259,16 @@ func TestStrMethods(t *testing.T) {
259259 return NewLong (big .NewInt (2 )).ToObject (), nil
260260 }).ToObject (),
261261 }))
262+ intIntType := newTestClass ("IntInt" , []* Type {ObjectType }, newStringDict (map [string ]* Object {
263+ "__int__" : newBuiltinFunction ("__int__" , func (f * Frame , _ Args , _ KWArgs ) (* Object , * BaseException ) {
264+ return NewInt (2 ).ToObject (), nil
265+ }).ToObject (),
266+ }))
267+ longIntType := newTestClass ("LongInt" , []* Type {ObjectType }, newStringDict (map [string ]* Object {
268+ "__int__" : newBuiltinFunction ("__int__" , func (f * Frame , _ Args , _ KWArgs ) (* Object , * BaseException ) {
269+ return NewLong (big .NewInt (2 )).ToObject (), nil
270+ }).ToObject (),
271+ }))
262272 cases := []struct {
263273 methodName string
264274 args Args
@@ -379,6 +389,41 @@ func TestStrMethods(t *testing.T) {
379389 {"strip" , wrapArgs ("foo" , "bar" , "baz" ), nil , mustCreateException (TypeErrorType , "'strip' of 'str' requires 2 arguments" )},
380390 {"strip" , wrapArgs ("\xfb oo" , NewUnicode ("o" )), nil , mustCreateException (UnicodeDecodeErrorType , "'utf8' codec can't decode byte 0xfb in position 0" )},
381391 {"strip" , wrapArgs ("foo" , NewUnicode ("o" )), NewUnicode ("f" ).ToObject (), nil },
392+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" , 1 ), NewStr ("one@two!three!" ).ToObject (), nil },
393+ {"replace" , wrapArgs ("one!two!three!" , "!" , "" ), NewStr ("onetwothree" ).ToObject (), nil },
394+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" , 2 ), NewStr ("one@two@three!" ).ToObject (), nil },
395+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" , 3 ), NewStr ("one@two@three@" ).ToObject (), nil },
396+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" , 4 ), NewStr ("one@two@three@" ).ToObject (), nil },
397+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" , 0 ), NewStr ("one!two!three!" ).ToObject (), nil },
398+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" ), NewStr ("one@two@three@" ).ToObject (), nil },
399+ {"replace" , wrapArgs ("one!two!three!" , "x" , "@" ), NewStr ("one!two!three!" ).ToObject (), nil },
400+ {"replace" , wrapArgs ("one!two!three!" , "x" , "@" , 2 ), NewStr ("one!two!three!" ).ToObject (), nil },
401+ {"replace" , wrapArgs ("\xd0 \xb2 \xd0 \xbe \xd0 \xbb " , "" , "\x00 " , - 1 ), NewStr ("\x00 \xd0 \x00 \xb2 \x00 \xd0 \x00 \xbe \x00 \xd0 \x00 \xbb \x00 " ).ToObject (), nil },
402+ {"replace" , wrapArgs ("\xd0 \xb2 \xd0 \xbe \xd0 \xbb " , "" , "\x01 \x02 " , - 1 ), NewStr ("\x01 \x02 \xd0 \x01 \x02 \xb2 \x01 \x02 \xd0 \x01 \x02 \xbe \x01 \x02 \xd0 \x01 \x02 \xbb \x01 \x02 " ).ToObject (), nil },
403+ {"replace" , wrapArgs ("abc" , "" , "-" ), NewStr ("-a-b-c-" ).ToObject (), nil },
404+ {"replace" , wrapArgs ("abc" , "" , "-" , 3 ), NewStr ("-a-b-c" ).ToObject (), nil },
405+ {"replace" , wrapArgs ("abc" , "" , "-" , 0 ), NewStr ("abc" ).ToObject (), nil },
406+ {"replace" , wrapArgs ("" , "" , "" ), NewStr ("" ).ToObject (), nil },
407+ {"replace" , wrapArgs ("" , "" , "a" ), NewStr ("a" ).ToObject (), nil },
408+ {"replace" , wrapArgs ("abc" , "a" , "--" , 0 ), NewStr ("abc" ).ToObject (), nil },
409+ {"replace" , wrapArgs ("abc" , "xy" , "--" ), NewStr ("abc" ).ToObject (), nil },
410+ {"replace" , wrapArgs ("123" , "123" , "" ), NewStr ("" ).ToObject (), nil },
411+ {"replace" , wrapArgs ("123123" , "123" , "" ), NewStr ("" ).ToObject (), nil },
412+ {"replace" , wrapArgs ("123x123" , "123" , "" ), NewStr ("x" ).ToObject (), nil },
413+ {"replace" , wrapArgs ("one!two!three!" , "!" , "@" , NewLong (big .NewInt (1 ))), NewStr ("one@two!three!" ).ToObject (), nil },
414+ {"replace" , wrapArgs ("foobar" , "bar" , "baz" , newObject (intIntType )), NewStr ("foobaz" ).ToObject (), nil },
415+ {"replace" , wrapArgs ("foobar" , "bar" , "baz" , newObject (longIntType )), NewStr ("foobaz" ).ToObject (), nil },
416+ {"replace" , wrapArgs ("" , "" , "x" ), NewStr ("x" ).ToObject (), nil },
417+ {"replace" , wrapArgs ("" , "" , "x" , - 1 ), NewStr ("x" ).ToObject (), nil },
418+ {"replace" , wrapArgs ("" , "" , "x" , 0 ), NewStr ("" ).ToObject (), nil },
419+ {"replace" , wrapArgs ("" , "" , "x" , 1 ), NewStr ("" ).ToObject (), nil },
420+ {"replace" , wrapArgs ("" , "" , "x" , 1000 ), NewStr ("" ).ToObject (), nil },
421+ // TODO: Support unicode substring.
422+ {"replace" , wrapArgs ("foobar" , "" , NewUnicode ("bar" )), nil , mustCreateException (TypeErrorType , "'replace' requires a 'str' object but received a 'unicode'" )},
423+ {"replace" , wrapArgs ("foobar" , NewUnicode ("bar" ), "" ), nil , mustCreateException (TypeErrorType , "'replace' requires a 'str' object but received a 'unicode'" )},
424+ {"replace" , wrapArgs ("foobar" , "bar" , "baz" , None ), nil , mustCreateException (TypeErrorType , "an integer is required" )},
425+ {"replace" , wrapArgs ("foobar" , "bar" , "baz" , newObject (intIndexType )), nil , mustCreateException (TypeErrorType , "an integer is required" )},
426+ {"replace" , wrapArgs ("foobar" , "bar" , "baz" , newObject (longIndexType )), nil , mustCreateException (TypeErrorType , "an integer is required" )},
382427 {"rstrip" , wrapArgs ("foo " ), NewStr ("foo" ).ToObject (), nil },
383428 {"rstrip" , wrapArgs (" foo bar " ), NewStr (" foo bar" ).ToObject (), nil },
384429 {"rstrip" , wrapArgs ("foo foo" , "o" ), NewStr ("foo f" ).ToObject (), nil },
0 commit comments