def should_move(self, current_position): orientation = current_position.orientation if orientation is Orientation.NORTH: return Position(current_position.x, current_position.y + 1, orientation) elif orientation is Orientation.SOUTH: return Position(current_position.x, current_position.y - 1, orientation) elif orientation is Orientation.EAST: return Position(current_position.x + 1, current_position.y, orientation) elif orientation is Orientation.WEST: return Position(current_position.x - 1, current_position.y, orientation) else: raise ValueError("Orientation unknown %s", orientation)
class PositionTestCase(unittest.TestCase): def setUp(self): self.position = Position(1, 1, Orientation.NORTH) def test_should_be_equal(self): other_position = Position(1, 1, Orientation.NORTH) result = self.position.__eq__(other_position) self.assertTrue(result) def test_should_not_be_equal_when_orientation_is_different(self): other_position = Position(1, 1, Orientation.SOUTH) result = self.position.__eq__(other_position) self.assertFalse(result) def test_should_not_be_equal_when_x_is_different(self): other_position = Position(2, 1, Orientation.NORTH) result = self.position.__eq__(other_position) self.assertFalse(result) def test_should_not_be_equal_when_y_is_different(self): other_position = Position(1, 2, Orientation.NORTH) result = self.position.__eq__(other_position) self.assertFalse(result) def test_should_be_same_when_is_equal(self): other_position = Position(1, 1, Orientation.NORTH) result = self.position.is_same(other_position) self.assertTrue(result) def test_should_be_same_when_orientation_is_different(self): other_position = Position(1, 1, Orientation.EAST) result = self.position.is_same(other_position) self.assertTrue(result) def test_should_not_be_same_when_x_is_different(self): other_position = Position(2, 1, Orientation.NORTH) result = self.position.is_same(other_position) self.assertFalse(result) def test_should_not_be_same_when_y_is_different(self): other_position = Position(1, 2, Orientation.NORTH) result = self.position.is_same(other_position) self.assertFalse(result) def test_should_stringify(self): result = self.position.__str__() self.assertEqual(result, "Position={x=1, y=1, orientation=NORTH}")
def test_should_not_move_when_position_is_not_valid(self): expected_next_position = Position(5, 7, Orientation.NORTH) self.mower.should_move = MagicMock(return_value=expected_next_position) self.mower.move = MagicMock(return_value=expected_next_position) self.mediator.is_position_valid = MagicMock(return_value=False) result = self.mediator.handle_move(self.mower) self.mower.should_move.assert_called_once_with() self.mediator.is_position_valid.assert_called_once_with( expected_next_position) self.mower.move.assert_not_called() self.assertEqual(result, self.mower.position)
def test_sequential_main(self): # Given grid = Grid(0, 0, 5, 5) mower1 = Mower(1, 2, Orientation.NORTH) mower2 = Mower(3, 3, Orientation.EAST) mediator = Mediator(grid) mediator.register(mower1) mediator.register(mower2) def to_instruction(string): return Instruction[string] mower1_instructions = map(to_instruction, list("GAGAGAGAA")) mower2_instructions = map(to_instruction, list("AADAADADDA")) # When for instruction1 in mower1_instructions: mediator.send_instruction(instruction1, mower1) for instruction2 in mower2_instructions: mediator.send_instruction(instruction2, mower2) # Then self.assertEqual(mower1.position, Position(1, 3, Orientation.NORTH)) self.assertEqual(mower2.position, Position(5, 1, Orientation.EAST))
def test_should_not_move_when_position_is_always_locked(self): current_position = self.mower.position expected_next_position = Position(5, 7, Orientation.NORTH) self.mower.should_move = MagicMock(return_value=expected_next_position) self.mower.move = MagicMock(return_value=expected_next_position) self.mediator.is_position_valid = MagicMock(return_value=True) self.mediator.is_position_locked = MagicMock(return_value=True) self.mediator.lock.wait = MagicMock(return_value=True) self.mediator.lock.notify_all = MagicMock(return_value=True) result = self.mediator.handle_move(self.mower) self.mower.should_move.assert_called_once_with() self.mower.move.assert_not_called() self.mediator.is_position_valid.assert_called_once_with( expected_next_position) self.assertEqual(self.mediator.is_position_locked.call_count, 2) self.assertEqual(self.mediator.lock.wait.call_count, 2) self.mediator.lock.notify_all.assert_called_once_with() self.assertEqual(result, current_position)
def test_should_wait_once_when_position_is_locked_once(self): expected_next_position = Position(5, 7, Orientation.NORTH) self.mower.should_move = MagicMock(return_value=expected_next_position) self.mower.move = MagicMock(return_value=expected_next_position) self.mediator.is_position_valid = MagicMock(return_value=True) self.mediator.is_position_locked = MagicMock(side_effect=[True, False]) self.mediator.lock.wait = MagicMock(return_value=True) self.mediator.lock.notify_all = MagicMock(return_value=True) result = self.mediator.handle_move(self.mower) self.mower.should_move.assert_called_once_with() self.mower.move.assert_called_once_with() self.mediator.is_position_valid.assert_called_once_with( expected_next_position) self.assertEqual(self.mediator.is_position_locked.call_count, 2) self.mediator.lock.wait.assert_called_once_with( self.mediator.DEFAULT_WAITING_TIME) self.mediator.lock.notify_all.assert_called_once_with() self.assertEqual(result, expected_next_position)
class Mower: def __init__(self, x, y, orientation): self.position = Position(x, y, orientation) self.mower_strategy = DefaultMowerStrategy() def __str__(self): return "Mower={" + \ "position=" + self.position.__str__() + \ "}" def should_move(self): return self.mower_strategy.should_move(self.position) def move(self): self.position = self.should_move() return self.position def turn_right(self): self.position = self.mower_strategy.turn_right(self.position) return self.position def turn_left(self): self.position = self.mower_strategy.turn_left(self.position) return self.position
def test_should_be_same_when_orientation_is_different(self): other_position = Position(1, 1, Orientation.EAST) result = self.position.is_same(other_position) self.assertTrue(result)
def test_should_move_to_east(self): current_position = Position(1, 1, Orientation.EAST) result = self.mower_strategy.should_move(current_position) self.assertEqual(result, Position(2, 1, Orientation.EAST))
def test_position_is_not_valid_grid_check_it_is_not_valid(self): position = Position(-1, 6, Orientation.NORTH) self.mediator.grid.is_position_valid = MagicMock(return_value=False) result = self.mediator.is_position_valid(position) self.mediator.grid.is_position_valid.assert_called_once_with(position) self.assertFalse(result)
def test_position_is_not_valid(self): position = Position(6, 3, Orientation.NORTH) result = self.grid.is_position_valid(position) self.assertIs(result, False)
def test_position_is_valid(self): position = Position(2, 3, Orientation.NORTH) result = self.grid.is_position_valid(position) self.assertIs(result, True)
def test_should_move_to_south(self): current_position = Position(1, 1, Orientation.SOUTH) result = self.mower_strategy.should_move(current_position) self.assertEqual(result, Position(1, 0, Orientation.SOUTH))
def test_should_raise_when_orientation_is_none(self): current_position = Position(1, 1, None) with self.assertRaises(ValueError): self.mower_strategy.should_move(current_position)
def test_position_is_locked_when_no_other_mower_owns_position(self): self.mediator.mower_list.append(self.mower) result = self.mediator.is_position_locked( Position(1, 3, Orientation.SOUTH)) self.assertFalse(result)
def test_should_send_turn_right_instruction(self): expected_position = Position(1, 2, Orientation.EAST) self.mower.turn_right = MagicMock(return_value=expected_position) result = self.mediator.send_instruction(Instruction.D, self.mower) self.mower.turn_right.assert_called_once_with() self.assertEqual(result, expected_position)
def test_should_be_same_when_is_equal(self): other_position = Position(1, 1, Orientation.NORTH) result = self.position.is_same(other_position) self.assertTrue(result)
def test_should_not_be_equal_when_x_is_different(self): other_position = Position(2, 1, Orientation.NORTH) result = self.position.__eq__(other_position) self.assertFalse(result)
def test_should_be_equal(self): other_position = Position(1, 1, Orientation.NORTH) result = self.position.__eq__(other_position) self.assertTrue(result)
def __init__(self, x, y, orientation): self.position = Position(x, y, orientation) self.mower_strategy = DefaultMowerStrategy()
def test_should_send_move_instruction(self): expected_position = Position(1, 2, Orientation.EAST) self.mediator.handle_move = MagicMock(return_value=expected_position) result = self.mediator.send_instruction(Instruction.A, self.mower) self.mediator.handle_move.assert_called_once_with(self.mower) self.assertEqual(result, expected_position)
def test_should_not_be_same_when_y_is_different(self): other_position = Position(1, 2, Orientation.NORTH) result = self.position.is_same(other_position) self.assertFalse(result)
def turn_left(self, current_position): return Position(current_position.x, current_position.y, current_position.orientation.left_orientation())
def setUp(self): self.position = Position(1, 1, Orientation.NORTH)
def turn_right(self, current_position): return Position(current_position.x, current_position.y, current_position.orientation.right_orientation())
def test_should_turn_left(self): current_position = Position(1, 1, Orientation.NORTH) result = self.mower_strategy.turn_left(current_position) self.assertEqual(result, Position(1, 1, Orientation.WEST))