コード例 #1
0
ファイル: test_lawn.py プロジェクト: GildasPhilippe/Mowers
 def test_add_mower(self):
     mower1 = Mower(0, 0, NORTH)
     mower2 = Mower(3, 4, EAST)
     lawn = Lawn(5, 5)
     lawn.add_mower(mower1)
     lawn.add_mower(mower2)
     self.assertEqual([mower1, mower2], lawn.mowers)
     self.assertEqual(mower1, lawn.mowers_positions["[0, 0]"])
     self.assertEqual(mower2, lawn.mowers_positions["[3, 4]"])
コード例 #2
0
 def test_turn_left(self):
     mower = Mower(0, 0, NORTH)
     mower.turn_left()
     self.assertEqual(WEST, mower.orientation)
     mower.turn_left()
     self.assertEqual(SOUTH, mower.orientation)
     mower.turn_left()
     self.assertEqual(EAST, mower.orientation)
     mower.turn_left()
     self.assertEqual(NORTH, mower.orientation)
コード例 #3
0
 def test_run(self):
     simulator = Simulator()
     mower1 = Mower(1, 2, NORTH, list("LFLFLFLFF"))
     mower2 = Mower(3, 3, EAST, list("FFRFFRFRRF"))
     simulator.lawn = Lawn(5, 5, [mower1, mower2])
     result = simulator.run()
     expected = "1 3 N\n5 1 E"
     self.assertEqual(expected, result)
     self.assertEqual((1, 3, NORTH),
                      (mower1.x, mower1.y, mower1.orientation))
     self.assertEqual((5, 1, EAST),
                      (mower2.x, mower2.y, mower2.orientation))
コード例 #4
0
 def test_get_next_position(self):
     mower = Mower(3, 3, NORTH)
     self.assertEqual((3, 4), mower.get_next_position())
     mower.orientation = SOUTH
     self.assertEqual((3, 2), mower.get_next_position())
     mower.orientation = WEST
     self.assertEqual((2, 3), mower.get_next_position())
     mower.orientation = EAST
     self.assertEqual((4, 3), mower.get_next_position())
コード例 #5
0
    def parse_input(input):
        """Parse a string input, check its consistency and return a made lawn out of it"""
        lines = input.split("\n")
        width, height = map(int, lines[0].split(" "))

        if width <= 0 or height <= 0:
            raise ValueError("Lawn area should be strictly positive")

        mowers = []
        for i in range((len(lines) - 1) // 2):
            x, y, orientation = lines[2 * i + 1].split(" ")
            x, y = int(x), int(y)
            movements = list(lines[2 * i + 2])

            input_verification = [
                0 <= x <= width, 0 <= y <= height, orientation
                in [NORTH, SOUTH, WEST, EAST]
            ] + list(
                map(lambda move: move in [MOVE_FORWARD, TURN_LEFT, TURN_RIGHT],
                    movements))

            mowers.append(Mower(x, y, orientation, movements))

            if not all(input_verification):
                raise ValueError("Wrong input for mower number {}".format(i))

        return Lawn(width, height, mowers)
コード例 #6
0
ファイル: test_lawn.py プロジェクト: GildasPhilippe/Mowers
    def test_move_mower(self):
        mower = Mower(0, 0, WEST)
        lawn = Lawn(5, 5, [mower])
        self.assertEqual(mower, lawn.mowers_positions["[0, 0]"])

        # Should be blocked on west border
        lawn.move_mower(mower, MOVE_FORWARD)
        self.assertEqual(mower, lawn.mowers_positions["[0, 0]"])
        self.assertEqual((mower.x, mower.y), (0, 0))
        self.assertEqual(mower.orientation, WEST)

        # Should turn right correctly
        lawn.move_mower(mower, TURN_LEFT)
        self.assertEqual(mower, lawn.mowers_positions["[0, 0]"])
        self.assertEqual((mower.x, mower.y), (0, 0))
        self.assertEqual(mower.orientation, SOUTH)

        # Should be blocked on South Border
        lawn.move_mower(mower, MOVE_FORWARD)
        self.assertEqual(mower, lawn.mowers_positions["[0, 0]"])
        self.assertEqual((mower.x, mower.y), (0, 0))
        self.assertEqual(mower.orientation, SOUTH)

        # Should turn left and move forward correctly
        lawn.move_mower(mower, TURN_LEFT)
        lawn.move_mower(mower, MOVE_FORWARD)
        self.assertEqual(mower, lawn.mowers_positions["[1, 0]"])
        self.assertEqual((mower.x, mower.y), (1, 0))
        with self.assertRaises(Exception):
            lawn.mowers_positions["[0, 0]"]

        # Should be blocked by second mower
        lawn.add_mower(Mower(1, 1))
        lawn.move_mower(mower, TURN_LEFT)
        lawn.move_mower(mower, MOVE_FORWARD)
        self.assertEqual((mower.x, mower.y), (1, 0))
コード例 #7
0
 def test_make_output(self):
     simulator = Simulator()
     mowers = [Mower(1, 2, NORTH), Mower(6, 7, SOUTH), Mower(0, 0, EAST)]
     self.assertEqual("1 2 N\n6 7 S\n0 0 E", simulator.make_output(mowers))
コード例 #8
0
 def test_move_to(self):
     mower = Mower(0, 0, NORTH)
     mower.move_to(3, 5)
     self.assertEqual(3, mower.x)
     self.assertEqual(5, mower.y)