Ejemplo n.º 1
0
 def test_suppresses(self):
     """
     Any warnings emitted by a call to a function passed to
     L{_collectWarnings} are not actually emitted to the warning system.
     """
     output = StringIO()
     self.patch(sys, 'stdout', output)
     _collectWarnings(lambda x: None, warnings.warn, "text")
     self.assertEqual(output.getvalue(), "")
Ejemplo n.º 2
0
    def test_callsObserver(self):
        """
        L{_collectWarnings} calls the observer with each emitted warning.
        """
        firstMessage = "dummy calls observer warning"
        secondMessage = firstMessage[::-1]
        events = []
        def f():
            events.append('call')
            warnings.warn(firstMessage)
            warnings.warn(secondMessage)
            events.append('returning')

        _collectWarnings(events.append, f)

        self.assertEqual(events[0], 'call')
        self.assertEqual(events[1].message, firstMessage)
        self.assertEqual(events[2].message, secondMessage)
        self.assertEqual(events[3], 'returning')
        self.assertEqual(len(events), 4)
Ejemplo n.º 3
0
    def test_duplicateWarningCollected(self):
        """
        Subsequent emissions of a warning from a particular source site can be
        collected by L{_collectWarnings}.  In particular, the per-module
        emitted-warning cache should be bypassed (I{__warningregistry__}).
        """
        # Make sure the worst case is tested: if __warningregistry__ isn't in a
        # module's globals, then the warning system will add it and start using
        # it to avoid emitting duplicate warnings.  Delete __warningregistry__
        # to ensure that even modules which are first imported as a test is
        # running still interact properly with the warning system.
        global __warningregistry__
        del __warningregistry__

        def f():
            warnings.warn("foo")
        warnings.simplefilter('default')
        f()
        events = []
        _collectWarnings(events.append, f)
        self.assertEqual(len(events), 1)
        self.assertEqual(events[0].message, "foo")
        self.assertEqual(len(self.flushWarnings()), 1)
Ejemplo n.º 4
0
    def test_callsFunction(self):
        """
        L{_collectWarnings} returns the result of calling the callable passed to
        it with the parameters given.
        """
        arguments = []
        value = object()

        def f(*args, **kwargs):
            arguments.append((args, kwargs))
            return value

        result = _collectWarnings(lambda x: None, f, 1, 'a', b=2, c='d')
        self.assertEqual(arguments, [((1, 'a'), {'b': 2, 'c': 'd'})])
        self.assertIdentical(result, value)
Ejemplo n.º 5
0
    def test_callsObserver(self):
        """
        L{_collectWarnings} calls the observer with each emitted warning.
        """
        firstMessage = "dummy calls observer warning"
        secondMessage = firstMessage[::-1]
        thirdMessage = Warning(1, 2, 3)
        events = []

        def f():
            events.append("call")
            warnings.warn(firstMessage)
            warnings.warn(secondMessage)
            warnings.warn(thirdMessage)
            events.append("returning")

        _collectWarnings(events.append, f)

        self.assertEqual(events[0], "call")
        self.assertEqual(events[1].message, firstMessage)
        self.assertEqual(events[2].message, secondMessage)
        self.assertEqual(events[3].message, str(thirdMessage))
        self.assertEqual(events[4], "returning")
        self.assertEqual(len(events), 5)