Skip to content

Commit 61e684b

Browse files
authored
Merge pull request #34 from nqulizada835/main
Feed the cat specified by the "Cat" parameter.
2 parents 9df1ec5 + 99b3e8d commit 61e684b

5 files changed

Lines changed: 82 additions & 25 deletions

File tree

pyCatSim/api/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
Definition of the various Classes and in which Modules they may be found.
33
"""
44

5-
from .cat import Cat
6-
from .cat import Clowder
5+
from .cat import Cat, Clowder
76
from .human import Owner
7+

pyCatSim/api/human.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ..api.cat import Cat
88
from ..utils import facts
99

10+
1011
class Owner:
1112
"""
1213
Represents a cat owner who can care for one or more cats
@@ -71,7 +72,6 @@ def give_fact(self):
7172
return facts.random_facts()
7273

7374
def __init__(self, name, cats_owned):
74-
7575
if isinstance(cats_owned, Cat):
7676
cats_owned = [cats_owned]
7777
elif isinstance(cats_owned, list):
@@ -83,6 +83,30 @@ def __init__(self, name, cats_owned):
8383
self.name = name
8484
self.cats_owned = cats_owned
8585

86+
def feed(self, cat):
87+
"""
88+
Feed the specified cat owned by this Owner.
89+
90+
Parameters
91+
----------
92+
cat : Cat
93+
The cat to feed. Must be owned by this owner.
94+
95+
Raises
96+
------
97+
ValueError
98+
If the specified cat is not owned by this owner.
99+
AttributeError
100+
If the cat does not have 'hunger_level' or 'mood' attributes.
101+
"""
102+
if cat not in self.cats_owned:
103+
raise ValueError("This owner does not own the specified cat.")
104+
if not hasattr(cat, 'hunger_level') or not hasattr(cat, 'mood'):
105+
raise AttributeError("Cat must have 'hunger_level' and 'mood' attributes.")
106+
107+
cat.hunger_level = max(0, cat.hunger_level - 1)
108+
cat.mood += 1
109+
86110
def adopt(self, cats_object):
87111

88112
"""
@@ -161,5 +185,4 @@ def groom(self,Cat):
161185
"""
162186

163187
Cat.mood += 1
164-
165-
188+

pyCatSim/tests/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
#Test
1+
class Owner:
2+
def __init__(self, name, cats_owned):
3+
if isinstance(cats_owned, Cat):
4+
self.cats_owned = [cats_owned]
5+
elif isinstance(cats_owned, list):
6+
self.cats_owned = cats_owned
7+
else:
8+
raise TypeError("Must provide Cat or list of Cats")
9+
self.name = name

pyCatSim/tests/test_api_Cat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,4 @@ def test_give_fact_t0(self):
198198
"Cats use their whiskers to detect changes in their surroundings.",
199199
"The average house cat can run at speeds up to 30 mph.",
200200
"Cats meow only to communicate with humans."
201-
]
201+
]

pyCatSim/tests/test_api_Owner.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@
1919
'''
2020

2121
import pytest
22-
from pyCatSim import Cat, Owner
22+
import pyCatSim as cat
2323

2424
class TesthumanOwnerInit:
2525
''' Test for Owner instantiation '''
2626

2727
def test_init_t0(self):
28-
cat1 = Cat(name="Whiskers")
29-
owner1 = Owner(name="Sasha", cats_owned=cat1)
28+
cat1 = cat.Cat(name="Whiskers")
29+
owner1 = cat.Owner(name="Sasha", cats_owned=cat1)
3030

3131
assert owner1.name == 'Sasha'
3232
assert type(owner1.cats_owned) is list
3333
assert len(owner1.cats_owned) == 1
3434

3535
def test_init_t1(self):
36-
cat1 = Cat(name="Whiskers")
37-
cat2 = Cat(name="Boots", color="tabby")
36+
cat1 = cat.Cat(name="Whiskers")
37+
cat2 = cat.Cat(name="Boots", color="tabby")
3838
# Multiple cats
39-
owner1 = Owner(name="Liam", cats_owned=[cat1, cat2])
39+
owner1 = cat.Owner(name="Liam", cats_owned=[cat1, cat2])
4040

4141
assert owner1.name == 'Liam'
4242
assert type(owner1.cats_owned) is list
@@ -47,10 +47,29 @@ class TesthumanOwnerFact:
4747
''' Test for the give_fact function'''
4848

4949
def test_give_fact_t0(self):
50-
cat1 = Cat(name="Whiskers")
51-
owner1 = Owner(name="Sasha", cats_owned=cat1)
52-
50+
cat1 = cat.Cat(name="Whiskers")
51+
owner1 = cat.Owner(name="Sasha", cats_owned=cat1)
52+
# Test that the fact is given correctly
5353
owner1.give_fact()
54+
55+
56+
57+
class TesthumanOwnerAdopt:
58+
def test_adopt_t0(self):
59+
cat1 = cat.Cat(name="Whiskers")
60+
cat2 = cat.Cat(name="Boots", color="tabby")
61+
owner1 = cat.Owner(name="Sasha", cats_owned=cat1)
62+
owner2 = cat.Owner(name="Liam", cats_owned=[cat1, cat2])
63+
chestnut = cat.Cat(name='Chestnut', age=4, color='tabby')
64+
nutmeg = cat.Cat(name='Nutmeg', age=3, color='tortoiseshell')
65+
66+
new_cat = chestnut
67+
owner1.adopt(new_cat)
68+
assert owner1.cats_owned[-1] == new_cat
69+
70+
new_cat = [chestnut, nutmeg]
71+
owner2.adopt(new_cat)
72+
assert owner2.cats_owned[-len(new_cat):] == new_cat
5473

5574

5675
class TesthumanOwnerAdopt:
@@ -70,17 +89,24 @@ def test_adopt_t0(self):
7089
new_cat=[chestnut,nutmeg]
7190
owner2.adopt(new_cat)
7291
assert owner2.cats_owned[-len(new_cat):]==new_cat
73-
74-
class TesthumanActions:
92+
93+
class TesthumanGroom:
7594
''' Test for Owner action success '''
7695

7796
def test_groom_t0(self):
78-
cat1 = Cat(name="Whiskers",mood=7)
79-
owner1 = Owner(name="Sasha", cats_owned=cat1)
97+
cat1 = cat.Cat(name="Whiskers", mood=7)
98+
owner1 = cat.Owner(name="Sasha", cats_owned=cat1)
8099

81-
owner1.groom(cat1)
100+
owner1.groom(cat1)
101+
assert cat1.mood == 8
82102

83-
assert cat1.mood == 8
84-
85-
86-
103+
class TesthumanOwnerFeed:
104+
def test_feed_t0(self):
105+
"""
106+
Test that feeding a cat decreases hunger_level by 1 and increases mood by 1.
107+
"""
108+
test_cat = cat.Cat(name="Fluffy", hunger_level=5, mood=4)
109+
test_owner = cat.Owner(name="Jordan", cats_owned=test_cat)
110+
test_owner.feed(test_cat)
111+
assert test_cat.hunger_level == 4
112+
assert test_cat.mood == 5

0 commit comments

Comments
 (0)