def add_voombot(self, x_coord, y_coord, heading): position = Coordinates2D(int(x_coord), int(y_coord)) if not self._in_bounds(position): raise ValueError('VoomBot must be in the room') bot = VoomBot(position, CardinalPoint[heading]) # id(bot) is inmutable, unique and last the life-time of the bot self.bots[id(bot)] = bot return bot
def bot(): return VoomBot(Coordinates2D(0, 0), CardinalPoint.N)
@pytest.fixture def sim(): return VoomBotSimulator(5, 5) def test_add_bot(bot, sim): assert (sim.add_voombot(0, 0, 'N') == bot) is True def test_add_boot_out_of_room(sim): with pytest.raises(ValueError): sim.add_voombot(-1, -2, 'N') @pytest.mark.parametrize('coord,expected', [(Coordinates2D(0, 0), True), (Coordinates2D(0, 6), False), (Coordinates2D(-1, 0), False), (Coordinates2D(5, 5), True)]) def test_in_bounds(sim, coord, expected): assert sim._in_bounds(coord) is expected @pytest.mark.parametrize('cmd,expected', [('L', (0, 0, 'W')), ('R', (0, 0, 'E')), ('M', (0, 1, 'N'))]) def test_execute(sim, cmd, expected): bot = sim.add_voombot(0, 0, 'N') sim.execute(cmd, bot) assert bot.position.x == expected[0] assert bot.position.y == expected[1]
def test_add_coordinates(): assert Coordinates2D(2, 3) + Coordinates2D(-1, -3) == Coordinates2D(1, 0)
def test_coordinates_order(): assert Coordinates2D(2, 3) <= Coordinates2D(5, 6)
import pytest from voombot_simulator.navigation import CardinalPoint, Coordinates2D def test_add_coordinates(): assert Coordinates2D(2, 3) + Coordinates2D(-1, -3) == Coordinates2D(1, 0) @pytest.mark.parametrize('coord1,coord2,expected', [ (Coordinates2D(1, 1), Coordinates2D(1, 1), True), (Coordinates2D(1, 1), Coordinates2D(0, 0), False) ]) def test_equality(coord1, coord2, expected): assert (coord1 == coord2) is expected def test_coordinates_order(): assert Coordinates2D(2, 3) <= Coordinates2D(5, 6) @pytest.mark.parametrize('input,expected', [ (CardinalPoint.N, CardinalPoint.E), (CardinalPoint.E, CardinalPoint.S), (CardinalPoint.S, CardinalPoint.W), (CardinalPoint.W, CardinalPoint.N) ]) def test_clockwise(input, expected): assert CardinalPoint.clockwise(input) == expected
def _in_bounds(self, coord): return Coordinates2D(0, 0) <= coord <= self.dims
def __init__(self, x_coord, y_coord): if int(x_coord) < 0 or int(y_coord) < 0: raise ValueError('Dimensions must be positive') self.dims = Coordinates2D(int(x_coord), int(y_coord)) self.bots = {}
def __init__(self, position=None, heading=None): self.position = Coordinates2D( 0, 0) if position is None else position self.heading = CardinalPoint.N if heading is None else heading self._previous_position = self.position
for _ in range(turns): bot.turn_left() assert bot.heading == expected @pytest.mark.parametrize('turns,expected', [(1, CardinalPoint.E), (2, CardinalPoint.S), (3, CardinalPoint.W), (4, CardinalPoint.N)]) def test_turns_right(bot, turns, expected): for _ in range(turns): bot.turn_right() assert bot.heading == expected @pytest.mark.parametrize( 'bot,expected', [(VoomBot(heading=CardinalPoint.N), Coordinates2D(0, 1)), (VoomBot(heading=CardinalPoint.E), Coordinates2D(1, 0)), (VoomBot(heading=CardinalPoint.S), Coordinates2D(0, -1)), (VoomBot(heading=CardinalPoint.W), Coordinates2D(-1, 0))]) def test_move_forward(bot, expected): bot.move_forward() assert bot.position == expected def test_revert_move(bot): prev_pos = bot.position bot.move_forward() bot.revert_move() assert bot.position == prev_pos