-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for the inheritance features
- Loading branch information
1 parent
009bbe6
commit f42a561
Showing
5 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters