Example #1
0
def test_init():
    '''Test that creating a Food object adds it to its location's food list.'''
    # TODO: I think at this point create_test_world would benefit from a fixture,
    #       as would create_test_animal.
    world = create_test_world()
    food = Food(world, 0, 0, 10)
    assert world.get_cell(0, 0).food == [food]
Example #2
0
def test_remove_animal_no_such_animal(capsys):
    world = create_test_world()
    animal = Animal(world, 0, 0, 1)

    animal_not_present = Bunch()
    world.remove_animal(animal_not_present)
    assert world.animals == [animal]
    assert capsys.readouterr()[0] == 'Error: Cannot remove animal from World(size=10).\n'
Example #3
0
def test_remove_food_no_such_food(capsys):
    world = create_test_world()
    food_cell = world.get_cell(0, 0)
    food = Food(world, 0, 0, 10)

    food_not_present = Bunch()
    world.get_cell(0, 0).remove_food(food_not_present)
    assert food_cell.food == [food]
    assert capsys.readouterr()[0] == 'Error: Cannot remove food from (0, 0).\n'
Example #4
0
def test_add_food_non_food(capsys):
    world = create_test_world()
    food_cell = world.get_cell(0, 0)

    non_food = Bunch(nutrition=10)
    food_cell.add_food(non_food)
    assert food_cell.food == []
    assert capsys.readouterr(
    )[0] == 'Error: Cannot add non-Food object Bunch to (0, 0).\n'
Example #5
0
def test_remove_animal_no_such_animal(capsys):
    cell = create_test_world().get_cell(0, 0)
    animal = Animal(cell.world, 0, 0, 1)

    animal_not_present = Bunch()
    cell.remove_animal(animal_not_present)
    assert cell.animals == [animal]
    out, _ = capsys.readouterr()
    assert out == 'Error: Cannot remove animal from (0, 0).\n'
Example #6
0
def test_add_and_remove_animal():
    cell = create_test_world().get_cell(0, 0)
    assert cell.animals == []

    animal = Animal(cell.world, 0, 0, 1)
    assert cell.animals == [animal]

    cell.remove_animal(animal)
    assert cell.animals == []
Example #7
0
def test_add_animal_non_animal(capsys):
    world = create_test_world()

    animal = Bunch(location=Bunch(x_pos=0, y_pos=0))
    world.add_animal(animal)
    assert world.animals == []
    # WorldCell.add_animal will also print an error, so just check that World's error is there.
    assert 'Error: Cannot add non-Animal object Bunch to World(size=10).\n' \
        in capsys.readouterr()[0]
Example #8
0
def test_add_and_remove_food():
    world = create_test_world()
    food_cell = world.get_cell(0, 0)
    assert food_cell.food == []

    food = Food(world, 0, 0, 10)
    assert food_cell.food == [food]

    food_cell.remove_food(food)
    assert food_cell.food == []
Example #9
0
def test_add_and_remove_animal():
    world = create_test_world()
    assert world.animals == []

    animal = Animal(world, 0, 0, 1)
    assert world.animals == [animal]
    assert world.get_cell(0, 0).animals == [animal]

    world.remove_animal(animal)
    assert world.animals == []
Example #10
0
def test_get_cell_invalid_location():
    world = create_test_world()
    cell = world.get_cell(1, -1)
    assert not cell

    cell = world.get_cell(1, 10)
    assert not cell

    cell = world.get_cell(1, "abc")
    assert not cell
Example #11
0
def test_repr():
    cell = create_test_world().get_cell(3, 3)
    assert repr(cell) == 'WorldCell at (3, 3)'
Example #12
0
def test_size():
    world = create_test_world()
    assert world.size == 10
Example #13
0
def test_get_cell_success():
    world = create_test_world()
    cell = world.get_cell(3, 3)
    assert cell == WorldCell(world, 3, 3)