def test_valid_grid_specs(self):
        try:
            # Test row and column bounds
            sperling.GridGenerator(n_rows=1,
                                   n_columns=1,
                                   charset=sperling.constants.CONSONANTS)
            sperling.GridGenerator(n_rows=1,
                                   n_columns=10,
                                   charset=sperling.constants.CONSONANTS)
            sperling.GridGenerator(n_rows=10,
                                   n_columns=1,
                                   charset=sperling.constants.CONSONANTS)

        except Exception as exc:
            self.fail('Raised unexpected exception: {}'.format(exc))
    def test_create_grid(self):

        n_rows = 5
        n_columns = 4
        charset_id = 'consonants'

        try:
            spec = sperling.GridGenerator(n_rows, n_columns, charset_id)
            grid = spec()

            self.assertIsInstance(grid, list)
            self.assertEqual(len(grid), n_rows)

            for row in grid:
                self.assertEqual(len(row), n_columns)

                for char in row:
                    self.assertIn(char, spec.charset)

            # Verify characters are unique
            self.assertEqual(sum(1 for row in grid for char in row),
                             n_rows * n_columns)

        except Exception as exc:
            self.fail('Raised unexpected exception: {}'.format(exc))
    def __init__(self,
                 screen,
                 font,
                 grid_spec=None,
                 duration_overrides=None,
                 n_trials=1):
        super().__init__(screen, font, duration_overrides, n_trials)

        self._grid_specs = [

            # 3/3
            # sperling.GridSpec(n_rows=2, n_columns=3, charset=sperling.constants.CONSONANTS, allow_repeats=True),

            # 4/4
            # sperling.GridSpec(n_rows=2, n_columns=4, charset=sperling.constants.CONSONANTS, allow_repeats=True),

            # 3/3/3
            # sperling.GridSpec(n_rows=3, n_columns=3, charset=sperling.constants.CONSONANTS, allow_repeats=True),

            # 4/4/4
            sperling.GridSpec(n_rows=3,
                              n_columns=4,
                              charset=sperling.constants.CONSONANTS,
                              allow_repeats=True),
        ]

        self._grid_spec = grid_spec or random.choice(self._grid_specs)
        self._grid_generator = sperling.GridGenerator(
            n_rows=self._grid_spec.n_rows,
            n_columns=self._grid_spec.n_columns,
            charset=self._grid_spec.charset,
            allow_repeats=self._grid_spec.allow_repeats)
    def test_to_string(self):
        generator = sperling.GridGenerator(n_rows=3,
                                           n_columns=4,
                                           charset=sperling.constants.ALPHANUM,
                                           allow_repeats=False)

        expected_string = 'n_rows: {}, n_columns: {}, charset: {}, allow_repeats: {}'.format(
            generator.n_rows, generator.n_columns, generator.charset,
            generator.allow_repeats)
        self.assertEqual(str(generator), expected_string)
    def test_invalid_grid_spec_raises_value_error(self):
        # Check row bounds
        with self.assertRaises(ValueError) as context:
            sperling.GridGenerator(n_rows=0,
                                   n_columns=1,
                                   charset=sperling.constants.CONSONANTS)

        self.assertEqual('Invalid Number of CharacterGrid Rows: Must be > 0.',
                         str(context.exception))

        # Check Column Bounds
        with self.assertRaises(ValueError) as context:
            sperling.GridGenerator(n_rows=1,
                                   n_columns=0,
                                   charset=sperling.constants.CONSONANTS)

        self.assertEqual(
            'Invalid Number of CharacterGrid Columns: Must be > 0.',
            str(context.exception))
    def __init__(self, screen, font, duration_overrides=None, n_trials=1):
        super().__init__(screen, font, duration_overrides, n_trials)

        self._grid_spec = sperling.GridSpec(
            n_rows=2,
            n_columns=3,
            charset=sperling.constants.CONSONANTS,
            allow_repeats=True)
        self._grid_generator = sperling.GridGenerator(
            n_rows=self._grid_spec.n_rows,
            n_columns=self._grid_spec.n_columns,
            charset=self._grid_spec.charset,
            allow_repeats=self._grid_spec.allow_repeats)
Example #7
0
    def test_init(self):
        grid_spec = sperling.GridGenerator(
            n_rows=3, n_columns=4, charset=sperling.constants.CONSONANTS)
        font = pygame.font.SysFont("courier", size=100)
        grid_values = grid_spec()

        try:
            char_grid = sperling.view.CharacterGrid(grid=grid_values,
                                                    font=font)

            # Check internal state after initializer assignments
            self.assertIs(char_grid.grid, grid_values)
            self.assertIs(char_grid.font, font)
            self.assertEqual(char_grid._char_dims, font.size('A'))

            # Verify one sprite per character in grid spec
            self.assertEqual(len(char_grid._sprite_group),
                             grid_spec.n_rows * grid_spec.n_columns)

        except Exception as exc:
            self.fail('Unexpected exception: {}'.format(exc))