def bool_option_init(self): """A Bool option requires a name and default value. The name and default value are available as attributes. """ bopt = config.Bool('test_bool', False) failUnlessEqual('test_bool', bopt.name) failUnless(bopt.default_value is False) bopt = config.Bool('test_bool', True) failUnless(bopt.default_value is True)
def bool_option_set_fail(self): """Failure to set a bool option returns an explanatory message.""" bopt = config.Bool('test_bool', False) message = bopt.set('wibble') failIf(bopt.value is True) failUnlessEqual( "Invalid value ('wibble'), valid are: true, false, 1, 0", message)
def bool_option_descr(self): """A Bool option provides support for UI fields. This helps support the UI configuration panel. """ bopt = config.Bool('test_bool', False, description='A bool option') kwargs = bopt.simple_field_args() failUnlessEqual('test_bool', kwargs['prefix']) failUnlessEqual('A bool option', kwargs['description']) failUnlessEqual([], bopt.values())
def bool_option_set_str(self): """A Bool option's value can be set using strings True and False. This makes setting from a configuration file easier. Spaces are stripped and the case is important. """ bopt = config.Bool('test_bool', False) ret = bopt.set(' True ') failUnless(bopt.value is True) failUnlessEqual('', ret) ret = bopt.set(' false ') failUnless(bopt.value is False) failUnlessEqual('', ret)
def config_get_options(self): """The options_ method provides access to the underlying dict.""" conf = config.Config('test_conf') bopt = config.Bool('test_bool', True) copt = config.Choice('test_choice', choices=('one', 'two', 'three')) iopt = config.Int('test_int', 2) conf.add_(bopt) conf.add_(copt) conf.add_(iopt) options = conf.options_() failUnless(options['test_bool'].value is True) failUnlessEqual(2, options['test_int'].value) failUnlessEqual('one', options['test_choice'].value)
def bool_option_set(self): """A Bool option's current value can be modified. The current value is available as the value property. Support for repr is provided. """ bopt = config.Bool('test_bool', False) failUnless(bopt.value is False) ret = bopt.set(True) failUnless(bopt.default_value is False) failUnless(bopt.value is True) failUnlessEqual('', ret) failUnlessEqual('True', repr(bopt)) failUnlessEqual('True', str(bopt))
def bool_option_descr(self): """A Bool option can be given a descripion. Multi-line descriptions are encouraged. The description attribute holds the text, including newlines and leading whitespace. """ bopt = config.Bool('test_bool', False, description=''' This is a test configuration item. It is truthy. ''') failUnlessEqual('test_bool', bopt.name) failUnlessEqual( '\n This is a test configuration item.\n' ' It is truthy.\n ', bopt.description)
def config_add(self): """Options can be accessed using the get method. Option values appear as attributes of the Config object. """ conf = config.Config('test_conf') bopt = config.Bool('test_bool', True) copt = config.Choice('test_choice', choices=('one', 'two', 'three')) iopt = config.Int('test_int', 2) conf.add_(bopt) conf.add_(copt) conf.add_(iopt) failUnless(conf.get_('test_bool').value is True) failUnlessEqual('one', conf.get_('test_choice').value) failUnlessEqual(2, conf.get_('test_int').value)
def config_save_load(self): """The config can be saved and loaded. The config.d directory is created as required. """ conf = config.Config('test_conf') bopt = config.Bool('test_bool', True) copt = config.Choice('test_choice', choices=('one', 'two', 'three')) iopt = config.Int('test_int', 2) conf.add_(bopt) conf.add_(copt) conf.add_(iopt) home = Path(__file__).resolve().parent / 'rt_test_data' config_d = home / '.vim/config.d' if config_d.exists(): try: shutil.rmtree(config_d) except OSError: # I do not understand, but running under Cygwin I have seen # PermissionError raised when shtil.rmtree tries # os.rmdir(config_d). Yet the following succeeds! os.rmdir(config_d) conf.save_() bopt.set(False) iopt.set(99) copt.set('three') failUnless(conf.test_bool is False) failUnlessEqual('three', conf.test_choice) failUnlessEqual(99, conf.test_int) conf.load_() failUnless(conf.test_bool is True) failUnlessEqual('one', conf.test_choice) failUnlessEqual(2, conf.test_int)