4 changed files with 73 additions and 68 deletions
@ -1,33 +0,0 @@
@@ -1,33 +0,0 @@
|
||||
class Animal(object): |
||||
"""Base class""" |
||||
noise = None |
||||
|
||||
def make_noise(self): |
||||
if self.noise is None: |
||||
raise NotImplementedError() |
||||
else: |
||||
print(self.noise) |
||||
|
||||
|
||||
class Goat(Animal): |
||||
noise = "baaa" |
||||
|
||||
|
||||
class Cow(Animal): |
||||
noise = "moo" |
||||
|
||||
|
||||
class Pig(Animal): |
||||
noise = "oink" |
||||
|
||||
|
||||
class Dog(Animal): |
||||
noise = "arf" |
||||
|
||||
|
||||
class Cat(Animal): |
||||
noise = "mrkgnao" |
||||
|
||||
|
||||
class Human(Animal): |
||||
noise = "buuurp" |
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
class Movie(object): |
||||
"""Base class: a movie starring Al Pacino""" |
||||
title = None |
||||
|
||||
def get_title(self): |
||||
if self.title is None: |
||||
raise NotImplementedError() |
||||
else: |
||||
print(self.title) |
||||
|
||||
|
||||
class Gf(Movie): |
||||
title = "The Godfather" |
||||
|
||||
|
||||
class Gf2(Movie): |
||||
title = "The Godfather Part II" |
||||
|
||||
|
||||
class Gf3(Movie): |
||||
title = "The Godfather Part III" |
||||
|
||||
|
||||
class Serpico(Movie): |
||||
title = "Serpico" |
||||
|
||||
|
||||
class Scarface(Movie): |
||||
title = "Scarface" |
||||
|
||||
|
||||
class Ggr(Movie): |
||||
title = "Glengarry Glen Ross" |
||||
|
||||
|
||||
class Heat(Movie): |
||||
title = "Heat" |
@ -1,35 +0,0 @@
@@ -1,35 +0,0 @@
|
||||
import os |
||||
import sys |
||||
import unittest |
||||
|
||||
from al_pacino.animals import Animal, Goat, Cow, Pig, Dog, Cat, Human |
||||
|
||||
pkg_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) # noqa |
||||
sys.path.insert(0, pkg_root) # noqa |
||||
|
||||
from tests import utils |
||||
|
||||
|
||||
class TestAnimals(unittest.TestCase): |
||||
def test_make_noise(self): |
||||
# base class |
||||
with self.assertRaises(NotImplementedError): |
||||
Animal().make_noise() |
||||
|
||||
# child classes |
||||
correct = [ |
||||
(Goat, "baaa"), |
||||
(Cow, "moo"), |
||||
(Pig, "oink"), |
||||
(Dog, "arf"), |
||||
(Cat, "mrkgnao"), |
||||
(Human, "buuurp"), |
||||
] |
||||
for cls, correct_noise in correct: |
||||
with utils.CaptureStdout() as output: |
||||
cls().make_noise() |
||||
self.assertIn(correct_noise, "".join(output)) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
unittest.main() |
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
import os |
||||
import sys |
||||
import unittest |
||||
|
||||
from al_pacino.movies import Movie, Gf, Gf2, Gf3, Serpico, Scarface, Ggr, Heat |
||||
|
||||
pkg_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) # noqa |
||||
sys.path.insert(0, pkg_root) # noqa |
||||
|
||||
from tests import utils |
||||
|
||||
|
||||
class TestMovies(unittest.TestCase): |
||||
def test_movies(self): |
||||
# base class |
||||
with self.assertRaises(NotImplementedError): |
||||
Movie().get_title() |
||||
|
||||
# child classes |
||||
correct = [ |
||||
(Gf, "The Godfather"), |
||||
(Gf2, "The Godfather Part II"), |
||||
(Gf3, "The Godfather Part III"), |
||||
(Serpico, "Serpico"), |
||||
(Scarface, "Scarface"), |
||||
(Ggr, "Glengarry Glen Ross"), |
||||
(Heat, "Heat"), |
||||
] |
||||
for cls, correct_title in correct: |
||||
with utils.CaptureStdout() as output: |
||||
cls().get_title() |
||||
self.assertIn(correct_title, output[0]) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
unittest.main() |
Loading…
Reference in new issue