@@ -98,6 +98,24 @@ func TestBuiltinFuncs(t *testing.T) {
98
98
return newObject (badNextType ), nil
99
99
}).ToObject (),
100
100
}))
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
+ }))
101
119
addType := newTestClass ("Add" , []* Type {ObjectType }, newStringDict (map [string ]* Object {
102
120
"__add__" : newBuiltinFunction ("__add__" , func (f * Frame , _ Args , _ KWArgs ) (* Object , * BaseException ) {
103
121
return NewInt (1 ).ToObject (), nil
@@ -186,6 +204,14 @@ func TestBuiltinFuncs(t *testing.T) {
186
204
{f : "divmod" , args : wrapArgs (- 3.25 , - 1.0 ), want : NewTuple2 (NewFloat (3.0 ).ToObject (), NewFloat (- 0.25 ).ToObject ()).ToObject ()},
187
205
{f : "divmod" , args : wrapArgs (NewStr ("a" ), NewStr ("b" )), wantExc : mustCreateException (TypeErrorType , "unsupported operand type(s) for divmod(): 'str' and 'str'" )},
188
206
{f : "divmod" , args : wrapArgs (), wantExc : mustCreateException (TypeErrorType , "'divmod' requires 2 arguments" )},
207
+ {f : "filter" , args : wrapArgs (None , NewTuple2 (NewInt (0 ).ToObject (), NewInt (1 ).ToObject ())), want : NewTuple1 (NewInt (1 ).ToObject ()).ToObject ()},
208
+ {f : "filter" , args : wrapArgs (BoolType , NewTuple2 (NewInt (0 ).ToObject (), NewInt (1 ).ToObject ())), want : NewTuple1 (NewInt (1 ).ToObject ()).ToObject ()},
209
+ {f : "filter" , args : wrapArgs (None , "012" ), want : NewStr ("012" ).ToObject ()},
210
+ {f : "filter" , args : wrapArgs (IntType , "012" ), want : NewStr ("12" ).ToObject ()},
211
+ {f : "filter" , args : wrapArgs (None , NewUnicode ("012" )), want : NewUnicode ("012" ).ToObject ()},
212
+ {f : "filter" , args : wrapArgs (None , newTestList (1 , 0 , 3 )), want : newTestList (1 , 3 ).ToObject ()},
213
+ {f : "filter" , args : wrapArgs (IntType , newTestList ("1" , "0" , "3" )), want : newTestList ("1" , "3" ).ToObject ()},
214
+ {f : "filter" , args : wrapArgs (BoolType , newObject (fooIterType )), want : newTestList (1 , 2 , 3 ).ToObject ()},
189
215
{f : "getattr" , args : wrapArgs (None , NewStr ("foo" ).ToObject (), NewStr ("bar" ).ToObject ()), want : NewStr ("bar" ).ToObject ()},
190
216
{f : "getattr" , args : wrapArgs (None , NewStr ("foo" ).ToObject ()), wantExc : mustCreateException (AttributeErrorType , "'NoneType' object has no attribute 'foo'" )},
191
217
{f : "hasattr" , args : wrapArgs (newObject (ObjectType ), NewStr ("foo" ).ToObject ()), want : False .ToObject ()},
0 commit comments