Exemplo n.º 1
0
    def test_unregister_dummy_loader(self):
        def load_text(filename):
            return 'Loading {}...'.format(filename)

        core.register('text', load_text)
        self.assertIn('text', core.loaders)
        core.unregister('text', in_config=False)
        self.assertNotIn('text', core.loaders)
Exemplo 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)
Exemplo n.º 3
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')
Exemplo n.º 4
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')
Exemplo n.º 5
0
def define_test_loader(name):
    """Utility context manager.

    1. Creates a text loader which gets registered to pygame_assets,
    2. Yields the loader,
    3. Cleans up by unregistering the loader from pygame_assets.
    """
    @core.loader(name=name)
    def load_text(filepath):
        with open(filepath, 'r') as textfile:
            text = textfile.read()
        return text

    yield load_text
    core.unregister(name)
Exemplo n.º 6
0
    def test_register_dummy_loader(self):
        with self.assertRaises(AttributeError):
            core.load.text

        def load_text(filename):
            return 'Loading {}...'.format(filename)

        # normally you'd register a real @loader() function.
        core.register('text', load_text)

        self.assertEqual(core.loaders['text'], load_text)
        self.assertEqual('Loading foo.txt...', core.load.text('foo.txt'))

        # cleanup
        core.unregister('text', in_config=False)
Exemplo n.º 7
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')