~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_branch.py

  • Committer: Vincent Ladeuil
  • Date: 2010-06-23 08:19:28 UTC
  • mfrom: (5317 +trunk)
  • mto: (5247.1.11 first-try)
  • mto: This revision was merged to the branch mainline in revision 5326.
  • Revision ID: v.ladeuil+lp@free.fr-20100623081928-z9q18q30oo5as831
Merge bzr.dev into cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
136
136
        return "opened branch."
137
137
 
138
138
 
 
139
# Demonstrating how lazy loading is often implemented:
 
140
# A constant string is created.
 
141
SampleSupportedBranchFormatString = "Sample supported branch format."
 
142
 
 
143
# And the format class can then reference the constant to avoid skew.
 
144
class SampleSupportedBranchFormat(_mod_branch.BranchFormat):
 
145
    """A sample supported format."""
 
146
 
 
147
    def get_format_string(self):
 
148
        """See BzrBranchFormat.get_format_string()."""
 
149
        return SampleSupportedBranchFormatString
 
150
 
 
151
    def initialize(self, a_bzrdir, name=None):
 
152
        t = a_bzrdir.get_branch_transport(self, name=name)
 
153
        t.put_bytes('format', self.get_format_string())
 
154
        return 'A branch'
 
155
 
 
156
    def open(self, transport, name=None, _found=False, ignore_fallbacks=False):
 
157
        return "opened supported branch."
 
158
 
 
159
 
139
160
class TestBzrBranchFormat(tests.TestCaseWithTransport):
140
161
    """Tests for the BzrBranchFormat facility."""
141
162
 
152
173
            self.failUnless(isinstance(found_format, format.__class__))
153
174
        check_format(_mod_branch.BzrBranchFormat5(), "bar")
154
175
 
 
176
    def test_find_format_factory(self):
 
177
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
 
178
        SampleSupportedBranchFormat().initialize(dir)
 
179
        factory = _mod_branch.MetaDirBranchFormatFactory(
 
180
            SampleSupportedBranchFormatString,
 
181
            "bzrlib.tests.test_branch", "SampleSupportedBranchFormat")
 
182
        _mod_branch.BranchFormat.register_format(factory)
 
183
        self.addCleanup(_mod_branch.BranchFormat.unregister_format, factory)
 
184
        b = _mod_branch.Branch.open(self.get_url())
 
185
        self.assertEqual(b, "opened supported branch.")
 
186
 
155
187
    def test_find_format_not_branch(self):
156
188
        dir = bzrdir.BzrDirMetaFormat1().initialize(self.get_url())
157
189
        self.assertRaises(errors.NotBranchError,
186
218
        self.make_branch_and_tree('bar')
187
219
 
188
220
 
 
221
#Used by TestMetaDirBranchFormatFactory 
 
222
FakeLazyFormat = None
 
223
 
 
224
 
 
225
class TestMetaDirBranchFormatFactory(tests.TestCase):
 
226
 
 
227
    def test_get_format_string_does_not_load(self):
 
228
        """Formats have a static format string."""
 
229
        factory = _mod_branch.MetaDirBranchFormatFactory("yo", None, None)
 
230
        self.assertEqual("yo", factory.get_format_string())
 
231
 
 
232
    def test_call_loads(self):
 
233
        # __call__ is used by the network_format_registry interface to get a
 
234
        # Format.
 
235
        global FakeLazyFormat
 
236
        del FakeLazyFormat
 
237
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
238
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
239
        self.assertRaises(AttributeError, factory)
 
240
 
 
241
    def test_call_returns_call_of_referenced_object(self):
 
242
        global FakeLazyFormat
 
243
        FakeLazyFormat = lambda:'called'
 
244
        factory = _mod_branch.MetaDirBranchFormatFactory(None,
 
245
            "bzrlib.tests.test_branch", "FakeLazyFormat")
 
246
        self.assertEqual('called', factory())
 
247
 
 
248
 
189
249
class TestBranch67(object):
190
250
    """Common tests for both branch 6 and 7 which are mostly the same."""
191
251