Skip to content

Commit

Permalink
Added tests for the inheritance features
Browse files Browse the repository at this point in the history
  • Loading branch information
tanay-man authored and Thirumalai-Shaktivel committed Aug 18, 2024
1 parent 009bbe6 commit f42a561
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 37 additions & 0 deletions integration_tests/class_05.py
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 36 additions & 0 deletions integration_tests/class_06.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_09-f3ffe08.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"stdout": null,
"stdout_hash": null,
"stderr": "asr-structs_09-f3ffe08.stderr",
"stderr_hash": "f59ab2d213f6423e0a891e43d5a19e83d4405391b1c7bf481b4b939e",
"stderr_hash": "14119a0bc6420ad242b99395d457f2092014d96d2a1ac81d376c649d",
"returncode": 2
}
2 changes: 1 addition & 1 deletion tests/reference/asr-structs_09-f3ffe08.stderr
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit f42a561

Please sign in to comment.