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)
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)
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()
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)
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)
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')
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')
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')
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)
def test_dir_is_sound(self): self.assertListEqual(get_config().dirs['music'], ['sound'])
def test_default_size_is_20(self): self.assertEqual(get_config().default_font_size, 20) self.assertEqual(self.asset().size, 20)
def test_config_has_meta_dict(self): config = get_config() self.assertIsNotNone(config._meta) self.assertIsInstance(config._meta, dict)
def test_has_default_font_size(self): config = get_config() self.assertIsNotNone(config.default_font_size)
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')
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)
def test_default_size_is_20(self): self.assertEqual(get_config().default_font_size, 20) self.assertAlmostEqual(self.asset().get_height(), 20, delta=10)
def test_dir_is_font(self): self.assertListEqual(get_config().dirs['freetype'], ['font'])
def test_meta_contains_all_parameters(self): config = get_config() for param in ConfigMeta._allowed_params: self.assertIn(param, config._meta)
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')
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))