Add tests related to recent class method changes

This commit is contained in:
K. Lange 2021-03-09 23:00:47 +09:00
parent 5d508bcc17
commit 05f7d9537a
7 changed files with 138 additions and 1 deletions

View File

@ -756,7 +756,7 @@ static KrkValue _property_init(int argc, KrkValue argv[], int hasKw) {
static KrkValue _property_repr(int argc, KrkValue argv[], int hasKw) {
if (argc != 1 || !IS_PROPERTY(argv[0])) return krk_runtimeError(vm.exceptions->typeError, "?");
struct StringBuilder sb = {0};
pushStringBuilderStr(&sb, "Property(", 9);
pushStringBuilderStr(&sb, "property(", 9);
KrkValue method = AS_PROPERTY(argv[0])->method;

View File

@ -0,0 +1,32 @@
class Foo:
def bar(self):
print("Called bar")
let f = Foo()
f.bar()
def other(instance):
print("Called other")
def noargs():
print("Uh oh, binding will work but call will fail.")
Foo.other = other
Foo.noargs = noargs
print(f.other)
f.other()
print(f.noargs)
try:
f.noargs()
except Exception as e:
print(e)
class SomethingCallable():
def __call__(self):
print("I can be called")
Foo.callable = SomethingCallable()
f.callable()

View File

@ -0,0 +1,6 @@
Called bar
<bound method Foo.other>
Called other
<bound method Foo.noargs>
noargs() takes exactly 0 arguments (1 given)
I can be called

View File

@ -0,0 +1,12 @@
class Foo(object):
def __str__(self):
return '<str>'
def __repr__(self):
return '<repr>'
print('Foo:',Foo())
print(str(Foo()))
print(repr(Foo()))
print(Foo().__str__)
print(Foo().__repr__)

View File

@ -0,0 +1,5 @@
Foo: <str>
<str>
<repr>
<bound method Foo.__str__>
<bound method Foo.__repr__>

View File

@ -0,0 +1,63 @@
def base():
class A:
val=45
@property
def p(self,*args):
print('p retrieved from',self.__class__.__name__)
if args: print('actually a setter')
return {"a": self.val}
#@p.setter
#def p(self, fuck):
# print('called setter with', fuck)
def method(self):
return self.val
def mrod(self):
return self.val
class AA(A):
val=90
@property
def p(self,*args):
print('calling property from subclass')
if args: print('actually a setter')
return super().p
#@p.setter
#def p(self, val):
# print('setting p to',val)
def method(self):
return self.val
def withsup(self):
return super().val
def foo(a=None,aa=None):
print('property' in str(A.p))
print('property' in str(AA.p))
print(dir(A.p))
a = A()
print(a.p)
aa = AA()
print(aa.p)
aa.p = 42
print(a.val)
print(aa.val)
print(a.method())
print(aa.method())
aa.val = 42
print(aa.withsup())
print(aa.method())
print(aa.mrod())
aa.method = "nope, lol"
print(A.val)
foo()
if __name__ == '__main__':
base()

View File

@ -0,0 +1,19 @@
True
True
['__class__', '__dir__', '__doc__', '__hash__', '__init__', '__method__', '__module__', '__name__', '__repr__', '__str__']
p retrieved from A
{'a': 45}
calling property from subclass
p retrieved from AA
{'a': 90}
calling property from subclass
actually a setter
p retrieved from AA
45
90
45
90
45
42
42
45