github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/runtime/super_test.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package grumpy 16 17 import ( 18 "testing" 19 ) 20 21 func TestSuperInitErrors(t *testing.T) { 22 // Only tests __init__ error cases. Non-error cases are tested 23 // implicitly by TestSuperGetAttribute. 24 cases := []invokeTestCase{ 25 {wantExc: mustCreateException(TypeErrorType, "'__init__' requires 2 arguments")}, 26 {args: wrapArgs(FloatType, 123), wantExc: mustCreateException(TypeErrorType, "super(type, obj): obj must be an instance or subtype of type")}, 27 } 28 for _, cas := range cases { 29 if err := runInvokeTestCase(superType.ToObject(), &cas); err != "" { 30 t.Error(err) 31 } 32 } 33 } 34 35 func TestSuperGetAttribute(t *testing.T) { 36 fun := wrapFuncForTest(func(f *Frame, t *Type, o *Object) (*Object, *BaseException) { 37 sup, raised := superType.Call(f, wrapArgs(t, o), nil) 38 if raised != nil { 39 return nil, raised 40 } 41 return GetAttr(f, sup, NewStr("attr"), nil) 42 }) 43 // top, left, bottom, right refer to parts of a diamond hierarchy. 44 topType := newTestClass("Top", []*Type{ObjectType}, newStringDict(map[string]*Object{ 45 "attr": NewStr("top").ToObject(), 46 })) 47 top := newObject(topType) 48 leftType := newTestClass("Left", []*Type{topType}, newStringDict(map[string]*Object{ 49 "attr": NewStr("left").ToObject(), 50 })) 51 left := newObject(leftType) 52 rightType := newTestClass("Right", []*Type{topType}, newStringDict(map[string]*Object{ 53 "attr": newProperty(newBuiltinFunction("attr", func(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) { 54 s := "right" 55 if args[0] == nil { 56 // When the "instance" argument is nil, the 57 // descriptor is unbound. 58 s = "rightType" 59 } 60 return NewStr(s).ToObject(), nil 61 }).ToObject(), nil, nil).ToObject(), 62 })) 63 right := newObject(rightType) 64 bottomType := newTestClass("Bottom", []*Type{leftType, rightType}, newStringDict(map[string]*Object{ 65 "attr": NewStr("bottom").ToObject(), 66 })) 67 bottom := newObject(bottomType) 68 cases := []invokeTestCase{ 69 {args: wrapArgs(bottomType, bottom), want: NewStr("left").ToObject()}, 70 {args: wrapArgs(bottomType, bottomType), want: NewStr("left").ToObject()}, 71 {args: wrapArgs(leftType, bottom), want: NewStr("right").ToObject()}, 72 {args: wrapArgs(leftType, bottomType), want: NewStr("rightType").ToObject()}, 73 {args: wrapArgs(rightType, bottom), want: NewStr("top").ToObject()}, 74 {args: wrapArgs(rightType, bottomType), want: NewStr("top").ToObject()}, 75 {args: wrapArgs(topType, bottom), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 76 {args: wrapArgs(topType, bottomType), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 77 {args: wrapArgs(leftType, left), want: NewStr("top").ToObject()}, 78 {args: wrapArgs(leftType, leftType), want: NewStr("top").ToObject()}, 79 {args: wrapArgs(topType, left), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 80 {args: wrapArgs(topType, leftType), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 81 {args: wrapArgs(rightType, right), want: NewStr("top").ToObject()}, 82 {args: wrapArgs(rightType, rightType), want: NewStr("top").ToObject()}, 83 {args: wrapArgs(topType, right), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 84 {args: wrapArgs(topType, rightType), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 85 {args: wrapArgs(topType, top), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 86 {args: wrapArgs(topType, topType), wantExc: mustCreateException(AttributeErrorType, "'super' object has no attribute 'attr'")}, 87 } 88 for _, cas := range cases { 89 if err := runInvokeTestCase(fun, &cas); err != "" { 90 t.Error(err) 91 } 92 } 93 }