Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Oct 13, 2019
1 parent e328943 commit a1afb71
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions jaraco/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

try:
import collections.abc
except ImportError:
except ImportError: # pragma: nocover
# Python 2.7
collections.abc = collections

Expand All @@ -34,6 +34,14 @@ class Projection(collections.abc.Mapping):
>>> sorted(list(prj.keys()))
['a', 'c']
Attempting to access a key not in the projection
results in a KeyError.
>>> prj['b']
Traceback (most recent call last):
...
KeyError: 'b'
Use the projection to update another dict.
>>> target = {'a': 2, 'b': 2}
Expand Down Expand Up @@ -73,6 +81,10 @@ class DictFilter(object):
>>> filtered = DictFilter(sample, ['a', 'c'])
>>> filtered == {'a': 1, 'c': 3}
True
>>> set(filtered.values()) == {1, 3}
True
>>> set(filtered.items()) == {('a', 1), ('c', 3)}
True
One can also filter by a regular expression pattern
Expand All @@ -85,14 +97,20 @@ class DictFilter(object):
>>> filtered == {'a': 1, 'b': 2, 'c': 3, 'd': 4}
True
>>> filtered['e']
Traceback (most recent call last):
...
KeyError: 'e'
Also note that DictFilter keeps a reference to the original dict, so
if you modify the original dict, that could modify the filtered dict.
>>> del sample['d']
>>> del sample['a']
>>> filtered == {'b': 2, 'c': 3}
True
>>> filtered != {'b': 2, 'c': 3}
False
"""

def __init__(self, dict, include_keys=[], include_pattern=None):
Expand All @@ -118,13 +136,11 @@ def keys(self):
return self.include_keys.intersection(self.dict.keys())

def values(self):
keys = self.keys()
values = map(self.dict.get, keys)
return values
return map(self.dict.get, self.keys())

def __getitem__(self, i):
if i not in self.include_keys:
return KeyError, i
raise KeyError(i)
return self.dict[i]

def items(self):
Expand Down Expand Up @@ -297,7 +313,7 @@ class KeyTransformingDict(dict):
"""

@staticmethod
def transform_key(key):
def transform_key(key): # pragma: nocover
return key

def __init__(self, *args, **kargs):
Expand Down Expand Up @@ -415,6 +431,11 @@ class FoldedCaseKeyedDict(KeyTransformingDict):
>>> print(d.matching_key_for('this'))
This
>>> d.matching_key_for('missing')
Traceback (most recent call last):
...
KeyError: 'missing'
"""

@staticmethod
Expand Down Expand Up @@ -702,6 +723,16 @@ class FrozenDict(collections.abc.Mapping, collections.abc.Hashable):
True
>>> dict(a=1, b=2) == a
True
>>> 'a' in a
True
>>> type(hash(a)) is type(0)
True
>>> set(iter(a)) == {'a', 'b'}
True
>>> len(a)
2
>>> a['a'] == a.get('a') == 1
True
>>> a['c'] = 3
Traceback (most recent call last):
Expand Down Expand Up @@ -745,7 +776,7 @@ def __contains__(self, key):

# Hashable
def __hash__(self):
return hash(tuple(sorted(self.__data.iteritems())))
return hash(tuple(sorted(six.iteritems(self.__data))))

# Mapping
def __iter__(self):
Expand Down

0 comments on commit a1afb71

Please sign in to comment.