diff options
| author | Robert Collins <robertc@robertcollins.net> | 2009-10-05 05:53:44 +1100 |
|---|---|---|
| committer | Robert Collins <robertc@robertcollins.net> | 2009-10-05 05:53:44 +1100 |
| commit | 1bb81bd80c26f41555f676d9f6b164be9c061e61 (patch) | |
| tree | 150e8a9d01cbf78f8cd880ce1bbbe1078ed8f9c5 /python/subunit | |
| parent | 869d2a4f792a4d5d2c9f7f55d95ba216f0fbe4f1 (diff) | |
| download | subunit-git-1bb81bd80c26f41555f676d9f6b164be9c061e61.tar.gz | |
Create TracebackContent for adapting exc_info tuples.
Diffstat (limited to 'python/subunit')
| -rw-r--r-- | python/subunit/content.py | 26 | ||||
| -rw-r--r-- | python/subunit/content_type.py | 8 | ||||
| -rw-r--r-- | python/subunit/tests/test_content.py | 18 | ||||
| -rw-r--r-- | python/subunit/tests/test_content_type.py | 7 |
4 files changed, 58 insertions, 1 deletions
diff --git a/python/subunit/content.py b/python/subunit/content.py index 517862a..cd3ad60 100644 --- a/python/subunit/content.py +++ b/python/subunit/content.py @@ -16,6 +16,12 @@ """Content - a MIME-like Content object.""" +from unittest import TestResult + +import subunit +from subunit.content_type import ContentType + + class Content(object): """A MIME-like Content object. @@ -39,3 +45,23 @@ class Content(object): def iter_bytes(self): """Iterate over bytestrings of the serialised content.""" return self._get_bytes() + + +class TracebackContent(Content): + """Content object for tracebacks. + + This adapts an exc_info tuple to the Content interface. + text/x-traceback;language=python is used for the mime type, in order to + provide room for other languages to format their tracebacks differently. + """ + + def __init__(self, err): + """Create a TracebackContent for err.""" + if err is None: + raise ValueError("err may not be None") + content_type = ContentType('text', 'x-traceback', + {"language": "python"}) + self._result = TestResult() + super(TracebackContent, self).__init__(content_type, + lambda:self._result._exc_info_to_string(err, + subunit.RemotedTestCase(''))) diff --git a/python/subunit/content_type.py b/python/subunit/content_type.py index cafc437..3aade6c 100644 --- a/python/subunit/content_type.py +++ b/python/subunit/content_type.py @@ -33,3 +33,11 @@ class ContentType(object): self.type = primary_type self.subtype = sub_type self.parameters = parameters or {} + + def __eq__(self, other): + if type(other) != ContentType: + return False + return self.__dict__ == other.__dict__ + + def __repr__(self): + return "%s/%s params=%s" % (self.type, self.subtype, self.parameters) diff --git a/python/subunit/tests/test_content.py b/python/subunit/tests/test_content.py index 869717e..8075cf9 100644 --- a/python/subunit/tests/test_content.py +++ b/python/subunit/tests/test_content.py @@ -16,7 +16,7 @@ import unittest import subunit -from subunit.content import Content +from subunit.content import Content, TracebackContent from subunit.content_type import ContentType @@ -39,3 +39,19 @@ class TestContent(unittest.TestCase): content = Content(content_type, lambda:["bytes"]) self.assertEqual(content_type, content.content_type) self.assertEqual(["bytes"], list(content.iter_bytes())) + + +class TestTracebackContent(unittest.TestCase): + + def test___init___None_errors(self): + self.assertRaises(ValueError, TracebackContent, None) + + def test___init___sets_ivars(self): + content = TracebackContent(subunit.RemoteError("weird")) + content_type = ContentType("text", "x-traceback", + {"language":"python"}) + self.assertEqual(content_type, content.content_type) + result = unittest.TestResult() + expected = result._exc_info_to_string(subunit.RemoteError("weird"), + subunit.RemotedTestCase('')) + self.assertEqual(expected, ''.join(list(content.iter_bytes()))) diff --git a/python/subunit/tests/test_content_type.py b/python/subunit/tests/test_content_type.py index 7e24316..3a3fa2c 100644 --- a/python/subunit/tests/test_content_type.py +++ b/python/subunit/tests/test_content_type.py @@ -41,3 +41,10 @@ class TestContentType(unittest.TestCase): def test___init___with_parameters(self): content_type = ContentType("foo", "bar", {"quux":"thing"}) self.assertEqual({"quux":"thing"}, content_type.parameters) + + def test___eq__(self): + content_type1 = ContentType("foo", "bar", {"quux":"thing"}) + content_type2 = ContentType("foo", "bar", {"quux":"thing"}) + content_type3 = ContentType("foo", "bar", {"quux":"thing2"}) + self.assertTrue(content_type1.__eq__(content_type2)) + self.assertFalse(content_type1.__eq__(content_type3)) |
