def test_feeding_mammals_should_increase_weight(): test = namedtuple('test', 'given eats expected_weight_increase') tests = [ test(given=Tiger('tincho', weight=3, living_region=''), eats=Meat(4), expected_weight_increase=4 * 1), test(given=Dog('kucho', weight=2, living_region=''), eats=Meat(4), expected_weight_increase=4 * 0.40), test(given=Cat('kotio', weight=2, living_region=''), eats=Vegetable(5), expected_weight_increase=5 * 0.30), test(given=Mouse('misho', weight=2, living_region=''), eats=Fruit(5), expected_weight_increase=5 * 0.10), ] for mammal, food, expected_increase in tests: before = mammal.weight mammal.feed(food) after = mammal.weight assert after - before == expected_increase, \ f'{mammal.__class__.__name__} with weight:{before} ate {food.quantity} {food.__class__.__name__}'\ f' should have gained {expected_increase}, but it gained {after - before}'
def test_first_zero(self): owl = Owl("Pip", 10, 10) self.assertEqual(str(owl), "Owl [Pip, 10, 10, 0]") meat = Meat(4) self.assertEqual(owl.make_sound(), "Hoot Hoot") owl.feed(meat) veg = Vegetable(1) owl.feed(veg) self.assertEqual(owl.feed(veg), "Owl does not eat Vegetable!") self.assertEqual(str(owl), "Owl [Pip, 10, 11.0, 4]")
def test_can_instatiate_birds(): owl = Owl("Pip", 10, 10) print(owl) meat = Meat(4) print(owl.make_sound()) owl.feed(meat) veg = Vegetable(1) print(owl.feed(veg)) print(owl) hen = Hen("Harry", 10, 10) veg = Vegetable(3) fruit = Fruit(5) meat = Meat(1) print(hen) print(hen.make_sound()) hen.feed(veg) hen.feed(fruit) hen.feed(meat) print(hen)
def test_animals_repr(): test = namedtuple('test', 'given that_eats expected_repr') tests = [ test( given=Tiger('tincho', weight=1, living_region='Sudan'), that_eats=[Meat(1), Meat(2)], expected_repr='Tiger [tincho, 4, Sudan, 3]', ), test( given=Owl('buho', weight=1, wing_size=20), that_eats=[Meat(1), Meat(2)], expected_repr='Owl [buho, 20, 1.75, 3]', ), ] for animal, food_to_be_eaten, expected in tests: for f in food_to_be_eaten: animal.feed(f) assert repr(animal) == expected, \ f'wanted "{expected}", got {repr(animal)}'
def test_feeding_birds_should_increase_weight(): test = namedtuple('test', 'bird food expected_increase') tests = [ test(Owl('buho', weight=3, wing_size=30), food=Meat(3), expected_increase=3 * 0.25), test(Hen('hencho', weight=2, wing_size=10), food=Seed(3), expected_increase=3 * 0.35) ] for bird, food, expected_increase in tests: before = bird.weight bird.feed(food) after = bird.weight assert after - before == expected_increase, \ f'{bird.__class__.__name__} with w:{before} ate {food.quantity} {food.__class__.__name__}'\ f' should have gained {expected_increase}, but it gained {after - before}'
# first zero test import unittest from project.animals.birds import Owl, Hen from project.animals.mammals import Mouse, Dog, Cat, Tiger from project.food import Vegetable, Fruit, Meat, Seed miki = Mouse("Miki", 10, "pakistan") print(miki) kartof = Vegetable(3) meso = Meat(5) miki.feed(kartof) print(miki.feed(meso)) print(miki) gosheto = Dog("goshe", 20, "gruzia") print(gosheto) print(gosheto.feed(kartof)) gosheto.feed(meso) print(gosheto) owl = Owl("Pip", 10, 10) print(owl) meat = Meat(4) print(owl.make_sound()) owl.feed(meat) veg = Vegetable(1) print(owl.feed(veg)) print(owl) hen = Hen("Harry", 10, 10) veg = Vegetable(3) fruit = Fruit(5) meat = Meat(1)
from project.animals.animal import Mammal, Bird, Animal from project.animals.birds import Hen, Owl from project.animals.mammals import Mouse, Cat, Dog, Tiger from project.food import Food, Meat, Vegetable, Fruit, Seed owl = Mouse("Pip", 10, 10) print(owl) meat = Meat(4) print(owl.make_sound()) print(owl.feed(meat)) veg = Vegetable(3) fruit = Fruit(4) seed = Seed(1) print(owl.feed(veg)) print(owl.feed(seed)) print(owl.feed(fruit)) print(owl) print("-" * 25) hen = Hen("Harry", 10, 10) veg = Vegetable(3) fruit = Fruit(4) seed = Seed(1) meat = Meat(1) print(hen) print(hen.make_sound()) hen.feed(veg) hen.feed(fruit) hen.feed(seed) hen.feed(meat)
def __repr__(self): return super().__repr__() class Hen(Bird): food_eaten = 0 def __init__(self, name: str, weight: float, wing_size): super().__init__(name, weight, wing_size) def make_sound(self): return "Cluck" def feed(self, food): self.food_eaten += food.quantity self.weight += food.quantity * 0.35 def __repr__(self): return super().__repr__() owl = Owl("Pip", 10, 10) print(owl) meat = Meat(4) # print(meat.__class__.__name__) print(owl.make_sound()) owl.feed(meat) veg = Vegetable(1) print(owl.feed(veg)) print(owl)
from project.animals.birds import Owl, Hen from project.food import Meat, Vegetable, Fruit # owl = Owl("Pip", 10, 10) # print(owl) # meat = Meat(4) # print(owl.make_sound()) # owl.feed(meat) # veg = Vegetable(1) # print(owl.feed(veg)) # print(owl) hen = Hen("Harry", 10, 10) veg = Vegetable(3) fruit = Fruit(5) meat = Meat(1) print(hen) print(hen.make_sound()) hen.feed(veg) hen.feed(fruit) hen.feed(meat) print(hen)
# print(hen) # print(owl) # print(mouse) # print(cat) # print(dog) # print(tiger) # # print(hen.make_sound()) # print(owl.make_sound()) # print(mouse.make_sound()) # print(cat.make_sound()) # print(dog.make_sound()) # print(tiger.make_sound()) # veg = Vegetable(5) meat = Meat(3) fruit = Fruit(6) seed = Seed(10) # # print(owl.feed(fruit)) # print(mouse.feed(seed)) # print(cat.feed(fruit)) # print(dog.feed(veg)) # print(tiger.feed(seed)) # # hen.feed(seed) # owl.feed(meat) # mouse.feed(fruit) # cat.feed(veg) # dog.feed(meat) # tiger.feed(meat)