~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/matchers.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-17 21:39:37 UTC
  • mfrom: (6072.2.5 match-tree-layout)
  • Revision ID: pqm@pqm.ubuntu.com-20110817213937-lzul6u2qz1svvgxm
(jelmer) Add a HasLayout matcher. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
"""
28
28
 
29
29
__all__ = [
 
30
    'HasLayout',
30
31
    'MatchesAncestry',
31
32
    'ReturnsUnlockable',
32
33
    ]
35
36
    revision as _mod_revision,
36
37
    )
37
38
 
38
 
from testtools.matchers import Mismatch, Matcher
 
39
from testtools.matchers import Equals, Mismatch, Matcher
39
40
 
40
41
 
41
42
class ReturnsUnlockable(Matcher):
112
113
        finally:
113
114
            self.repository.unlock()
114
115
        if sorted(got) != sorted(expected):
115
 
            return _AncestryMismatch(self.revision_id, sorted(got), sorted(expected))
 
116
            return _AncestryMismatch(self.revision_id, sorted(got),
 
117
                sorted(expected))
 
118
 
 
119
 
 
120
class HasLayout(Matcher):
 
121
    """A matcher that checks if a tree has a specific layout.
 
122
 
 
123
    :ivar entries: List of expected entries, as (path, file_id) pairs.
 
124
    """
 
125
 
 
126
    def __init__(self, entries):
 
127
        Matcher.__init__(self)
 
128
        self.entries = entries
 
129
 
 
130
    def get_tree_layout(self, tree):
 
131
        """Get the (path, file_id) pairs for the current tree."""
 
132
        tree.lock_read()
 
133
        try:
 
134
            return [(path, ie.file_id) for path, ie
 
135
                    in tree.iter_entries_by_dir()]
 
136
        finally:
 
137
            tree.unlock()
 
138
 
 
139
    def __str__(self):
 
140
        return ('HasLayout(%r)' % self.entries)
 
141
 
 
142
    def match(self, tree):
 
143
        actual = self.get_tree_layout(tree)
 
144
        if self.entries and isinstance(self.entries[0], basestring):
 
145
            actual = [path for (path, fileid) in actual]
 
146
        return Equals(actual).match(self.entries)