feat: add parse_bool()
and parse_list()
methods for config object
This commit is contained in:
parent
2deba45588
commit
49e77d7407
2 changed files with 128 additions and 8 deletions
|
@ -17,6 +17,9 @@ from wuttjamaican.testing import FileTestCase
|
|||
|
||||
class TestWuttaConfig(FileTestCase):
|
||||
|
||||
def make_config(self, **kwargs):
|
||||
return mod.WuttaConfig(**kwargs)
|
||||
|
||||
def test_contstructor_basic(self):
|
||||
config = conf.WuttaConfig()
|
||||
self.assertEqual(config.appname, 'wutta')
|
||||
|
@ -410,6 +413,111 @@ configure_logging = true
|
|||
config.setdefault('foo.bar', 'hello world')
|
||||
self.assertEqual(config.get_list('foo.bar'), ['hello', 'world'])
|
||||
|
||||
def test_parse_bool_null(self):
|
||||
config = self.make_config()
|
||||
self.assertIsNone(config.parse_bool(None))
|
||||
|
||||
def test_parse_bool_bool(self):
|
||||
config = self.make_config()
|
||||
self.assertTrue(config.parse_bool(True))
|
||||
self.assertFalse(config.parse_bool(False))
|
||||
|
||||
def test_parse_bool_string_true(self):
|
||||
config = self.make_config()
|
||||
self.assertTrue(config.parse_bool('true'))
|
||||
self.assertTrue(config.parse_bool('yes'))
|
||||
self.assertTrue(config.parse_bool('y'))
|
||||
self.assertTrue(config.parse_bool('on'))
|
||||
self.assertTrue(config.parse_bool('1'))
|
||||
|
||||
def test_parse_bool_string_false(self):
|
||||
config = self.make_config()
|
||||
self.assertFalse(config.parse_bool('false'))
|
||||
self.assertFalse(config.parse_bool('no'))
|
||||
self.assertFalse(config.parse_bool('n'))
|
||||
self.assertFalse(config.parse_bool('off'))
|
||||
self.assertFalse(config.parse_bool('0'))
|
||||
# nb. assume false for unrecognized input
|
||||
self.assertFalse(config.parse_bool('whatever-else'))
|
||||
|
||||
def test_parse_list_null(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list(None)
|
||||
self.assertIsInstance(value, list)
|
||||
self.assertEqual(len(value), 0)
|
||||
|
||||
def test_parse_list_list_instance(self):
|
||||
config = self.make_config()
|
||||
mylist = []
|
||||
value = config.parse_list(mylist)
|
||||
self.assertIs(value, mylist)
|
||||
|
||||
def test_parse_list_single_value(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list('foo')
|
||||
self.assertEqual(len(value), 1)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
|
||||
def test_parse_list_single_value_padded_by_spaces(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list(' foo ')
|
||||
self.assertEqual(len(value), 1)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
|
||||
def test_parse_list_slash_is_not_a_separator(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list('/dev/null')
|
||||
self.assertEqual(len(value), 1)
|
||||
self.assertEqual(value[0], '/dev/null')
|
||||
|
||||
def test_parse_list_multiple_values_separated_by_whitespace(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list('foo bar baz')
|
||||
self.assertEqual(len(value), 3)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
self.assertEqual(value[1], 'bar')
|
||||
self.assertEqual(value[2], 'baz')
|
||||
|
||||
def test_parse_list_multiple_values_separated_by_commas(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list('foo,bar,baz')
|
||||
self.assertEqual(len(value), 3)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
self.assertEqual(value[1], 'bar')
|
||||
self.assertEqual(value[2], 'baz')
|
||||
|
||||
def test_parse_list_multiple_values_separated_by_whitespace_and_commas(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list(' foo, bar baz')
|
||||
self.assertEqual(len(value), 3)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
self.assertEqual(value[1], 'bar')
|
||||
self.assertEqual(value[2], 'baz')
|
||||
|
||||
def test_parse_list_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list("""
|
||||
foo
|
||||
"C:\\some path\\with spaces\\and, a comma",
|
||||
baz
|
||||
""")
|
||||
self.assertEqual(len(value), 3)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
self.assertEqual(value[1], 'C:\\some path\\with spaces\\and, a comma')
|
||||
self.assertEqual(value[2], 'baz')
|
||||
|
||||
def test_parse_list_multiple_values_separated_by_whitespace_and_commas_with_single_quotes(self):
|
||||
config = self.make_config()
|
||||
value = config.parse_list("""
|
||||
foo
|
||||
'C:\\some path\\with spaces\\and, a comma',
|
||||
baz
|
||||
""")
|
||||
self.assertEqual(len(value), 3)
|
||||
self.assertEqual(value[0], 'foo')
|
||||
self.assertEqual(value[1], 'C:\\some path\\with spaces\\and, a comma')
|
||||
self.assertEqual(value[2], 'baz')
|
||||
|
||||
def test_get_app(self):
|
||||
|
||||
# default handler
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue