Skip to content

Commit

Permalink
Implemented contains method #97
Browse files Browse the repository at this point in the history
  • Loading branch information
darthbear committed Dec 2, 2016
1 parent a9182a8 commit de05982
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyhocon/config_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/test_config_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit de05982

Please sign in to comment.