~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_matchers.py

  • Committer: Martin Pool
  • Date: 2010-02-25 06:17:27 UTC
  • mfrom: (5055 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5057.
  • Revision ID: mbp@sourcefrog.net-20100225061727-4sd9lt0qmdc6087t
merge news

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
21
 
from bzrlib.tests import (
22
 
    TestCase,
23
 
    TestCaseWithTransport,
24
 
    )
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
 
 
41
 
class FakeUnlockable(object):
42
 
    """Something that can be unlocked."""
43
 
 
44
 
    def unlock(self):
45
 
        pass
46
 
 
47
 
 
48
 
class TestReturnsUnlockable(TestCase):
49
 
 
50
 
    def test___str__(self):
51
 
        matcher = ReturnsUnlockable(StubTree(True))
52
 
        self.assertEqual(
53
 
            'ReturnsUnlockable(lockable_thing=I am da tree)',
54
 
            str(matcher))
55
 
 
56
 
    def test_match(self):
57
 
        stub_tree = StubTree(False)
58
 
        matcher = ReturnsUnlockable(stub_tree)
59
 
        self.assertThat(matcher.match(lambda:FakeUnlockable()), Equals(None))
60
 
 
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"))
67
 
 
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")
91
 
        self.assertThat(["unknown"], m)
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())
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'])
119
 
        self.assertThat(t, HasLayout(['', 'a', 'b/', 'b/c']))
120
 
        self.assertThat(t, HasLayout(
121
 
            [('', t.get_root_id()),
122
 
             ('a', 'a-id'),
123
 
             ('b/', 'b-id'),
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(
133
 
            "['a'] != [u'', u'a', u'b/', u'b/c']",
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(
147
 
            "[u'', u'a'] != [u'', u'a', u'b/', u'b/c']",
148
 
            mismatch.describe())