@@ -357,3 +357,31 @@ func TestBuiltinPrint(t *testing.T) {
357357 }
358358 }
359359}
360+
361+ func TestBuiltinSetAttr (t * testing.T ) {
362+ fun := wrapFuncForTest (func (f * Frame , args ... * Object ) (* Object , * BaseException ) {
363+ fooObject := newTestClass ("Foo" , []* Type {ObjectType }, newStringDict (map [string ]* Object {})).ToObject ()
364+ result , raised := builtinSetAttr (f , append (Args {fooObject }, args ... ), nil )
365+ if raised != nil {
366+ return nil , raised
367+ }
368+ val , raised := GetAttr (f , fooObject , toStrUnsafe (args [0 ]), nil )
369+ if raised != nil {
370+ return nil , raised
371+ }
372+ return newTestTuple (result , val ).ToObject (), nil
373+ })
374+ cases := []invokeTestCase {
375+ {args : wrapArgs (), wantExc : mustCreateException (TypeErrorType , "'setattr' requires 3 arguments" )},
376+ {args : wrapArgs ("foo" , "bar" ), want : newTestTuple (None , "bar" ).ToObject ()},
377+ {args : wrapArgs ("foo" , 123 ), want : newTestTuple (None , 123 ).ToObject ()},
378+ {args : wrapArgs ("foo" ), wantExc : mustCreateException (TypeErrorType , "'setattr' requires 3 arguments" )},
379+ {args : wrapArgs ("foo" , 123 , None ), wantExc : mustCreateException (TypeErrorType , "'setattr' requires 3 arguments" )},
380+ {args : wrapArgs (123 , 123 ), wantExc : mustCreateException (TypeErrorType , "'setattr' requires a 'str' object but received a \" int\" " )},
381+ }
382+ for _ , cas := range cases {
383+ if err := runInvokeTestCase (fun , & cas ); err != "" {
384+ t .Error (err )
385+ }
386+ }
387+ }
0 commit comments