From d46f926f14944e05a3a3a49bb24f05d1bcee003a Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Mon, 1 Apr 2013 13:46:13 +0200 Subject: [PATCH] config: Add List ConfigValue and tests. --- mopidy/utils/config.py | 13 +++++++++++++ tests/utils/config_test.py | 17 +++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/mopidy/utils/config.py b/mopidy/utils/config.py index 4009011d..a1a91b88 100644 --- a/mopidy/utils/config.py +++ b/mopidy/utils/config.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals +import re + def validate_choice(value, choices): """Choice validation, normally called in config value's validate().""" @@ -109,3 +111,14 @@ class Boolean(ConfigValue): return 'true' else: return 'false' + + +class List(ConfigValue): + def deserialize(self, value): + if '\n' in value: + return re.split(r'\s*\n\s*', value.strip()) + else: + return re.split(r',\s*', value.strip()) + + def serialize(self, value): + return '\n '.join(value) diff --git a/tests/utils/config_test.py b/tests/utils/config_test.py index 1442395f..bcc80561 100644 --- a/tests/utils/config_test.py +++ b/tests/utils/config_test.py @@ -175,3 +175,20 @@ class BooleanTest(unittest.TestCase): def test_format_masks_secrets(self): value = config.Boolean(secret=True) self.assertEqual('********', value.format('true')) + + +class ListTest(unittest.TestCase): + def test_deserialize_splits_commas(self): + value = config.List() + self.assertEqual(['foo', 'bar', 'baz'], + value.deserialize('foo, bar,baz')) + + def test_deserialize_splits_newlines(self): + value = config.List() + self.assertEqual(['foo,bar', 'bar', 'baz'], + value.deserialize('foo,bar\nbar\nbaz')) + + def test_serialize_joins_by_newlines(self): + value = config.List() + self.assertRegexpMatches(value.serialize(['foo', 'bar', 'baz']), + r'foo\n\s*bar\n\s*baz')