Skip to content

Commit

Permalink
Moved asserts from main code to unit tests (aimacode#396)
Browse files Browse the repository at this point in the history
* replace assert with if test in add_thing

* removed inline assert

* added unit test to check edit

* improve user interface
  • Loading branch information
kaivalyar authored and norvig committed Apr 17, 2017
1 parent 2c29a90 commit 4d9bea0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
18 changes: 10 additions & 8 deletions agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ def __init__(self, program=None):
self.bump = False
self.holding = []
self.performance = 0
if program is None:
if program is None or not isinstance(program, collections.Callable):
print("Can't find a valid program for {}, falling back to default.".format(self.__class__.__name__))
def program(percept):
return eval(input('Percept={}; action? '.format(percept)))
assert isinstance(program, collections.Callable)
self.program = program

def can_grab(self, thing):
Expand Down Expand Up @@ -298,12 +298,14 @@ def add_thing(self, thing, location=None):
for it. (Shouldn't need to override this."""
if not isinstance(thing, Thing):
thing = Agent(thing)
assert thing not in self.things, "Don't add the same thing twice"
thing.location = location if location is not None else self.default_location(thing)
self.things.append(thing)
if isinstance(thing, Agent):
thing.performance = 0
self.agents.append(thing)
if thing in self.things:
print("Can't add the same thing twice")
else:
thing.location = location if location is not None else self.default_location(thing)
self.things.append(thing)
if isinstance(thing, Agent):
thing.performance = 0
self.agents.append(thing)

def delete_thing(self, thing):
"""Remove a thing from the environment."""
Expand Down
7 changes: 7 additions & 0 deletions tests/test_agents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from agents import Direction
from agents import Agent
from agents import ReflexVacuumAgent, ModelBasedVacuumAgent, TrivialVacuumEnvironment


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

def test_Agent():
def constant_prog(percept):
return percept
agent = Agent(constant_prog)
result = agent.program(5)
assert result == 5

0 comments on commit 4d9bea0

Please sign in to comment.