~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())
6072.2.4 by Jelmer Vernooij
tests for matcher
107
108
109
class TestHasLayout(TestCaseWithTransport):
110
111
    def test__str__(self):
112
        matcher = HasLayout([("a", "a-id")])
113
        self.assertEqual("HasLayout([('a', 'a-id')])", str(matcher))
114
115
    def test_match(self):
116
        t = self.make_branch_and_tree('.')
117
        self.build_tree(['a', 'b/', 'b/c'])
118
        t.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
119
        self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
6072.2.4 by Jelmer Vernooij
tests for matcher
120
        self.assertThat(t, HasLayout(
121
            [('', t.get_root_id()),
122
             ('a', 'a-id'),
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
123
             ('b/', 'b-id'),
6072.2.4 by Jelmer Vernooij
tests for matcher
124
             ('b/c', 'c-id')]))
125
126
    def test_mismatch(self):
127
        t = self.make_branch_and_tree('.')
128
        self.build_tree(['a', 'b/', 'b/c'])
129
        t.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
130
        mismatch = HasLayout(['a']).match(t)
131
        self.assertIsNot(None, mismatch)
132
        self.assertEquals(
6110.6.3 by Jelmer Vernooij
review feedback from mgz
133
            "['a'] != [u'', u'a', u'b/', u'b/c']",
6110.6.2 by Jelmer Vernooij
In HasLayout, take into consideration Tree.has_versioned_directories.
134
            mismatch.describe())
135
136
    def test_no_dirs(self):
137
        # Some tree/repository formats do not support versioned directories
138
        t = self.make_branch_and_tree('.')
139
        t.has_versioned_directories = lambda: False
140
        self.build_tree(['a', 'b/', 'b/c'])
141
        t.add(['a', 'b', 'b/c'], ['a-id', 'b-id', 'c-id'])
142
        self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c']).match(t))
143
        self.assertIs(None, HasLayout(['', 'a', 'b/', 'b/c', 'd/']).match(t))
144
        mismatch = HasLayout([u'', u'a', u'd/']).match(t)
145
        self.assertIsNot(None, mismatch)
146
        self.assertEquals(
6110.6.3 by Jelmer Vernooij
review feedback from mgz
147
            "[u'', u'a'] != [u'', u'a', u'b/', u'b/c']",
6072.2.4 by Jelmer Vernooij
tests for matcher
148
            mismatch.describe())