Skip to content

Commit

Permalink
improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Jul 7, 2019
1 parent 6960818 commit a27c8de
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
21 changes: 2 additions & 19 deletions omegaconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,9 @@ def __delitem__(self, key):
def __len__(self):
return self.content.__len__()

@abstractmethod
def __iter__(self):
class MyItems(object):
def __init__(self, l):
self.lst = l
self.iterator = iter(l)

def __iter__(self):
return self

# Python 3 compatibility
def __next__(self):
return self.next()

def next(self):
v = next(self.iterator)
if isinstance(v, BaseNode):
v = v.value()
return v

return MyItems(self.content)
raise NotImplementedError()

def __contains__(self, item):
return self.content.__contains__(item)
Expand Down
3 changes: 3 additions & 0 deletions omegaconf/dictconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def pop(self, key, default=__marker):
def keys(self):
return self.content.keys()

def __iter__(self):
return iter(self.keys())

def items(self):
class MyItems(object):
def __init__(self, m):
Expand Down
21 changes: 21 additions & 0 deletions omegaconf/listconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,24 @@ def __ne__(self, other):
def __hash__(self):
# TODO: should actually iterate
return hash(str(self))

def __iter__(self):
class MyItems(object):
def __init__(self, l):
self.lst = l
self.iterator = iter(l)

def __iter__(self):
return self

# Python 3 compatibility
def __next__(self):
return self.next()

def next(self):
v = next(self.iterator)
if isinstance(v, BaseNode):
v = v.value()
return v

return MyItems(self.content)
16 changes: 16 additions & 0 deletions tests/test_basic_ops_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ def eq(a, b):

@pytest.mark.parametrize('input1, input2', [
(dict(), dict(a=10)),
({}, []),
(dict(a=12), dict(a=13)),
(dict(a=0), dict(b=0)),
(dict(a=12), dict(a=nodes.UntypedNode(13))),
(dict(a=12, b=dict()), dict(a=13, b=dict())),
(dict(a=12, b=dict(c=10)), dict(a=13, b=dict(c=10))),
Expand All @@ -412,6 +414,12 @@ def neq(a, b):
neq(c1, c2)


def test_config_eq_mismatch_types():
c1 = OmegaConf.create({})
c2 = OmegaConf.create([])
assert not Config._config_eq(c1, c2)


def test_dict_not_eq_with_another_class():
assert OmegaConf.create() != "string"

Expand Down Expand Up @@ -483,3 +491,11 @@ def test_frozen_dict_del():
with pytest.raises(FrozenConfigError, match='a'):
del c['a']
assert c == dict(a=10)


def test_hash():
c1 = OmegaConf.create(dict(a=10))
c2 = OmegaConf.create(dict(a=10))
assert hash(c1) == hash(c2)
c2.a = 20
assert hash(c1) != hash(c2)
8 changes: 8 additions & 0 deletions tests/test_basic_ops_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,11 @@ def test_deepcopy():
c1[0] = 10
assert c1[0] == 10
assert c2[0] == 1


def test_hash():
c1 = OmegaConf.create([10])
c2 = OmegaConf.create([10])
assert hash(c1) == hash(c2)
c2[0] = 20
assert hash(c1) != hash(c2)

0 comments on commit a27c8de

Please sign in to comment.