# HG changeset patch # User Ludovic Chabant # Date 1421610798 28800 # Node ID f9845123737194e000f1758526725ac4e9aa9483 # Parent 29165f2f315d782d087e088182a974b389eb57ed internal: Add ability to get a default value if a config value doesn't exist. tests: Add some configuration tests. diff -r 29165f2f315d -r f98451237371 piecrust/configuration.py --- a/piecrust/configuration.py Thu Jan 15 22:54:59 2015 -0800 +++ b/piecrust/configuration.py Sun Jan 18 11:53:18 2015 -0800 @@ -39,7 +39,7 @@ def getAll(self): return self.get() - def get(self, key_path=None): + def get(self, key_path=None, default_value=None): self._ensureLoaded() if key_path is None: return self._values @@ -48,7 +48,7 @@ for b in bits: cur = cur.get(b) if cur is None: - return None + return default_value return cur def set(self, key_path, value): diff -r 29165f2f315d -r f98451237371 tests/test_configuration.py --- a/tests/test_configuration.py Thu Jan 15 22:54:59 2015 -0800 +++ b/tests/test_configuration.py Sun Jan 18 11:53:18 2015 -0800 @@ -60,6 +60,26 @@ assert config.has('baz') is False +def test_config_deep_set_non_existing(): + config = Configuration({'foo': 'bar'}) + assert config.get('baz') is None + config.set('baz/or/whatever', 'something') + assert config.has('baz') is True + assert config.has('baz/or') is True + assert config.get('baz/or/whatever') == 'something' + + +def test_config_deep_set_existing(): + config = Configuration({'foo': 'bar', 'baz': {'wat': 'nothing'}}) + assert config.has('baz') is True + assert config.get('baz/wat') == 'nothing' + assert config.get('baz/or') is None + config.set('baz/or/whatever', 'something') + assert config.has('baz') is True + assert config.has('baz/or') is True + assert config.get('baz/or/whatever') == 'something' + + @pytest.mark.parametrize('local, incoming, expected', [ ({}, {}, {}), ({'foo': 'bar'}, {}, {'foo': 'bar'}),