@@ -98,6 +98,34 @@ func TestBuiltinFuncs(t *testing.T) {
9898 return newObject (badNextType ), nil
9999 }).ToObject (),
100100 }))
101+ createNextType := func (n int ) * Type {
102+ i := - 1
103+ nextType := newTestClass ("FooNextType" , []* Type {ObjectType }, newStringDict (map [string ]* Object {
104+ "next" : newBuiltinFunction ("next" , func (f * Frame , args Args , kwargs KWArgs ) (* Object , * BaseException ) {
105+ if i >= n {
106+ return nil , f .RaiseType (StopIterationType , "foo" )
107+ }
108+ i ++
109+ return NewInt (i ).ToObject (), nil
110+ }).ToObject (),
111+ }))
112+ return nextType
113+ }
114+ fooIterType := newTestClass ("FooIterType" , []* Type {ObjectType }, newStringDict (map [string ]* Object {
115+ "__iter__" : newBuiltinFunction ("__iter__" , func (f * Frame , args Args , kwargs KWArgs ) (* Object , * BaseException ) {
116+ return newObject (createNextType (3 )), nil
117+ }).ToObject (),
118+ }))
119+ customIterStrType := newTestClass ("CustomIterStrType" , []* Type {StrType }, newStringDict (map [string ]* Object {
120+ "__iter__" : newBuiltinFunction ("__iter__" , func (f * Frame , args Args , kwargs KWArgs ) (* Object , * BaseException ) {
121+ return newObject (createNextType (3 )), nil
122+ }).ToObject (),
123+ }))
124+ customIterTupleType := newTestClass ("CustomIterTupleType" , []* Type {TupleType }, newStringDict (map [string ]* Object {
125+ "__iter__" : newBuiltinFunction ("__iter__" , func (f * Frame , args Args , kwargs KWArgs ) (* Object , * BaseException ) {
126+ return newObject (createNextType (3 )), nil
127+ }).ToObject (),
128+ }))
101129 addType := newTestClass ("Add" , []* Type {ObjectType }, newStringDict (map [string ]* Object {
102130 "__add__" : newBuiltinFunction ("__add__" , func (f * Frame , _ Args , _ KWArgs ) (* Object , * BaseException ) {
103131 return NewInt (1 ).ToObject (), nil
@@ -186,6 +214,16 @@ func TestBuiltinFuncs(t *testing.T) {
186214 {f : "divmod" , args : wrapArgs (- 3.25 , - 1.0 ), want : NewTuple2 (NewFloat (3.0 ).ToObject (), NewFloat (- 0.25 ).ToObject ()).ToObject ()},
187215 {f : "divmod" , args : wrapArgs (NewStr ("a" ), NewStr ("b" )), wantExc : mustCreateException (TypeErrorType , "unsupported operand type(s) for divmod(): 'str' and 'str'" )},
188216 {f : "divmod" , args : wrapArgs (), wantExc : mustCreateException (TypeErrorType , "'divmod' requires 2 arguments" )},
217+ {f : "filter" , args : wrapArgs (None , NewTuple2 (NewInt (0 ).ToObject (), NewInt (1 ).ToObject ())), want : NewTuple1 (NewInt (1 ).ToObject ()).ToObject ()},
218+ {f : "filter" , args : wrapArgs (BoolType , NewTuple2 (NewInt (0 ).ToObject (), NewInt (1 ).ToObject ())), want : NewTuple1 (NewInt (1 ).ToObject ()).ToObject ()},
219+ {f : "filter" , args : wrapArgs (None , "012" ), want : NewStr ("012" ).ToObject ()},
220+ {f : "filter" , args : wrapArgs (IntType , "012" ), want : NewStr ("12" ).ToObject ()},
221+ {f : "filter" , args : wrapArgs (None , NewUnicode ("012" )), want : NewUnicode ("012" ).ToObject ()},
222+ {f : "filter" , args : wrapArgs (None , newTestList (1 , 0 , 3 )), want : newTestList (1 , 3 ).ToObject ()},
223+ {f : "filter" , args : wrapArgs (IntType , newTestList ("1" , "0" , "3" )), want : newTestList ("1" , "3" ).ToObject ()},
224+ {f : "filter" , args : wrapArgs (BoolType , newObject (fooIterType )), want : newTestList (1 , 2 , 3 ).ToObject ()},
225+ {f : "filter" , args : wrapArgs (BoolType , & Str {Object : Object {typ : customIterStrType }, value : "foo" }), want : NewStr ("foo" ).ToObject ()},
226+ {f : "filter" , args : wrapArgs (BoolType , & Tuple {Object : Object {typ : customIterTupleType }, elems : []* Object {NewInt (5 ).ToObject ()}}), want : NewTuple1 (NewInt (5 ).ToObject ()).ToObject ()},
189227 {f : "getattr" , args : wrapArgs (None , NewStr ("foo" ).ToObject (), NewStr ("bar" ).ToObject ()), want : NewStr ("bar" ).ToObject ()},
190228 {f : "getattr" , args : wrapArgs (None , NewStr ("foo" ).ToObject ()), wantExc : mustCreateException (AttributeErrorType , "'NoneType' object has no attribute 'foo'" )},
191229 {f : "hasattr" , args : wrapArgs (newObject (ObjectType ), NewStr ("foo" ).ToObject ()), want : False .ToObject ()},
0 commit comments