def test_machine_specific_options(self): class FakeMachine(object): @staticmethod def get_option_info(): bool_converter = lambda s: s == 'True' return { 'stroption1': (None, str), 'intoption1': (3, int), 'stroption2': ('abc', str), 'floatoption1': (1, float), 'booloption1': (True, bool_converter), 'booloption2': (False, bool_converter) } defaults = {k: v[0] for k, v in FakeMachine.get_option_info().items()} machine_name = 'machine foo' registry = Registry() registry.register(machine_name, FakeMachine) with patch('plover.config.machine_registry', registry): c = config.Config() # Check default value. actual = c.get_machine_specific_options(machine_name) self.assertEqual(actual, defaults) # Make sure setting a value is reflecting in the getter. options = { 'stroption1': 'something', 'intoption1': 5, 'floatoption1': 5.9, 'booloption1': False, } c.set_machine_specific_options(machine_name, options) actual = c.get_machine_specific_options(machine_name) expected = dict(defaults.items() + options.items()) self.assertEqual(actual, expected) # Test loading a file. Unknown option is ignored. s = '\n'.join(('[machine foo]', 'stroption1 = foo', 'intoption1 = 3', 'booloption1 = True', 'booloption2 = False', 'unknown = True')) f = StringIO(s) c.load(f) expected = { 'stroption1': 'foo', 'intoption1': 3, 'booloption1': True, 'booloption2': False, } expected = dict(defaults.items() + expected.items()) actual = c.get_machine_specific_options(machine_name) self.assertEqual(actual, expected) # Test saving a file. f = StringIO() c.save(f) self.assertEqual(f.getvalue(), s + '\n\n') # Test reading invalid values. s = '\n'.join(['[machine foo]', 'floatoption1 = None', 'booloption2 = True']) f = StringIO(s) c.load(f) expected = { 'floatoption1': 1, 'booloption2': True, } expected = dict(defaults.items() + expected.items()) actual = c.get_machine_specific_options(machine_name) self.assertEqual(actual, expected)
def test_lookup(self): registry = Registry() registry.register('a', 1) self.assertEqual(1, registry.get('a'))
def test_alias(self): registry = Registry() registry.register('a', 1) registry.add_alias('b', 'a') self.assertEqual(registry.resolve_alias('b'), 'a') self.assertEqual(1, registry.get('b'))
def test_all_names(self): registry = Registry() registry.register('a', 1) registry.register('b', 5) registry.add_alias('c', 'b') self.assertEqual(['a', 'b'], sorted(registry.get_all_names()))
def test_unknown_entry(self): registry = Registry() with self.assertRaises(NoSuchMachineException): registry.get('b')