Ejemplo n.º 1
0
    def test_load_asset(self):
        loader_name = 'text'
        get_config().add_default_dir(loader_name)

        filename = 'test.txt'
        content = 'TEST!'

        # define a get_asset function
        def get_text(filepath):
            with open(filepath) as textfile:
                text = textfile.read()
            return text

        # get search_paths
        search_paths = get_config().search_paths(loader_name, filename)

        # write in the test file.
        text_path = search_paths[0]
        with open(text_path, 'w') as textfile:
            textfile.write(content)

        # use the core load_asset function to load the text asset
        text = core.load_asset(get_text, filename, search_paths)
        self.assertEqual(content, text)

        # cleanup
        os.remove(text_path)
        get_config().remove_dirs(loader_name)
Ejemplo n.º 2
0
    def test_unregister_loader_removes_loader_name_from_config(self):
        @core.loader(name='text')
        def load_text(filepath):
            with open(filepath, 'r') as textfile:
                text = textfile.read()
            return text

        self.assertIn('text', get_config().dirs)
        core.unregister('text')
        self.assertNotIn('text', get_config().dirs)
Ejemplo n.º 3
0
 def test_setup_sets_test_config(self):
     test_case = TestCase()
     self.assertEqual(get_environ_config(), None)
     test_case.setUp()
     self.assertEqual(get_environ_config(), 'test')
     self.assertEqual(get_config(), TestConfig())
     test_case.tearDown()
Ejemplo n.º 4
0
 def test_define_test_text_loader(self):
     config = get_config()
     with define_test_text_loader() as load_text:
         self.assertIn('text', load)
         self.assertIn('text', config.dirs)
         text = load_text('test.txt')
         self.assertEqual('TEST!', text)
     self.assertNotIn('text', load)
     self.assertNotIn('text', config.dirs)
Ejemplo n.º 5
0
    def test_load_existing_asset(self):
        with define_test_loader('text'):
            # write a test file.
            text_path = get_config().search_paths('text', 'test.txt')[0]
            with open(text_path, 'w') as textfile:
                textfile.write('TEST!')

            # load the asset using the pygame_assets.load API.
            text = load.text('test.txt')
            self.assertEqual('TEST!', text)
Ejemplo n.º 6
0
    def test_define_custom_search_dirs(self):
        config = get_config()
        self.assertNotIn('text', config.dirs)

        @core.loader(dirs=['levels', 'scenarios'])
        def text(filepath):
            pass

        self.assertListEqual(config.dirs['text'], ['levels', 'scenarios'])

        core.unregister('text')
Ejemplo n.º 7
0
    def test_new_loader_adds_default_dir_list_to_config(self):
        config = get_config()
        self.assertNotIn('special', config.dirs)

        @core.loader()
        def special(filepath):
            pass

        self.assertIn('special', config.dirs)
        self.assertListEqual(config.dirs['special'], ['special'])

        core.unregister('special')
Ejemplo n.º 8
0
    def test_define_custom_name(self):
        config = get_config()
        self.assertNotIn('fantastic', config.dirs)

        @core.loader(name='fantastic')
        def special(filepath):
            pass

        self.assertIn('fantastic', config.dirs)
        # the load object has registered the loader using the custom name
        self.assertIsNotNone(load.fantastic)
        with self.assertRaises(AttributeError):
            # the function name was not used
            load.special

        core.unregister('fantastic')
Ejemplo n.º 9
0
def change_config(*attributes, config=None):
    """Utility context manager.

    Use when you are writing an attribute of the config returned by
    get_config(). It ensures that the attribute will be kept the same
    before and after the write -- useful to keep tests independant.

    Example
    -------
    ```
    with change_config('default_setting') as config:
        config.default_setting = 'foo'
        # do some asserts...
    # now the value of config.default_setting is not 'foo' anymore.
    ```

    Returns
    -------
    config : Config

    Parameters
    ----------
    *attributes : list of str
        The names of the config attributes that are going to be written during
        tests.
    config : Config, optional, kwarg only.
        You can pass a config object to use, default behavior is to
        use get_config().

    """
    if config is None:
        config = get_config()
    old_values = {attr: getattr(config, attr) for attr in attributes}
    try:
        yield config
    finally:
        for attr, old_value in old_values.items():
            setattr(config, attr, old_value)
Ejemplo n.º 10
0
 def test_dir_is_sound(self):
     self.assertListEqual(get_config().dirs['music'], ['sound'])
Ejemplo n.º 11
0
 def test_default_size_is_20(self):
     self.assertEqual(get_config().default_font_size, 20)
     self.assertEqual(self.asset().size, 20)
Ejemplo n.º 12
0
 def test_config_has_meta_dict(self):
     config = get_config()
     self.assertIsNotNone(config._meta)
     self.assertIsInstance(config._meta, dict)
Ejemplo n.º 13
0
 def test_has_default_font_size(self):
     config = get_config()
     self.assertIsNotNone(config.default_font_size)
Ejemplo n.º 14
0
 def test_set_parameter_propagates_accross_get_config(self):
     name = ConfigMeta._allowed_params[0]
     with change_config(name) as config:
         setattr(config, name, 'other')
         self.assertEqual(getattr(get_config(), name), 'other')
Ejemplo n.º 15
0
 def test_change_config_with_preset_config(self):
     config = get_config()
     with change_config('name', config=config):
         old_value = config.name
         config.name = 'foo'
     self.assertEqual(old_value, config.name)
Ejemplo n.º 16
0
 def test_default_size_is_20(self):
     self.assertEqual(get_config().default_font_size, 20)
     self.assertAlmostEqual(self.asset().get_height(), 20, delta=10)
Ejemplo n.º 17
0
 def test_dir_is_font(self):
     self.assertListEqual(get_config().dirs['freetype'], ['font'])
Ejemplo n.º 18
0
 def test_meta_contains_all_parameters(self):
     config = get_config()
     for param in ConfigMeta._allowed_params:
         self.assertIn(param, config._meta)
Ejemplo n.º 19
0
 def test_load_default_config(self):
     explicit_config = get_config('default')
     self.assertEqual(explicit_config.name, 'default')
     no_args_config = get_config()
     self.assertEqual(no_args_config.name, 'default')
Ejemplo n.º 20
0
 def test_get_parameter_through_dot_notation(self):
     config = get_config()
     for param in config._meta:
         self.assertEqual(getattr(config, param), config._meta.get(param))