def int_option_set_fail(self): """Failure to set a int option returns an explanatory message. The typical failure is for an out of range value. """ iopt = config.Int('test_int', 2, minval=-1, maxval=10) message = iopt.set(-2) failUnlessEqual(2, iopt.value) failUnlessEqual("-2 is less than -1", message)
def int_option_init(self): """A Int option requires a name and default value. The name and ints value are available as attributes. The default value is the first int. """ iopt = config.Int('test_int', 2) failUnlessEqual('test_int', iopt.name) failUnlessEqual(2, iopt.default_value)
def int_option_independent_limits(self): """The min and max values can be independently set or omitted. When omitted then no min/max restriction is applied. """ iopt = config.Int('test_int', 2, maxval=10) message = iopt.set(-2) failUnlessEqual(-2, iopt.value) failUnlessEqual("", message) message = iopt.set(11) failUnlessEqual(-2, iopt.value) failUnlessEqual("11 is greater than 10", message)
def int_option_set(self): """A Int option's current value can be modified. The current value is available as the value property. Support for repr is provided. """ iopt = config.Int('test_int', 2) failUnlessEqual(2, iopt.value) ret = iopt.set(-987654321) failUnlessEqual(-987654321, iopt.value) failUnlessEqual('-987654321', repr(iopt)) failUnlessEqual('-987654321', str(iopt))
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 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)
def int_option_set_from_string(self): """Set will accept the string representation of an int.""" iopt = config.Int('test_int', 2) message = iopt.set('99') failUnlessEqual(99, iopt.value) failUnlessEqual("", message)
def int_option_set_bad_type(self): """Set will fail if the type is invalid.""" iopt = config.Int('test_int', 2) message = iopt.set('hello') failUnlessEqual(2, iopt.value) failUnlessEqual("'hello' is not an integer", message)