Skip to content

Commit f42a561

Browse files
tanay-manThirumalai-Shaktivel
authored andcommitted
Added tests for the inheritance features
1 parent 009bbe6 commit f42a561

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

integration_tests/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,9 @@ RUN(NAME class_01 LABELS cpython llvm llvm_jit)
841841
RUN(NAME class_02 LABELS cpython llvm llvm_jit)
842842
RUN(NAME class_03 LABELS cpython llvm llvm_jit)
843843
RUN(NAME class_04 LABELS cpython llvm llvm_jit)
844+
RUN(NAME class_05 LABELS cpython llvm llvm_jit)
845+
RUN(NAME class_06 LABELS cpython llvm llvm_jit)
846+
844847

845848
# callback_04 is to test emulation. So just run with cpython
846849
RUN(NAME callback_04 IMPORT_PATH .. LABELS cpython)

integration_tests/class_05.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from lpython import i32
2+
3+
class Animal:
4+
def __init__(self:"Animal"):
5+
self.species: str = "Generic Animal"
6+
self.age: i32 = 0
7+
self.is_domestic: bool = True
8+
9+
class Dog(Animal):
10+
def __init__(self:"Dog", name:str, age:i32):
11+
super().__init__()
12+
self.species: str = "Dog"
13+
self.name: str = name
14+
self.age: i32 = age
15+
16+
class Cat(Animal):
17+
def __init__(self:"Cat", name: str, age: i32):
18+
super().__init__()
19+
self.species: str = "Cat"
20+
self.name:str = name
21+
self.age: i32 = age
22+
23+
def main():
24+
dog: Dog = Dog("Buddy", 5)
25+
cat: Cat = Cat("Whiskers", 3)
26+
op1: str = str(dog.name+" is a "+str(dog.age)+"-year-old "+dog.species+".")
27+
print(op1)
28+
assert op1 == "Buddy is a 5-year-old Dog."
29+
print(dog.is_domestic)
30+
assert dog.is_domestic == True
31+
op2: str = str(cat.name+ " is a "+ str(cat.age)+ "-year-old "+ cat.species+ ".")
32+
print(op2)
33+
assert op2 == "Whiskers is a 3-year-old Cat."
34+
print(cat.is_domestic)
35+
assert cat.is_domestic == True
36+
37+
main()

integration_tests/class_06.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from lpython import i32
2+
3+
class Base():
4+
def __init__(self:"Base"):
5+
self.x : i32 = 10
6+
7+
def get_x(self:"Base")->i32:
8+
print(self.x)
9+
return self.x
10+
11+
#Testing polymorphic fn calls
12+
def get_x_static(d: Base)->i32:
13+
print(d.x)
14+
return d.x
15+
16+
class Derived(Base):
17+
def __init__(self: "Derived"):
18+
super().__init__()
19+
self.y : i32 = 20
20+
21+
def get_y(self:"Derived")->i32:
22+
print(self.y)
23+
return self.y
24+
25+
26+
def main():
27+
d : Derived = Derived()
28+
x : i32 = get_x_static(d)
29+
assert x == 10
30+
# Testing parent method call using der obj
31+
x = d.get_x()
32+
assert x == 10
33+
y: i32 = d.get_y()
34+
assert y == 20
35+
36+
main()

tests/reference/asr-structs_09-f3ffe08.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"stdout": null,
99
"stdout_hash": null,
1010
"stderr": "asr-structs_09-f3ffe08.stderr",
11-
"stderr_hash": "f59ab2d213f6423e0a891e43d5a19e83d4405391b1c7bf481b4b939e",
11+
"stderr_hash": "14119a0bc6420ad242b99395d457f2092014d96d2a1ac81d376c649d",
1212
"returncode": 2
1313
}

tests/reference/asr-structs_09-f3ffe08.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
semantic error: read not present in StringIO dataclass
1+
semantic error: Method not found in the class StringIO or it's parents
22
--> tests/errors/structs_09.py:13:23
33
|
44
13 | bytes_read: i32 = fd.read()

0 commit comments

Comments
 (0)