/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

[merge] bzr.dev 1987

Show diffs side-by-side

added added

removed removed

Lines of Context:
576
576
            self.fail("%r is an instance of %s rather than %s" % (
577
577
                obj, obj.__class__, kls))
578
578
 
579
 
    def callDeprecated(self, expected, callable, *args, **kwargs):
580
 
        """Assert that a callable is deprecated in a particular way.
 
579
    def _capture_warnings(self, a_callable, *args, **kwargs):
 
580
        """A helper for callDeprecated and applyDeprecated.
581
581
 
582
 
        :param expected: a list of the deprecation warnings expected, in order
583
 
        :param callable: The callable to call
 
582
        :param a_callable: A callable to call.
584
583
        :param args: The positional arguments for the callable
585
584
        :param kwargs: The keyword arguments for the callable
 
585
        :return: A tuple (warnings, result). result is the result of calling
 
586
            a_callable(*args, **kwargs).
586
587
        """
587
588
        local_warnings = []
588
589
        def capture_warnings(msg, cls, stacklevel=None):
 
590
            # we've hooked into a deprecation specific callpath,
 
591
            # only deprecations should getting sent via it.
589
592
            self.assertEqual(cls, DeprecationWarning)
590
593
            local_warnings.append(msg)
591
 
        method = symbol_versioning.warn
 
594
        original_warning_method = symbol_versioning.warn
592
595
        symbol_versioning.set_warning_method(capture_warnings)
593
596
        try:
594
 
            result = callable(*args, **kwargs)
 
597
            result = a_callable(*args, **kwargs)
595
598
        finally:
596
 
            symbol_versioning.set_warning_method(method)
597
 
        self.assertEqual(expected, local_warnings)
 
599
            symbol_versioning.set_warning_method(original_warning_method)
 
600
        return (local_warnings, result)
 
601
 
 
602
    def applyDeprecated(self, deprecation_format, a_callable, *args, **kwargs):
 
603
        """Call a deprecated callable without warning the user.
 
604
 
 
605
        :param deprecation_format: The deprecation format that the callable
 
606
            should have been deprecated with. This is the same type as the 
 
607
            parameter to deprecated_method/deprecated_function. If the 
 
608
            callable is not deprecated with this format, an assertion error
 
609
            will be raised.
 
610
        :param a_callable: A callable to call. This may be a bound method or
 
611
            a regular function. It will be called with *args and **kwargs.
 
612
        :param args: The positional arguments for the callable
 
613
        :param kwargs: The keyword arguments for the callable
 
614
        :return: The result of a_callable(*args, **kwargs)
 
615
        """
 
616
        call_warnings, result = self._capture_warnings(a_callable,
 
617
            *args, **kwargs)
 
618
        expected_first_warning = symbol_versioning.deprecation_string(
 
619
            a_callable, deprecation_format)
 
620
        if len(call_warnings) == 0:
 
621
            self.fail("No assertion generated by call to %s" %
 
622
                a_callable)
 
623
        self.assertEqual(expected_first_warning, call_warnings[0])
 
624
        return result
 
625
 
 
626
    def callDeprecated(self, expected, callable, *args, **kwargs):
 
627
        """Assert that a callable is deprecated in a particular way.
 
628
 
 
629
        This is a very precise test for unusual requirements. The 
 
630
        applyDeprecated helper function is probably more suited for most tests
 
631
        as it allows you to simply specify the deprecation format being used
 
632
        and will ensure that that is issued for the function being called.
 
633
 
 
634
        :param expected: a list of the deprecation warnings expected, in order
 
635
        :param callable: The callable to call
 
636
        :param args: The positional arguments for the callable
 
637
        :param kwargs: The keyword arguments for the callable
 
638
        """
 
639
        call_warnings, result = self._capture_warnings(callable,
 
640
            *args, **kwargs)
 
641
        self.assertEqual(expected, call_warnings)
598
642
        return result
599
643
 
600
644
    def _startLogFile(self):