Beispiel #1
0
    def test_when_max_bytes_not_a_number(self):
        self.file.write('.\n1\na')
        self.file.seek(0)

        with self.assertRaisesRegex(
                ValueError, 'expected a positive integer .* at line 3: a'):
            Level.fromfile(self.file, nrows=1, ncols=1)
Beispiel #2
0
    def test_when_too_many_robots(self):
        self.file.write('.u.\n..d\n1\n1')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError,
                                    'too many robots, .* \(2, 3\)'):
            Level.fromfile(self.file, nrows=2, ncols=3)
Beispiel #3
0
    def test_when_points_non_positive(self):
        self.file.write('.\n0\n1')
        self.file.seek(0)

        with self.assertRaisesRegex(
                ValueError, 'expected a positive integer .* at line 2: 0'):
            Level.fromfile(self.file, nrows=1, ncols=1)
Beispiel #4
0
    def test_when_line_too_long(self):
        self.file.write('.\n..\n.')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError,
                                    'line 2 is not 1 character long'):
            Level.fromfile(self.file, nrows=3, ncols=1)
Beispiel #5
0
    def test_when_line_too_short(self):
        self.file.write('.\n..')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError,
                                    'line 1 is not 2 characters long'):
            Level.fromfile(self.file, nrows=2, ncols=2)
Beispiel #6
0
    def test_when_illegal_character(self):
        self.file.write('.\n.\nS')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError,
                                    'illegal character found at line 3'):
            Level.fromfile(self.file, nrows=3, ncols=1)
Beispiel #7
0
    def test_when_max_bytes_too_large(self):
        self.file.write('.\n1\n1000')
        self.file.seek(0)

        with self.assertRaisesRegex(
                ValueError,
                'expected a positive integer in the range \[1, 1000\) at line 3: 1000'
        ):
            Level.fromfile(self.file, nrows=1, ncols=1)
Beispiel #8
0
    def test_when_file_ends_with_a_newline(self):
        self.file.write('10\n')
        self.file.seek(0)

        level = Level.fromfile(self.file, nrows=13, ncols=7)

        self.assertEqual(level.nrows, 13)
        self.assertEqual(level.ncols, 7)
        self.assertEqual(level.points, 1000)
        self.assertEqual(level.max_bytes, 10)
        self.assertEqual(level.robot, (10, 3, 'u'))

        self.assertEqual(len(level.gray_buttons), 1)
        self.assertTrue((5, 3) in level.gray_buttons)

        self.assertEqual(len(level.white_buttons), 4)
        self.assertTrue((1, 3) in level.white_buttons)
        self.assertTrue((3, 3) in level.white_buttons)
        self.assertTrue((7, 3) in level.white_buttons)
        self.assertTrue((9, 3) in level.white_buttons)

        self.assertEqual(len(level.walls), 3)

        self.assertEqual(len(level.inaccessible_spots), 7)
        self.assertTrue((9, 2) in level.inaccessible_spots)
        self.assertTrue((9, 4) in level.inaccessible_spots)
        self.assertTrue((10, 2) in level.inaccessible_spots)
        self.assertTrue((10, 4) in level.inaccessible_spots)
        self.assertTrue((11, 2) in level.inaccessible_spots)
        self.assertTrue((11, 3) in level.inaccessible_spots)
        self.assertTrue((11, 4) in level.inaccessible_spots)
Beispiel #9
0
    def test_example1(self):
        file = io.StringIO()
        file.write('.r.wwwwwwwwwwwwwwwwwwww.\n')
        file.write('100\n')
        file.write('10\n')
        file.seek(0)

        level = Level.fromfile(file, nrows=1, ncols=24)
        re = level()

        for _ in range(25):
            re.step('s')

        # points = 100
        # max_bytes = 10
        # total_buttons = 20
        # buttons = 20

        # bytes = 10
        self.assertEqual(re.score(10), 100)

        # bytes = 5
        self.assertEqual(re.score(5), 200)

        # bytes = 15
        self.assertEqual(re.score(15), 20)

        file.close()
Beispiel #10
0
    def test_when_file_does_not_end_with_a_newline(self):
        self.file.write('10')
        self.file.seek(0)

        level = Level.fromfile(self.file, nrows=13, ncols=7)

        self.assertEqual(level.points, 1000)
        self.assertEqual(level.max_bytes, 10)
Beispiel #11
0
    def setUp(self):
        file = self.file = io.StringIO()
        file.write('..........\n')
        file.write('.***......\n')
        file.write('.*r.w.g.w.\n')
        file.write('.***......\n')
        file.write('..........\n')
        file.write('50\n')
        file.write('11')
        file.seek(0)

        level = Level.fromfile(file, nrows=5, ncols=10)

        # N.B. The sequence of commands "sslsrssssrs"
        # can be used to complete the level.
        self.re = level()
Beispiel #12
0
    def test_when_empty_line(self):
        self.file.write('.\n\n.')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError, 'line 2 is an empty line'):
            Level.fromfile(self.file, nrows=3, ncols=1)
Beispiel #13
0
    def test_when_less_lines(self):
        self.file.write('.\n.\n')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError, 'expected 3 lines but got 2'):
            Level.fromfile(self.file, nrows=3, ncols=1)
Beispiel #14
0
 def test_when_ncols_non_positive(self):
     with self.assertRaisesRegex(ValueError,
                                 'columns .* greater than or equal to 1'):
         Level.fromfile(self.file, ncols=0)
Beispiel #15
0
    def test_when_no_robot(self):
        self.file.write('..****.\n1\n1')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError, 'no robot found'):
            Level.fromfile(self.file, nrows=1, ncols=7)
Beispiel #16
0
    def test_when_improper_wall(self):
        self.file.write('..**.*.\n1\n1')
        self.file.seek(0)

        with self.assertRaisesRegex(ValueError, 'improper wall at \(1, 6\)'):
            Level.fromfile(self.file, nrows=1, ncols=7)