From de05982393e881e52f2f1f1d407ff58822c06aa2 Mon Sep 17 00:00:00 2001 From: Francois Dang Ngoc Date: Fri, 2 Dec 2016 11:20:28 -0500 Subject: [PATCH] Implemented contains method #97 --- pyhocon/config_tree.py | 3 +++ tests/test_config_tree.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pyhocon/config_tree.py b/pyhocon/config_tree.py index 76ef19fa..37c12e1e 100644 --- a/pyhocon/config_tree.py +++ b/pyhocon/config_tree.py @@ -291,6 +291,9 @@ def __getitem__(self, item): raise KeyError(item) return val + def __contains__(self, item): + return self._get(self.parse_key(item), default=NoneValue) is not NoneValue + def with_fallback(self, config): """ return a new config with fallback on config diff --git a/tests/test_config_tree.py b/tests/test_config_tree.py index 372e8975..251a7ae6 100644 --- a/tests/test_config_tree.py +++ b/tests/test_config_tree.py @@ -198,3 +198,21 @@ def test_plain_ordered_dict(self): d['a']['b.c'] = [OrderedDict(), 2] d['a']['b.c'][0]['c.d'] = 1 assert config_tree.as_plain_ordered_dict() == d + + def test_contains(self): + config_tree = ConfigTree() + config_tree.put('a.b', 5) + config_tree.put('a.c', None) + assert 'a' in config_tree + assert 'a.b' in config_tree + assert 'a.c' in config_tree + assert 'a.b.c' not in config_tree + + def test_contains_with_quoted_keys(self): + config_tree = ConfigTree() + config_tree.put('a.b."c.d"', 5) + assert 'a' in config_tree + assert 'a.b' in config_tree + assert 'a.c' not in config_tree + assert 'a.b."c.d"' in config_tree + assert 'a.b.c.d' not in config_tree