def test_dictionary_config(self): short_path = os.path.normcase(os.path.normpath('~/foo/bar')) full_path = os.path.normcase(os.path.expanduser(os.path.normpath('~/foo/bar'))) dc = DictionaryConfig(short_path) # Path should be expanded. self.assertEqual(dc.path, full_path) # Shortened path is available through short_path. self.assertEqual(dc.short_path, short_path) # Enabled default to True. self.assertEqual(dc.enabled, True) # Check conversion to dict: short path should be used. self.assertEqual(dc.to_dict(), {'path': short_path, 'enabled': True}) # Test replace method. dc = dc.replace(enabled=False) self.assertEqual(dc.path, full_path) self.assertEqual(dc.enabled, False) # Test creation from dict. self.assertEqual(DictionaryConfig.from_dict({'path': short_path, 'enabled': False}), dc)
class DictCommandsTest(unittest.TestCase): def setUp(self): self.spanish = DictionaryConfig('spanish/main.json') self.english = DictionaryConfig('main.json') self.commands = DictionaryConfig('commands.json') self.user = DictionaryConfig('user.json') self.extra = DictionaryConfig('extra.json') self.engine = FakeEngine([ self.user, self.commands, self.english, self.spanish, ]) solo_state[SOLO_ENABLED] = False solo_state[SOLO_DICT_HAS_RUN] = False self.tf = tempfile.NamedTemporaryFile(delete=False) pdc.BACKUP_DICTIONARY_PATH = self.tf.name def tearDown(self): try: os.unlink(self.tf.name) except OSError: # This file gets deleted by the module being tested right now, # leave this here in case that changes to delete temp file # if necessary. pass def test_priority_dict_shortest_path_is_default(self): priority_dict(self.engine, 'main.json') self.assertEqual(self.engine.config['dictionaries'], [ self.english, self.user, self.commands, self.spanish, ]) priority_dict(self.engine, 'spanish/main.json') self.assertEqual(self.engine.config['dictionaries'], [ self.spanish, self.english, self.user, self.commands, ]) def test_priority_dict_multiple(self): priority_dict(self.engine, 'user.json, spanish/main.json, commands.json') self.assertEqual(self.engine.config['dictionaries'], [ self.user, self.spanish, self.commands, self.english, ]) def test_priority_dict_invalid(self): with self.assertRaises(ValueError): priority_dict(self.engine, 'foobar.json') def test_toggle_dict_shortest_path_is_default(self): toggle_dict(self.engine, '+main.json, -spanish/main.json') self.assertEqual(self.engine.config['dictionaries'], [ self.user, self.commands, self.english.replace(enabled=True), self.spanish.replace(enabled=False), ]) def test_toggle_dict_multiple(self): toggle_dict(self.engine, '+spanish/main.json, !commands.json, -user.json') self.assertEqual(self.engine.config['dictionaries'], [ self.user.replace(enabled=False), self.commands.replace(enabled=False), self.english, self.spanish, ]) def test_toggle_dict_invalid_toggle(self): with self.assertRaises(ValueError): toggle_dict(self.engine, '=user.json') def test_toggle_dict_invalid_dictionary(self): with self.assertRaises(ValueError): toggle_dict(self.engine, '+foobar.json') def test_solo_dict(self): solo_dict(self.engine, '+spanish/main.json') self.assertEqual(self.engine.config['dictionaries'], [ self.user.replace(enabled=False), self.commands.replace(enabled=False), self.english.replace(enabled=False), self.spanish, ]) def test_end_solo_dict_doesnt_delete_new_dictionaries(self): solo_dict(self.engine, '+spanish/main.json') # ...then load a new dictionary while in the temporary mode dictionaries = self.engine.config['dictionaries'] dictionaries.append(self.extra) self.engine.config = {'dictionaries': dictionaries} end_solo_dict(self.engine, '') self.assertEqual(self.engine.config['dictionaries'], [ self.user, self.commands, self.english, self.spanish, self.extra, ]) pass def test_backup_dictionaries_to_json_and_reload(self): original_dictionaries = self.engine.config['dictionaries'] #import pdb; pdb.set_trace() backup_dictionary_stack(original_dictionaries, pdc.BACKUP_DICTIONARY_PATH) toggle_dict(self.engine, '-main.json') self.assertEqual( self.engine.config['dictionaries'], [ self.user, self.commands, self.english.replace(enabled=False), # turned off self.spanish, ]) restored_dictionaries = load_dictionary_stack_from_backup( pdc.BACKUP_DICTIONARY_PATH) self.engine.config = {'dictionaries': restored_dictionaries} self.assertEqual( self.engine.config['dictionaries'], [ self.user, self.commands, self.english, # turned back on again after restore self.spanish, ]) backup_dictionary_stack([], pdc.BACKUP_DICTIONARY_PATH) #clear the file for the next test def test_backed_up_dictionaries_restored_after_solo_if_backup_exists(self): toggle_dict(self.engine, '-main.json') #turned off before backup... original_dictionaries = self.engine.config['dictionaries'] backup_dictionary_stack(original_dictionaries, pdc.BACKUP_DICTIONARY_PATH) toggle_dict(self.engine, '+main.json') #but normal before solo_dict #Now that there's a backup file, do the first solo_dict since we've run... solo_dict(self.engine, '+spanish/main.json') end_solo_dict(self.engine, '') self.assertEqual( self.engine.config['dictionaries'], [ self.user, self.commands, self.english.replace( enabled=False), # turned back off again after restore self.spanish, ]) def test_end_solo_dict_restores_previous_state(self): toggle_dict(self.engine, '-main.json') solo_dict(self.engine, '+spanish/main.json') end_solo_dict(self.engine, '') self.assertEqual(self.engine.config['dictionaries'], [ self.user, self.commands, self.english.replace(enabled=False), self.spanish, ]) def test_end_solo_dict_without_first_doing_solo_1(self): backup_dictionary_stack([ self.spanish.replace(enabled=False), self.user.replace(enabled=False), ], pdc.BACKUP_DICTIONARY_PATH) end_solo_dict(self.engine, '') self.assertEqual(self.engine.config['dictionaries'], [ self.user.replace(enabled=False), self.commands, self.english, self.spanish.replace(enabled=False), ]) def test_end_solo_dict_without_first_doing_solo_2(self): backup_dictionary_stack([ self.extra, self.english.replace(enabled=False), ], pdc.BACKUP_DICTIONARY_PATH) end_solo_dict(self.engine, '') self.assertEqual(self.engine.config['dictionaries'], [ self.user, self.commands, self.english.replace(enabled=False), self.spanish, ])