"""
Test cases for failure module.
"""
from twisted.trial import unittest
import StringIO
from twisted.python import failure
class FailureTestCase(unittest.TestCase):
def testFailAndTrap(self):
"""Trapping a failure."""
try:
raise NotImplementedError('test')
except:
f = failure.Failure()
error = f.trap(SystemExit, RuntimeError)
self.assertEquals(error, RuntimeError)
self.assertEquals(f.type, NotImplementedError)
def test_notTrapped(self):
"""Making sure trap doesn't trap what it shouldn't."""
try:
raise ValueError()
except:
f = failure.Failure()
self.assertRaises(failure.Failure,f.trap,OverflowError)
def testPrinting(self):
out = StringIO.StringIO()
try:
1/0
except:
f = failure.Failure()
f.printDetailedTraceback(out)
f.printBriefTraceback(out)
f.printTraceback(out)
def testExplictPass(self):
e = RuntimeError()
f = failure.Failure(e)
f.trap(RuntimeError)
self.assertEquals(f.value, e)