From 05f7d9537a455dbdffb091e77e9b67cc20d2c08d Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Tue, 9 Mar 2021 23:00:47 +0900 Subject: [PATCH] Add tests related to recent class method changes --- src/builtins.c | 2 +- test/testClassMethodAssignment.krk | 32 ++++++++++ test/testClassMethodAssignment.krk.expect | 6 ++ test/testStrReprOrdering.krk | 12 ++++ test/testStrReprOrdering.krk.expect | 5 ++ test/testSubclassPropertySuperCall.krk | 63 +++++++++++++++++++ test/testSubclassPropertySuperCall.krk.expect | 19 ++++++ 7 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 test/testClassMethodAssignment.krk create mode 100644 test/testClassMethodAssignment.krk.expect create mode 100644 test/testStrReprOrdering.krk create mode 100644 test/testStrReprOrdering.krk.expect create mode 100644 test/testSubclassPropertySuperCall.krk create mode 100644 test/testSubclassPropertySuperCall.krk.expect diff --git a/src/builtins.c b/src/builtins.c index 883ea52..c662335 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -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; diff --git a/test/testClassMethodAssignment.krk b/test/testClassMethodAssignment.krk new file mode 100644 index 0000000..74e6541 --- /dev/null +++ b/test/testClassMethodAssignment.krk @@ -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() diff --git a/test/testClassMethodAssignment.krk.expect b/test/testClassMethodAssignment.krk.expect new file mode 100644 index 0000000..c88a86d --- /dev/null +++ b/test/testClassMethodAssignment.krk.expect @@ -0,0 +1,6 @@ +Called bar + +Called other + +noargs() takes exactly 0 arguments (1 given) +I can be called diff --git a/test/testStrReprOrdering.krk b/test/testStrReprOrdering.krk new file mode 100644 index 0000000..4ff6c64 --- /dev/null +++ b/test/testStrReprOrdering.krk @@ -0,0 +1,12 @@ +class Foo(object): + def __str__(self): + return '' + def __repr__(self): + return '' +print('Foo:',Foo()) + +print(str(Foo())) +print(repr(Foo())) + +print(Foo().__str__) +print(Foo().__repr__) diff --git a/test/testStrReprOrdering.krk.expect b/test/testStrReprOrdering.krk.expect new file mode 100644 index 0000000..25cca39 --- /dev/null +++ b/test/testStrReprOrdering.krk.expect @@ -0,0 +1,5 @@ +Foo: + + + + diff --git a/test/testSubclassPropertySuperCall.krk b/test/testSubclassPropertySuperCall.krk new file mode 100644 index 0000000..7943750 --- /dev/null +++ b/test/testSubclassPropertySuperCall.krk @@ -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() diff --git a/test/testSubclassPropertySuperCall.krk.expect b/test/testSubclassPropertySuperCall.krk.expect new file mode 100644 index 0000000..1aadf21 --- /dev/null +++ b/test/testSubclassPropertySuperCall.krk.expect @@ -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