Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d9bea0

Browse files
kaivalyarnorvig
authored andcommitted
Moved asserts from main code to unit tests (#396)
* replace assert with if test in add_thing * removed inline assert * added unit test to check edit * improve user interface
1 parent 2c29a90 commit 4d9bea0

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

agents.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def __init__(self, program=None):
8585
self.bump = False
8686
self.holding = []
8787
self.performance = 0
88-
if program is None:
88+
if program is None or not isinstance(program, collections.Callable):
89+
print("Can't find a valid program for {}, falling back to default.".format(self.__class__.__name__))
8990
def program(percept):
9091
return eval(input('Percept={}; action? '.format(percept)))
91-
assert isinstance(program, collections.Callable)
9292
self.program = program
9393

9494
def can_grab(self, thing):
@@ -298,12 +298,14 @@ def add_thing(self, thing, location=None):
298298
for it. (Shouldn't need to override this."""
299299
if not isinstance(thing, Thing):
300300
thing = Agent(thing)
301-
assert thing not in self.things, "Don't add the same thing twice"
302-
thing.location = location if location is not None else self.default_location(thing)
303-
self.things.append(thing)
304-
if isinstance(thing, Agent):
305-
thing.performance = 0
306-
self.agents.append(thing)
301+
if thing in self.things:
302+
print("Can't add the same thing twice")
303+
else:
304+
thing.location = location if location is not None else self.default_location(thing)
305+
self.things.append(thing)
306+
if isinstance(thing, Agent):
307+
thing.performance = 0
308+
self.agents.append(thing)
307309

308310
def delete_thing(self, thing):
309311
"""Remove a thing from the environment."""

tests/test_agents.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from agents import Direction
2+
from agents import Agent
23
from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment
34

45

@@ -65,3 +66,9 @@ def test_ModelBasedVacuumAgent() :
6566
# check final status of the environment
6667
assert environment.status == {(1,0):'Clean' , (0,0) : 'Clean'}
6768

69+
def test_Agent():
70+
def constant_prog(percept):
71+
return percept
72+
agent = Agent(constant_prog)
73+
result = agent.program(5)
74+
assert result == 5

0 commit comments

Comments
 (0)