This repository has been archived by the owner on Jul 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from RoelAdriaans/feature/add-chapter-13-inher…
…itance
- Loading branch information
Showing
11 changed files
with
244 additions
and
8 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
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
class ClassType(enum.Enum): | ||
NONE = enum.auto() | ||
CLASS = enum.auto() | ||
SUBCLASS = enum.auto() |
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
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
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
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,118 @@ | ||
class TestClassesInheretance: | ||
def test_class_circular(self, run_code_block): | ||
line = "class Oops < Oops {}" | ||
|
||
assert ( | ||
run_code_block(line).err | ||
== "[line 1] Error at 'Oops' : A class can't inherit from itself.\n" | ||
) | ||
assert run_code_block(line).out == "" | ||
|
||
def test_class_not_a_class(self, run_code_block): | ||
lines = """ | ||
var NotAClass = "I am totally not a class"; | ||
class Subclass < NotAClass {} // ?! | ||
""" | ||
assert ( | ||
run_code_block(lines).err == "Superclass must be a class. in line [line3]\n" | ||
) | ||
assert run_code_block(lines).out == "" | ||
|
||
def test_class_inheritance(self, run_code_block): | ||
lines = """ | ||
class Doughnut { | ||
cook() { | ||
print "Fry until golden brown."; | ||
} | ||
} | ||
class BostonCream < Doughnut {} | ||
BostonCream().cook(); | ||
""" | ||
|
||
assert run_code_block(lines).err == "" | ||
assert run_code_block(lines).out == "Fry until golden brown.\n" | ||
|
||
def test_class_call_super(self, run_code_block): | ||
lines = """ | ||
class Doughnut { | ||
cook() { | ||
print "Fry until golden brown."; | ||
} | ||
} | ||
class BostonCream < Doughnut { | ||
cook() { | ||
super.cook(); | ||
print "Pipe full of custard and coat with chocolate."; | ||
} | ||
} | ||
BostonCream().cook(); | ||
// Prints: | ||
// Fry until golden brown. | ||
// Pipe full of custard and coat with chocolate.""" | ||
|
||
captured = run_code_block(lines) | ||
|
||
assert captured.err == "" | ||
assert ( | ||
captured.out == "Fry until golden brown.\n" | ||
"Pipe full of custard and coat with chocolate.\n" | ||
) | ||
|
||
def test_multiple_inherit(self, run_code_block): | ||
lines = """ | ||
class A { | ||
method() { | ||
print "A method"; | ||
} | ||
} | ||
class B < A { | ||
method() { | ||
print "B method"; | ||
} | ||
test() { | ||
super.method(); | ||
} | ||
} | ||
class C < B {} | ||
C().test(); | ||
""" | ||
captured = run_code_block(lines) | ||
|
||
assert captured.err == "" | ||
assert captured.out == "A method\n" | ||
|
||
def test_invalid_super(self, run_code_block): | ||
lines = """ | ||
class Eclair { | ||
cook() { | ||
super.cook(); | ||
print "Pipe full of crème pâtissière."; | ||
} | ||
} | ||
""" | ||
captured = run_code_block(lines) | ||
|
||
assert ( | ||
captured.err == "[line 4] Error at 'super' : " | ||
"Can't use 'super' in a class with no superclass.\n" | ||
) | ||
|
||
assert captured.out == "" | ||
|
||
def test_invalid_super_not_even_a_class(self, run_code_block): | ||
lines = "super.notEvenInAClass();" | ||
captured = run_code_block(lines) | ||
|
||
assert ( | ||
captured.err | ||
== "[line 1] Error at 'super' : Can't use 'super' outside of a class.\n" | ||
) | ||
assert captured.out == "" |
Oops, something went wrong.