From f42a56174c667d5c4074c556993c4d6ac013997b Mon Sep 17 00:00:00 2001 From: Tanay Manerikar Date: Sun, 18 Aug 2024 20:55:06 +0530 Subject: [PATCH] Added tests for the inheritance features --- integration_tests/CMakeLists.txt | 3 ++ integration_tests/class_05.py | 37 +++++++++++++++++++ integration_tests/class_06.py | 36 ++++++++++++++++++ tests/reference/asr-structs_09-f3ffe08.json | 2 +- tests/reference/asr-structs_09-f3ffe08.stderr | 2 +- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 integration_tests/class_05.py create mode 100644 integration_tests/class_06.py diff --git a/integration_tests/CMakeLists.txt b/integration_tests/CMakeLists.txt index fb862da002..5ad8a0074d 100644 --- a/integration_tests/CMakeLists.txt +++ b/integration_tests/CMakeLists.txt @@ -841,6 +841,9 @@ RUN(NAME class_01 LABELS cpython llvm llvm_jit) RUN(NAME class_02 LABELS cpython llvm llvm_jit) RUN(NAME class_03 LABELS cpython llvm llvm_jit) RUN(NAME class_04 LABELS cpython llvm llvm_jit) +RUN(NAME class_05 LABELS cpython llvm llvm_jit) +RUN(NAME class_06 LABELS cpython llvm llvm_jit) + # callback_04 is to test emulation. So just run with cpython RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython) diff --git a/integration_tests/class_05.py b/integration_tests/class_05.py new file mode 100644 index 0000000000..75af54cd8f --- /dev/null +++ b/integration_tests/class_05.py @@ -0,0 +1,37 @@ +from lpython import i32 + +class Animal: + def __init__(self:"Animal"): + self.species: str = "Generic Animal" + self.age: i32 = 0 + self.is_domestic: bool = True + +class Dog(Animal): + def __init__(self:"Dog", name:str, age:i32): + super().__init__() + self.species: str = "Dog" + self.name: str = name + self.age: i32 = age + +class Cat(Animal): + def __init__(self:"Cat", name: str, age: i32): + super().__init__() + self.species: str = "Cat" + self.name:str = name + self.age: i32 = age + +def main(): + dog: Dog = Dog("Buddy", 5) + cat: Cat = Cat("Whiskers", 3) + op1: str = str(dog.name+" is a "+str(dog.age)+"-year-old "+dog.species+".") + print(op1) + assert op1 == "Buddy is a 5-year-old Dog." + print(dog.is_domestic) + assert dog.is_domestic == True + op2: str = str(cat.name+ " is a "+ str(cat.age)+ "-year-old "+ cat.species+ ".") + print(op2) + assert op2 == "Whiskers is a 3-year-old Cat." + print(cat.is_domestic) + assert cat.is_domestic == True + +main() diff --git a/integration_tests/class_06.py b/integration_tests/class_06.py new file mode 100644 index 0000000000..868985efdf --- /dev/null +++ b/integration_tests/class_06.py @@ -0,0 +1,36 @@ +from lpython import i32 + +class Base(): + def __init__(self:"Base"): + self.x : i32 = 10 + + def get_x(self:"Base")->i32: + print(self.x) + return self.x + +#Testing polymorphic fn calls +def get_x_static(d: Base)->i32: + print(d.x) + return d.x + +class Derived(Base): + def __init__(self: "Derived"): + super().__init__() + self.y : i32 = 20 + + def get_y(self:"Derived")->i32: + print(self.y) + return self.y + + +def main(): + d : Derived = Derived() + x : i32 = get_x_static(d) + assert x == 10 + # Testing parent method call using der obj + x = d.get_x() + assert x == 10 + y: i32 = d.get_y() + assert y == 20 + +main() diff --git a/tests/reference/asr-structs_09-f3ffe08.json b/tests/reference/asr-structs_09-f3ffe08.json index 0af164202d..a27b365565 100644 --- a/tests/reference/asr-structs_09-f3ffe08.json +++ b/tests/reference/asr-structs_09-f3ffe08.json @@ -8,6 +8,6 @@ "stdout": null, "stdout_hash": null, "stderr": "asr-structs_09-f3ffe08.stderr", - "stderr_hash": "f59ab2d213f6423e0a891e43d5a19e83d4405391b1c7bf481b4b939e", + "stderr_hash": "14119a0bc6420ad242b99395d457f2092014d96d2a1ac81d376c649d", "returncode": 2 } \ No newline at end of file diff --git a/tests/reference/asr-structs_09-f3ffe08.stderr b/tests/reference/asr-structs_09-f3ffe08.stderr index c7265fdddc..a67cb70dba 100644 --- a/tests/reference/asr-structs_09-f3ffe08.stderr +++ b/tests/reference/asr-structs_09-f3ffe08.stderr @@ -1,4 +1,4 @@ -semantic error: read not present in StringIO dataclass +semantic error: Method not found in the class StringIO or it's parents --> tests/errors/structs_09.py:13:23 | 13 | bytes_read: i32 = fd.read()