~bzr-pqm/bzr/bzr.dev

5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
1
# Copyright (C) 2010 Canonical Ltd
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Tests of bzrlib test matchers."""
18
19
from testtools.matchers import *
20
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
21
from bzrlib.tests import (
22
    TestCase,
23
    TestCaseWithTransport,
24
    )
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
25
from bzrlib.tests.matchers import *
26
27
28
class StubTree(object):
29
    """Stubg for testing."""
30
31
    def __init__(self, lock_status):
32
        self._is_locked = lock_status
33
34
    def __str__(self):
35
        return u'I am da tree'
36
37
    def is_locked(self):
38
        return self._is_locked
39
40
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
41
class FakeUnlockable(object):
42
    """Something that can be unlocked."""
43
44
    def unlock(self):
45
        pass
46
47
48
class TestReturnsUnlockable(TestCase):
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
49
50
    def test___str__(self):
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
51
        matcher = ReturnsUnlockable(StubTree(True))
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
52
        self.assertEqual(
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
53
            'ReturnsUnlockable(lockable_thing=I am da tree)',
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
54
            str(matcher))
55
56
    def test_match(self):
57
        stub_tree = StubTree(False)
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
58
        matcher = ReturnsUnlockable(stub_tree)
59
        self.assertThat(matcher.match(lambda:FakeUnlockable()), Equals(None))
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
60
61
    def test_mismatch(self):
62
        stub_tree = StubTree(True)
5200.3.2 by Robert Collins
Cleaner matcher matching revised unlocking protocol.
63
        matcher = ReturnsUnlockable(stub_tree)
64
        mismatch = matcher.match(lambda:FakeUnlockable())
5200.3.1 by Robert Collins
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
65
        self.assertNotEqual(None, mismatch)
66
        self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
67
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
68
69
class TestMatchesAncestry(TestCaseWithTransport):
70
71
    def test__str__(self):
72
        matcher = MatchesAncestry("A repository", "arevid")
73
        self.assertEqual(
74
            "MatchesAncestry(repository='A repository', "
75
            "revision_id='arevid')",
76
            str(matcher))
77
78
    def test_match(self):
79
        b = self.make_branch_builder('.')
80
        b.start_series()
81
        revid1 = b.build_commit()
82
        revid2 = b.build_commit()
83
        b.finish_series()
84
        branch = b.get_branch()
85
        m = MatchesAncestry(branch.repository, revid2)
86
        self.assertThat([revid2, revid1], m)
87
        self.assertThat([revid1, revid2], m)
88
        m = MatchesAncestry(branch.repository, revid1)
89
        self.assertThat([revid1], m)
90
        m = MatchesAncestry(branch.repository, "unknown")
5972.3.21 by Jelmer Vernooij
More test fixes.
91
        self.assertThat(["unknown"], m)
5972.3.13 by Jelmer Vernooij
Add matcher for ancestry.
92
93
    def test_mismatch(self):
94
        b = self.make_branch_builder('.')
95
        b.start_series()
96
        revid1 = b.build_commit()
97
        revid2 = b.build_commit()
98
        b.finish_series()
99
        branch = b.get_branch()
100
        m = MatchesAncestry(branch.repository, revid1)
101
        mismatch = m.match([])
102
        self.assertIsNot(None, mismatch)
103
        self.assertEquals(
104
            "mismatched ancestry for revision '%s' was ['%s'], expected []" % (
105
                revid1, revid1),
106
            mismatch.describe())