def test_init_orientation(orientation, expected): if isinstance(expected, Exception): # Expect an exception with pytest.raises(type(expected), match=str(expected)): orientation_obj = Orientation(orientation) print(orientation_obj) else: # Expect a successful initialization with the right attribute orientation_obj = Orientation(orientation) assert orientation_obj.cplx_orientation == expected assert orientation_obj.orientation == orientation
def test_forward_position(orientation, dst_position): position = Position(1, 2) orientation = Orientation(orientation) with mock.patch.object(position, 'restrict_to_grid') as restrict_to_grid: position.forward(orientation, grid_size=(100, 100)) restrict_to_grid.assert_called_once() assert (position.x, position.y) == dst_position
def test_mower_initialization(): position = Position(8, 9) with mock.patch.object(position, 'restrict_to_grid') as restrict_to_grid: mower = Mower(position, Orientation('W'), grid_size=(10, 10)) restrict_to_grid.assert_called_once() assert (mower.x, mower.y) == (8, 9) assert mower.orientation == 'W'
def test_rotate_orientation(src_orientation, rotate_method, dst_orientation): orientation = Orientation(src_orientation) # Check base orientation assert orientation.orientation == src_orientation # Rotate left or right rotate_method = getattr(orientation, rotate_method) rotate_method() # Check orientation after rotation assert orientation.orientation == dst_orientation
def test_step_mower(move, rotate_left_calls, rotate_right_calls, forward_calls): position = Position(8, 9) orientation = Orientation('W') mower = Mower(position, orientation, grid_size=(10, 10)) with mock.patch.object(orientation, 'rotate_left') as rotate_left: with mock.patch.object(orientation, 'rotate_right') as rotate_right: with mock.patch.object(position, 'forward') as forward: mower.step(move) assert rotate_left.call_count == rotate_left_calls assert rotate_right.call_count == rotate_right_calls assert forward.call_count == forward_calls
def _parse_mower_line(line: str, grid_size: Tuple[int, int]) -> Mower: """ Parse and validate initial position (2 space-separated ints) and orientation (N/S/W/E char) of mower. """ try: x, y, orientation = line.split(' ') x, y = _parse_point(x, y) position = Position(x, y) orientation = Orientation(orientation) except ValueError: raise ValueError(f'Invalid initial position and orientation: "{line}"') else: mower = Mower(position, orientation, grid_size) return mower
def test_str_orientation(orientation): assert str(Orientation(orientation)) == orientation
def test_str_mower(): mower = Mower(Position(8, 9), Orientation('W'), grid_size=(10, 10)) assert str(mower) == '8 9 W'