1
# Copyright (C) 2010 Canonical Ltd
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.
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.
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
17
"""Tests of bzrlib test matchers."""
19
from testtools.matchers import *
21
from bzrlib.tests import (
23
TestCaseWithTransport,
25
from bzrlib.tests.matchers import *
28
class StubTree(object):
29
"""Stubg for testing."""
31
def __init__(self, lock_status):
32
self._is_locked = lock_status
35
return u'I am da tree'
38
return self._is_locked
41
class FakeUnlockable(object):
42
"""Something that can be unlocked."""
48
class TestReturnsUnlockable(TestCase):
50
def test___str__(self):
51
matcher = ReturnsUnlockable(StubTree(True))
53
'ReturnsUnlockable(lockable_thing=I am da tree)',
57
stub_tree = StubTree(False)
58
matcher = ReturnsUnlockable(stub_tree)
59
self.assertThat(matcher.match(lambda:FakeUnlockable()), Equals(None))
61
def test_mismatch(self):
62
stub_tree = StubTree(True)
63
matcher = ReturnsUnlockable(stub_tree)
64
mismatch = matcher.match(lambda:FakeUnlockable())
65
self.assertNotEqual(None, mismatch)
66
self.assertThat(mismatch.describe(), Equals("I am da tree is locked"))
69
class TestMatchesAncestry(TestCaseWithTransport):
71
def test__str__(self):
72
matcher = MatchesAncestry("A repository", "arevid")
74
"MatchesAncestry(repository='A repository', "
75
"revision_id='arevid')",
79
b = self.make_branch_builder('.')
81
revid1 = b.build_commit()
82
revid2 = b.build_commit()
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")
91
self.assertThat(["unknown"], m)
93
def test_mismatch(self):
94
b = self.make_branch_builder('.')
96
revid1 = b.build_commit()
97
revid2 = b.build_commit()
99
branch = b.get_branch()
100
m = MatchesAncestry(branch.repository, revid1)
101
mismatch = m.match([])
102
self.assertIsNot(None, mismatch)
104
"mismatched ancestry for revision '%s' was ['%s'], expected []" % (
109
class TestHasLayout(TestCaseWithTransport):
111
def test__str__(self):
112
matcher = HasLayout([("a", "a-id")])
113
self.assertEqual("HasLayout([('a', 'a-id')])", str(matcher))
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'])
119
self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
120
self.assertThat(t, HasLayout(
121
[('', t.get_root_id()),
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)
133
"['a'] != [u'', u'a', u'b/', u'b/c']",
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)
147
"[u'', u'a'] != [u'', u'a', u'b/', u'b/c']",