~testing-cabal/testtools/trunk

« back to all changes in this revision

Viewing changes to testtools/matchers/_dict.py

  • Committer: Free Ekanayaka
  • Author(s): Gavin Panella
  • Date: 2017-03-17 18:29:29 UTC
  • Revision ID: git-v1:bb7c7ecfc6ede7160856513db7160f52df40be14
Make KeysEqual usable with no arguments, i.e. match a dict with no keys. (#241)

Make KeysEqual usable with no arguments, i.e. match a dict with no keys.

Show diffs side-by-side

added added

removed removed

Lines of Context:
235
235
    def __init__(self, *expected):
236
236
        """Create a `KeysEqual` Matcher.
237
237
 
238
 
        :param expected: The keys the dict is expected to have.  If a dict,
239
 
            then we use the keys of that dict, if a collection, we assume it
240
 
            is a collection of expected keys.
 
238
        :param expected: The keys the matchee is expected to have. As a
 
239
            special case, if a single argument is specified, and it is a
 
240
            mapping, then we use its keys as the expected set.
241
241
        """
242
242
        super(KeysEqual, self).__init__()
243
 
        try:
244
 
            self.expected = expected[0].keys()
245
 
        except AttributeError:
246
 
            self.expected = list(expected)
 
243
        if len(expected) == 1:
 
244
            try:
 
245
                expected = expected[0].keys()
 
246
            except AttributeError:
 
247
                pass
 
248
        self.expected = list(expected)
247
249
 
248
250
    def __str__(self):
249
251
        return "KeysEqual(%s)" % ', '.join(map(repr, self.expected))