Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / trial / test / test_assertions.py
1 # Copyright (c) 2001-2011 Twisted Matrix Laboratories.
2 # See LICENSE for details
3
4 """
5 Tests for assertions provided by L{twisted.trial.unittest.TestCase}.
6 """
7
8 import warnings
9 from pprint import pformat
10
11 from twisted.python import reflect, failure
12 from twisted.python.deprecate import deprecated, getVersionString
13 from twisted.python.versions import Version
14 from twisted.internet import defer
15 from twisted.trial import unittest, runner, reporter
16
17 class MockEquality(object):
18     def __init__(self, name):
19         self.name = name
20
21     def __repr__(self):
22         return "MockEquality(%s)" % (self.name,)
23
24     def __eq__(self, other):
25         if not hasattr(other, 'name'):
26             raise ValueError("%r not comparable to %r" % (other, self))
27         return self.name[0] == other.name[0]
28
29
30 class TestAssertions(unittest.TestCase):
31     """
32     Tests for TestCase's assertion methods.  That is, failUnless*,
33     failIf*, assert*.
34
35     Note: As of 11.2, assertEqual is preferred over the failUnlessEqual(s)
36     variants.  Tests have been modified to reflect this preference.
37
38     This is pretty paranoid.  Still, a certain paranoia is healthy if you
39     are testing a unit testing framework.
40     """
41
42     class FailingTest(unittest.TestCase):
43         def test_fails(self):
44             raise self.failureException()
45
46     def testFail(self):
47         try:
48             self.fail("failed")
49         except self.failureException, e:
50             if not str(e) == 'failed':
51                 raise self.failureException("Exception had msg %s instead of %s"
52                                             % str(e), 'failed')
53         else:
54             raise self.failureException("Call to self.fail() didn't fail test")
55
56     def test_failingException_fails(self):
57         test = runner.TestLoader().loadClass(TestAssertions.FailingTest)
58         result = reporter.TestResult()
59         test.run(result)
60         self.failIf(result.wasSuccessful())
61         self.assertEqual(result.errors, [])
62         self.assertEqual(len(result.failures), 1)
63
64     def test_failIf(self):
65         for notTrue in [0, 0.0, False, None, (), []]:
66             self.failIf(notTrue, "failed on %r" % (notTrue,))
67         for true in [1, True, 'cat', [1,2], (3,4)]:
68             try:
69                 self.failIf(true, "failed on %r" % (true,))
70             except self.failureException, e:
71                 self.assertEqual(str(e), "failed on %r" % (true,))
72             else:
73                 self.fail("Call to failIf(%r) didn't fail" % (true,))
74
75     def test_failUnless(self):
76         for notTrue in [0, 0.0, False, None, (), []]:
77             try:
78                 self.failUnless(notTrue, "failed on %r" % (notTrue,))
79             except self.failureException, e:
80                 self.assertEqual(str(e), "failed on %r" % (notTrue,))
81             else:
82                 self.fail("Call to failUnless(%r) didn't fail" % (notTrue,))
83         for true in [1, True, 'cat', [1,2], (3,4)]:
84             self.failUnless(true, "failed on %r" % (true,))
85
86
87     def _testEqualPair(self, first, second):
88         x = self.assertEqual(first, second)
89         if x != first:
90             self.fail("assertEqual should return first parameter")
91
92
93     def _testUnequalPair(self, first, second):
94         try:
95             self.assertEqual(first, second)
96         except self.failureException, e:
97             expected = 'not equal:\na = %s\nb = %s\n' % (
98                 pformat(first), pformat(second))
99             if str(e) != expected:
100                 self.fail("Expected: %r; Got: %s" % (expected, str(e)))
101         else:
102             self.fail("Call to assertEqual(%r, %r) didn't fail"
103                       % (first, second))
104
105
106     def test_assertEqual_basic(self):
107         self._testEqualPair('cat', 'cat')
108         self._testUnequalPair('cat', 'dog')
109         self._testEqualPair([1], [1])
110         self._testUnequalPair([1], 'orange')
111
112
113     def test_assertEqual_custom(self):
114         x = MockEquality('first')
115         y = MockEquality('second')
116         z = MockEquality('fecund')
117         self._testEqualPair(x, x)
118         self._testEqualPair(x, z)
119         self._testUnequalPair(x, y)
120         self._testUnequalPair(y, z)
121
122
123     def test_assertEqualMessage(self):
124         """
125         When a message is passed to L{assertEqual}, it is included in the
126         error message.
127         """
128         exception = self.assertRaises(
129             self.failureException, self.assertEqual,
130             'foo', 'bar', 'message')
131         self.assertEqual(
132             str(exception),
133             "message\nnot equal:\na = 'foo'\nb = 'bar'\n")
134
135
136     def test_assertEqualNoneMessage(self):
137         """
138         If a message is specified as C{None}, it is not included in the error
139         message of L{assertEqual}.
140         """
141         exception = self.assertRaises(
142             self.failureException, self.assertEqual, 'foo', 'bar', None)
143         self.assertEqual(str(exception), "not equal:\na = 'foo'\nb = 'bar'\n")
144
145
146     def test_assertEqual_incomparable(self):
147         apple = MockEquality('apple')
148         orange = ['orange']
149         try:
150             self.assertEqual(apple, orange)
151         except self.failureException:
152             self.fail("Fail raised when ValueError ought to have been raised.")
153         except ValueError:
154             # good. error not swallowed
155             pass
156         else:
157             self.fail("Comparing %r and %r should have raised an exception"
158                       % (apple, orange))
159
160
161     def _raiseError(self, error):
162         raise error
163
164     def test_failUnlessRaises_expected(self):
165         x = self.failUnlessRaises(ValueError, self._raiseError, ValueError)
166         self.failUnless(isinstance(x, ValueError),
167                         "Expect failUnlessRaises to return instance of raised "
168                         "exception.")
169
170     def test_failUnlessRaises_unexpected(self):
171         try:
172             self.failUnlessRaises(ValueError, self._raiseError, TypeError)
173         except TypeError:
174             self.fail("failUnlessRaises shouldn't re-raise unexpected "
175                       "exceptions")
176         except self.failureException:
177             # what we expect
178             pass
179         else:
180             self.fail("Expected exception wasn't raised. Should have failed")
181
182     def test_failUnlessRaises_noException(self):
183         try:
184             self.failUnlessRaises(ValueError, lambda : None)
185         except self.failureException, e:
186             self.assertEqual(str(e),
187                                  'ValueError not raised (None returned)')
188         else:
189             self.fail("Exception not raised. Should have failed")
190
191     def test_failUnlessRaises_failureException(self):
192         x = self.failUnlessRaises(self.failureException, self._raiseError,
193                                   self.failureException)
194         self.failUnless(isinstance(x, self.failureException),
195                         "Expected %r instance to be returned"
196                         % (self.failureException,))
197         try:
198             x = self.failUnlessRaises(self.failureException, self._raiseError,
199                                       ValueError)
200         except self.failureException:
201             # what we expect
202             pass
203         else:
204             self.fail("Should have raised exception")
205
206     def test_failIfEqual_basic(self):
207         x, y, z = [1], [2], [1]
208         ret = self.failIfEqual(x, y)
209         self.assertEqual(ret, x,
210                              "failIfEqual should return first parameter")
211         self.failUnlessRaises(self.failureException,
212                               self.failIfEqual, x, x)
213         self.failUnlessRaises(self.failureException,
214                               self.failIfEqual, x, z)
215
216     def test_failIfEqual_customEq(self):
217         x = MockEquality('first')
218         y = MockEquality('second')
219         z = MockEquality('fecund')
220         ret = self.failIfEqual(x, y)
221         self.assertEqual(ret, x,
222                              "failIfEqual should return first parameter")
223         self.failUnlessRaises(self.failureException,
224                               self.failIfEqual, x, x)
225         # test when __ne__ is not defined
226         self.failIfEqual(x, z, "__ne__ not defined, so not equal")
227
228     def test_failUnlessIdentical(self):
229         x, y, z = [1], [1], [2]
230         ret = self.failUnlessIdentical(x, x)
231         self.assertEqual(ret, x,
232                              'failUnlessIdentical should return first '
233                              'parameter')
234         self.failUnlessRaises(self.failureException,
235                               self.failUnlessIdentical, x, y)
236         self.failUnlessRaises(self.failureException,
237                               self.failUnlessIdentical, x, z)
238
239     def test_failUnlessApproximates(self):
240         x, y, z = 1.0, 1.1, 1.2
241         self.failUnlessApproximates(x, x, 0.2)
242         ret = self.failUnlessApproximates(x, y, 0.2)
243         self.assertEqual(ret, x, "failUnlessApproximates should return "
244                              "first parameter")
245         self.failUnlessRaises(self.failureException,
246                               self.failUnlessApproximates, x, z, 0.1)
247         self.failUnlessRaises(self.failureException,
248                               self.failUnlessApproximates, x, y, 0.1)
249
250     def test_failUnlessAlmostEqual(self):
251         precision = 5
252         x = 8.000001
253         y = 8.00001
254         z = 8.000002
255         self.failUnlessAlmostEqual(x, x, precision)
256         ret = self.failUnlessAlmostEqual(x, z, precision)
257         self.assertEqual(ret, x, "failUnlessAlmostEqual should return "
258                              "first parameter (%r, %r)" % (ret, x))
259         self.failUnlessRaises(self.failureException,
260                               self.failUnlessAlmostEqual, x, y, precision)
261
262     def test_failIfAlmostEqual(self):
263         precision = 5
264         x = 8.000001
265         y = 8.00001
266         z = 8.000002
267         ret = self.failIfAlmostEqual(x, y, precision)
268         self.assertEqual(ret, x, "failIfAlmostEqual should return "
269                              "first parameter (%r, %r)" % (ret, x))
270         self.failUnlessRaises(self.failureException,
271                               self.failIfAlmostEqual, x, x, precision)
272         self.failUnlessRaises(self.failureException,
273                               self.failIfAlmostEqual, x, z, precision)
274
275     def test_failUnlessSubstring(self):
276         x = "cat"
277         y = "the dog sat"
278         z = "the cat sat"
279         self.failUnlessSubstring(x, x)
280         ret = self.failUnlessSubstring(x, z)
281         self.assertEqual(ret, x, 'should return first parameter')
282         self.failUnlessRaises(self.failureException,
283                               self.failUnlessSubstring, x, y)
284         self.failUnlessRaises(self.failureException,
285                               self.failUnlessSubstring, z, x)
286
287     def test_failIfSubstring(self):
288         x = "cat"
289         y = "the dog sat"
290         z = "the cat sat"
291         self.failIfSubstring(z, x)
292         ret = self.failIfSubstring(x, y)
293         self.assertEqual(ret, x, 'should return first parameter')
294         self.failUnlessRaises(self.failureException,
295                               self.failIfSubstring, x, x)
296         self.failUnlessRaises(self.failureException,
297                               self.failIfSubstring, x, z)
298
299     def test_assertFailure(self):
300         d = defer.maybeDeferred(lambda: 1/0)
301         return self.assertFailure(d, ZeroDivisionError)
302
303     def test_assertFailure_wrongException(self):
304         d = defer.maybeDeferred(lambda: 1/0)
305         self.assertFailure(d, OverflowError)
306         d.addCallbacks(lambda x: self.fail('Should have failed'),
307                        lambda x: x.trap(self.failureException))
308         return d
309
310     def test_assertFailure_noException(self):
311         d = defer.succeed(None)
312         self.assertFailure(d, ZeroDivisionError)
313         d.addCallbacks(lambda x: self.fail('Should have failed'),
314                        lambda x: x.trap(self.failureException))
315         return d
316
317     def test_assertFailure_moreInfo(self):
318         """
319         In the case of assertFailure failing, check that we get lots of
320         information about the exception that was raised.
321         """
322         try:
323             1/0
324         except ZeroDivisionError:
325             f = failure.Failure()
326             d = defer.fail(f)
327         d = self.assertFailure(d, RuntimeError)
328         d.addErrback(self._checkInfo, f)
329         return d
330
331     def _checkInfo(self, assertionFailure, f):
332         assert assertionFailure.check(self.failureException)
333         output = assertionFailure.getErrorMessage()
334         self.assertIn(f.getErrorMessage(), output)
335         self.assertIn(f.getBriefTraceback(), output)
336
337     def test_assertFailure_masked(self):
338         """
339         A single wrong assertFailure should fail the whole test.
340         """
341         class ExampleFailure(Exception):
342             pass
343
344         class TC(unittest.TestCase):
345             failureException = ExampleFailure
346             def test_assertFailure(self):
347                 d = defer.maybeDeferred(lambda: 1/0)
348                 self.assertFailure(d, OverflowError)
349                 self.assertFailure(d, ZeroDivisionError)
350                 return d
351
352         test = TC('test_assertFailure')
353         result = reporter.TestResult()
354         test.run(result)
355         self.assertEqual(1, len(result.failures))
356
357
358     def test_assertWarns(self):
359         """
360         Test basic assertWarns report.
361         """
362         def deprecated(a):
363             warnings.warn("Woo deprecated", category=DeprecationWarning)
364             return a
365         r = self.assertWarns(DeprecationWarning, "Woo deprecated", __file__,
366             deprecated, 123)
367         self.assertEqual(r, 123)
368
369
370     def test_assertWarnsRegistryClean(self):
371         """
372         Test that assertWarns cleans the warning registry, so the warning is
373         not swallowed the second time.
374         """
375         def deprecated(a):
376             warnings.warn("Woo deprecated", category=DeprecationWarning)
377             return a
378         r1 = self.assertWarns(DeprecationWarning, "Woo deprecated", __file__,
379             deprecated, 123)
380         self.assertEqual(r1, 123)
381         # The warning should be raised again
382         r2 = self.assertWarns(DeprecationWarning, "Woo deprecated", __file__,
383             deprecated, 321)
384         self.assertEqual(r2, 321)
385
386
387     def test_assertWarnsError(self):
388         """
389         Test assertWarns failure when no warning is generated.
390         """
391         def normal(a):
392             return a
393         self.assertRaises(self.failureException,
394             self.assertWarns, DeprecationWarning, "Woo deprecated", __file__,
395             normal, 123)
396
397
398     def test_assertWarnsWrongCategory(self):
399         """
400         Test assertWarns failure when the category is wrong.
401         """
402         def deprecated(a):
403             warnings.warn("Foo deprecated", category=DeprecationWarning)
404             return a
405         self.assertRaises(self.failureException,
406             self.assertWarns, UserWarning, "Foo deprecated", __file__,
407             deprecated, 123)
408
409
410     def test_assertWarnsWrongMessage(self):
411         """
412         Test assertWarns failure when the message is wrong.
413         """
414         def deprecated(a):
415             warnings.warn("Foo deprecated", category=DeprecationWarning)
416             return a
417         self.assertRaises(self.failureException,
418             self.assertWarns, DeprecationWarning, "Bar deprecated", __file__,
419             deprecated, 123)
420
421
422     def test_assertWarnsWrongFile(self):
423         """
424         If the warning emitted by a function refers to a different file than is
425         passed to C{assertWarns}, C{failureException} is raised.
426         """
427         def deprecated(a):
428             # stacklevel=2 points at the direct caller of the function.  The
429             # way assertRaises is invoked below, the direct caller will be
430             # something somewhere in trial, not something in this file.  In
431             # Python 2.5 and earlier, stacklevel of 0 resulted in a warning
432             # pointing to the warnings module itself.  Starting in Python 2.6,
433             # stacklevel of 0 and 1 both result in a warning pointing to *this*
434             # file, presumably due to the fact that the warn function is
435             # implemented in C and has no convenient Python
436             # filename/linenumber.
437             warnings.warn(
438                 "Foo deprecated", category=DeprecationWarning, stacklevel=2)
439         self.assertRaises(
440             self.failureException,
441             # Since the direct caller isn't in this file, try to assert that
442             # the warning *does* point to this file, so that assertWarns raises
443             # an exception.
444             self.assertWarns, DeprecationWarning, "Foo deprecated", __file__,
445             deprecated, 123)
446
447     def test_assertWarnsOnClass(self):
448         """
449         Test assertWarns works when creating a class instance.
450         """
451         class Warn:
452             def __init__(self):
453                 warnings.warn("Do not call me", category=RuntimeWarning)
454         r = self.assertWarns(RuntimeWarning, "Do not call me", __file__,
455             Warn)
456         self.assertTrue(isinstance(r, Warn))
457         r = self.assertWarns(RuntimeWarning, "Do not call me", __file__,
458             Warn)
459         self.assertTrue(isinstance(r, Warn))
460
461
462     def test_assertWarnsOnMethod(self):
463         """
464         Test assertWarns works when used on an instance method.
465         """
466         class Warn:
467             def deprecated(self, a):
468                 warnings.warn("Bar deprecated", category=DeprecationWarning)
469                 return a
470         w = Warn()
471         r = self.assertWarns(DeprecationWarning, "Bar deprecated", __file__,
472             w.deprecated, 321)
473         self.assertEqual(r, 321)
474         r = self.assertWarns(DeprecationWarning, "Bar deprecated", __file__,
475             w.deprecated, 321)
476         self.assertEqual(r, 321)
477
478
479     def test_assertWarnsOnCall(self):
480         """
481         Test assertWarns works on instance with C{__call__} method.
482         """
483         class Warn:
484             def __call__(self, a):
485                 warnings.warn("Egg deprecated", category=DeprecationWarning)
486                 return a
487         w = Warn()
488         r = self.assertWarns(DeprecationWarning, "Egg deprecated", __file__,
489             w, 321)
490         self.assertEqual(r, 321)
491         r = self.assertWarns(DeprecationWarning, "Egg deprecated", __file__,
492             w, 321)
493         self.assertEqual(r, 321)
494
495
496     def test_assertWarnsFilter(self):
497         """
498         Test assertWarns on a warning filterd by default.
499         """
500         def deprecated(a):
501             warnings.warn("Woo deprecated", category=PendingDeprecationWarning)
502             return a
503         r = self.assertWarns(PendingDeprecationWarning, "Woo deprecated",
504             __file__, deprecated, 123)
505         self.assertEqual(r, 123)
506
507
508     def test_assertWarnsMultipleWarnings(self):
509         """
510         C{assertWarns} does not raise an exception if the function it is passed
511         triggers the same warning more than once.
512         """
513         def deprecated():
514             warnings.warn("Woo deprecated", category=PendingDeprecationWarning)
515         def f():
516             deprecated()
517             deprecated()
518         self.assertWarns(
519             PendingDeprecationWarning, "Woo deprecated", __file__, f)
520
521
522     def test_assertWarnsDifferentWarnings(self):
523         """
524         For now, assertWarns is unable to handle multiple different warnings,
525         so it should raise an exception if it's the case.
526         """
527         def deprecated(a):
528             warnings.warn("Woo deprecated", category=DeprecationWarning)
529             warnings.warn("Another one", category=PendingDeprecationWarning)
530         e = self.assertRaises(self.failureException,
531                 self.assertWarns, DeprecationWarning, "Woo deprecated",
532                 __file__, deprecated, 123)
533         self.assertEqual(str(e), "Can't handle different warnings")
534
535
536     def test_assertWarnsAfterUnassertedWarning(self):
537         """
538         Warnings emitted before L{TestCase.assertWarns} is called do not get
539         flushed and do not alter the behavior of L{TestCase.assertWarns}.
540         """
541         class TheWarning(Warning):
542             pass
543
544         def f(message):
545             warnings.warn(message, category=TheWarning)
546         f("foo")
547         self.assertWarns(TheWarning, "bar", __file__, f, "bar")
548         [warning] = self.flushWarnings([f])
549         self.assertEqual(warning['message'], "foo")
550
551
552     def test_assertIsInstance(self):
553         """
554         Test a true condition of assertIsInstance.
555         """
556         A = type('A', (object,), {})
557         a = A()
558         self.assertIsInstance(a, A)
559
560     def test_assertIsInstanceMultipleClasses(self):
561         """
562         Test a true condition of assertIsInstance with multiple classes.
563         """
564         A = type('A', (object,), {})
565         B = type('B', (object,), {})
566         a = A()
567         self.assertIsInstance(a, (A, B))
568
569     def test_assertIsInstanceError(self):
570         """
571         Test an error with assertIsInstance.
572         """
573         A = type('A', (object,), {})
574         B = type('B', (object,), {})
575         a = A()
576         self.assertRaises(self.failureException, self.assertIsInstance, a, B)
577
578     def test_assertIsInstanceErrorMultipleClasses(self):
579         """
580         Test an error with assertIsInstance and multiple classes.
581         """
582         A = type('A', (object,), {})
583         B = type('B', (object,), {})
584         C = type('C', (object,), {})
585         a = A()
586         self.assertRaises(self.failureException, self.assertIsInstance, a, (B, C))
587
588
589     def test_assertIsInstanceCustomMessage(self):
590         """
591         If L{TestCase.assertIsInstance} is passed a custom message as its 3rd
592         argument, the message is included in the failure exception raised when
593         the assertion fails.
594         """
595         exc = self.assertRaises(
596             self.failureException,
597             self.assertIsInstance, 3, str, "Silly assertion")
598         self.assertIn("Silly assertion", str(exc))
599
600
601     def test_assertNotIsInstance(self):
602         """
603         Test a true condition of assertNotIsInstance.
604         """
605         A = type('A', (object,), {})
606         B = type('B', (object,), {})
607         a = A()
608         self.assertNotIsInstance(a, B)
609
610     def test_assertNotIsInstanceMultipleClasses(self):
611         """
612         Test a true condition of assertNotIsInstance and multiple classes.
613         """
614         A = type('A', (object,), {})
615         B = type('B', (object,), {})
616         C = type('C', (object,), {})
617         a = A()
618         self.assertNotIsInstance(a, (B, C))
619
620     def test_assertNotIsInstanceError(self):
621         """
622         Test an error with assertNotIsInstance.
623         """
624         A = type('A', (object,), {})
625         a = A()
626         error = self.assertRaises(self.failureException,
627                                   self.assertNotIsInstance, a, A)
628         self.assertEqual(str(error), "%r is an instance of %s" % (a, A))
629
630
631     def test_assertNotIsInstanceErrorMultipleClasses(self):
632         """
633         Test an error with assertNotIsInstance and multiple classes.
634         """
635         A = type('A', (object,), {})
636         B = type('B', (object,), {})
637         a = A()
638         self.assertRaises(self.failureException, self.assertNotIsInstance, a, (A, B))
639
640
641     def test_assertDictEqual(self):
642         """
643         L{twisted.trial.unittest.TestCase} supports the C{assertDictEqual}
644         method inherited from the standard library in Python 2.7.
645         """
646         self.assertDictEqual({'a': 1}, {'a': 1})
647     if getattr(unittest.TestCase, 'assertDictEqual', None) is None:
648         test_assertDictEqual.skip = (
649             "assertDictEqual is not available on this version of Python")
650
651
652
653 class TestAssertionNames(unittest.TestCase):
654     """
655     Tests for consistency of naming within TestCase assertion methods
656     """
657     def _getAsserts(self):
658         dct = {}
659         reflect.accumulateMethods(self, dct, 'assert')
660         return [ dct[k] for k in dct if not k.startswith('Not') and k != '_' ]
661
662     def _name(self, x):
663         return x.__name__
664
665
666     def test_failUnlessMatchesAssert(self):
667         """
668         The C{failUnless*} test methods are a subset of the C{assert*} test
669         methods.  This is intended to ensure that methods using the
670         I{failUnless} naming scheme are not added without corresponding methods
671         using the I{assert} naming scheme.  The I{assert} naming scheme is
672         preferred, and new I{assert}-prefixed methods may be added without
673         corresponding I{failUnless}-prefixed methods.
674         """
675         asserts = set(self._getAsserts())
676         failUnlesses = set(reflect.prefixedMethods(self, 'failUnless'))
677         self.assertEqual(
678             failUnlesses, asserts.intersection(failUnlesses))
679
680
681     def test_failIf_matches_assertNot(self):
682         asserts = reflect.prefixedMethods(unittest.TestCase, 'assertNot')
683         failIfs = reflect.prefixedMethods(unittest.TestCase, 'failIf')
684         self.assertEqual(sorted(asserts, key=self._name),
685                              sorted(failIfs, key=self._name))
686
687     def test_equalSpelling(self):
688         for name, value in vars(self).items():
689             if not callable(value):
690                 continue
691             if name.endswith('Equal'):
692                 self.failUnless(hasattr(self, name+'s'),
693                                 "%s but no %ss" % (name, name))
694                 self.assertEqual(value, getattr(self, name+'s'))
695             if name.endswith('Equals'):
696                 self.failUnless(hasattr(self, name[:-1]),
697                                 "%s but no %s" % (name, name[:-1]))
698                 self.assertEqual(value, getattr(self, name[:-1]))
699
700
701 class TestCallDeprecated(unittest.TestCase):
702     """
703     Test use of the L{TestCase.callDeprecated} method with version objects.
704     """
705
706     version = Version('Twisted', 8, 0, 0)
707
708     def test_callDeprecatedSuppressesWarning(self):
709         """
710         callDeprecated calls a deprecated callable, suppressing the
711         deprecation warning.
712         """
713         self.callDeprecated(self.version, oldMethod, 'foo')
714         self.assertEqual(
715             self.flushWarnings(), [], "No warnings should be shown")
716
717
718     def test_callDeprecatedCallsFunction(self):
719         """
720         L{callDeprecated} actually calls the callable passed to it, and
721         forwards the result.
722         """
723         result = self.callDeprecated(self.version, oldMethod, 'foo')
724         self.assertEqual('foo', result)
725
726
727     def test_failsWithoutDeprecation(self):
728         """
729         L{callDeprecated} raises a test failure if the callable is not
730         deprecated.
731         """
732         def notDeprecated():
733             pass
734         exception = self.assertRaises(
735             self.failureException,
736             self.callDeprecated, self.version, notDeprecated)
737         self.assertEqual(
738             "%r is not deprecated." % notDeprecated, str(exception))
739
740
741     def test_failsWithIncorrectDeprecation(self):
742         """
743         callDeprecated raises a test failure if the callable was deprecated
744         at a different version to the one expected.
745         """
746         differentVersion = Version('Foo', 1, 2, 3)
747         exception = self.assertRaises(
748             self.failureException,
749             self.callDeprecated,
750             differentVersion, oldMethod, 'foo')
751         self.assertIn(getVersionString(self.version), str(exception))
752         self.assertIn(getVersionString(differentVersion), str(exception))
753
754
755     def test_nestedDeprecation(self):
756         """
757         L{callDeprecated} ignores all deprecations apart from the first.
758
759         Multiple warnings are generated when a deprecated function calls
760         another deprecated function. The first warning is the one generated by
761         the explicitly called function. That's the warning that we care about.
762         """
763         differentVersion = Version('Foo', 1, 2, 3)
764
765         def nestedDeprecation(*args):
766             return oldMethod(*args)
767         nestedDeprecation = deprecated(differentVersion)(nestedDeprecation)
768
769         self.callDeprecated(differentVersion, nestedDeprecation, 24)
770
771         # The oldMethod deprecation should have been emitted too, not captured
772         # by callDeprecated.  Flush it now to make sure it did happen and to
773         # prevent it from showing up on stdout.
774         warningsShown = self.flushWarnings()
775         self.assertEqual(len(warningsShown), 1)
776
777
778     def test_callDeprecationWithMessage(self):
779         """
780         L{callDeprecated} can take a message argument used to check the warning
781         emitted.
782         """
783         self.callDeprecated((self.version, "newMethod"),
784                             oldMethodReplaced, 1)
785
786
787     def test_callDeprecationWithWrongMessage(self):
788         """
789         If the message passed to L{callDeprecated} doesn't match,
790         L{callDeprecated} raises a test failure.
791         """
792         exception = self.assertRaises(
793             self.failureException,
794             self.callDeprecated,
795             (self.version, "something.wrong"),
796             oldMethodReplaced, 1)
797         self.assertIn(getVersionString(self.version), str(exception))
798         self.assertIn("please use newMethod instead", str(exception))
799
800
801
802
803 @deprecated(TestCallDeprecated.version)
804 def oldMethod(x):
805     """
806     Deprecated method for testing.
807     """
808     return x
809
810
811 @deprecated(TestCallDeprecated.version, replacement="newMethod")
812 def oldMethodReplaced(x):
813     """
814     Another deprecated method, which has been deprecated in favor of the
815     mythical 'newMethod'.
816     """
817     return 2 * x