~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 18:13:57 UTC
  • mfrom: (5268.7.29 transport-segments)
  • Revision ID: pqm@pqm.ubuntu.com-20110817181357-y5q5eth1hk8bl3om
(jelmer) Allow specifying the colocated branch to use in the branch URL,
 and retrieving the branch name using ControlDir._get_selected_branch.
 (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
"""
28
28
 
29
29
__all__ = [
30
 
    'ReturnsCallableLeavingObjectUnlocked',
 
30
    'MatchesAncestry',
 
31
    'ReturnsUnlockable',
31
32
    ]
32
33
 
 
34
from bzrlib import (
 
35
    revision as _mod_revision,
 
36
    )
 
37
 
33
38
from testtools.matchers import Mismatch, Matcher
34
39
 
35
40
 
36
 
class ReturnsCallableLeavingObjectUnlocked(Matcher):
 
41
class ReturnsUnlockable(Matcher):
37
42
    """A matcher that checks for the pattern we want lock* methods to have:
38
43
 
39
 
    They should return a callable.
40
 
    Calling that callable should unlock the original object.
 
44
    They should return an object with an unlock() method.
 
45
    Calling that method should unlock the original object.
41
46
 
42
47
    :ivar lockable_thing: The object which can be locked that will be
43
48
        inspected.
48
53
        self.lockable_thing = lockable_thing
49
54
 
50
55
    def __str__(self):
51
 
        return ('ReturnsCallableLeavingObjectUnlocked(lockable_thing=%s)' % 
 
56
        return ('ReturnsUnlockable(lockable_thing=%s)' % 
52
57
            self.lockable_thing)
53
58
 
54
59
    def match(self, lock_method):
55
 
        lock_method()()
 
60
        lock_method().unlock()
56
61
        if self.lockable_thing.is_locked():
57
62
            return _IsLocked(self.lockable_thing)
58
63
        return None
66
71
 
67
72
    def describe(self):
68
73
        return "%s is locked" % self.lockable_thing
 
74
 
 
75
 
 
76
class _AncestryMismatch(Mismatch):
 
77
    """Ancestry matching mismatch."""
 
78
 
 
79
    def __init__(self, tip_revision, got, expected):
 
80
        self.tip_revision = tip_revision
 
81
        self.got = got
 
82
        self.expected = expected
 
83
 
 
84
    def describe(self):
 
85
        return "mismatched ancestry for revision %r was %r, expected %r" % (
 
86
            self.tip_revision, self.got, self.expected)
 
87
 
 
88
 
 
89
class MatchesAncestry(Matcher):
 
90
    """A matcher that checks the ancestry of a particular revision.
 
91
 
 
92
    :ivar graph: Graph in which to check the ancestry
 
93
    :ivar revision_id: Revision id of the revision
 
94
    """
 
95
 
 
96
    def __init__(self, repository, revision_id):
 
97
        Matcher.__init__(self)
 
98
        self.repository = repository
 
99
        self.revision_id = revision_id
 
100
 
 
101
    def __str__(self):
 
102
        return ('MatchesAncestry(repository=%r, revision_id=%r)' % (
 
103
            self.repository, self.revision_id))
 
104
 
 
105
    def match(self, expected):
 
106
        self.repository.lock_read()
 
107
        try:
 
108
            graph = self.repository.get_graph()
 
109
            got = [r for r, p in graph.iter_ancestry([self.revision_id])]
 
110
            if _mod_revision.NULL_REVISION in got:
 
111
                got.remove(_mod_revision.NULL_REVISION)
 
112
        finally:
 
113
            self.repository.unlock()
 
114
        if sorted(got) != sorted(expected):
 
115
            return _AncestryMismatch(self.revision_id, sorted(got), sorted(expected))