1
# Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
5
from testtools.content import Content, TracebackContent
6
from testtools.content_type import ContentType
10
from unittest import TestLoader
11
return TestLoader().loadTestsFromName(__name__)
14
class TestContent(unittest.TestCase):
16
def test___init___None_errors(self):
17
self.assertRaises(ValueError, Content, None, None)
18
self.assertRaises(ValueError, Content, None, lambda:["traceback"])
19
self.assertRaises(ValueError, Content,
20
ContentType("text", "traceback"), None)
22
def test___init___sets_ivars(self):
23
content_type = ContentType("foo", "bar")
24
content = Content(content_type, lambda:["bytes"])
25
self.assertEqual(content_type, content.content_type)
26
self.assertEqual(["bytes"], list(content.iter_bytes()))
28
def test___eq__(self):
29
content_type = ContentType("foo", "bar")
30
content1 = Content(content_type, lambda:["bytes"])
31
content2 = Content(content_type, lambda:["bytes"])
32
content3 = Content(content_type, lambda:["by", "tes"])
33
content4 = Content(content_type, lambda:["by", "te"])
34
content5 = Content(ContentType("f","b"), lambda:["by", "tes"])
35
self.assertEqual(content1, content2)
36
self.assertEqual(content1, content3)
37
self.assertNotEqual(content1, content4)
38
self.assertNotEqual(content1, content5)
40
def test_iter_text_not_text_errors(self):
41
content_type = ContentType("foo", "bar")
42
content = Content(content_type, lambda:["bytes"])
43
self.assertRaises(ValueError, content.iter_text)
45
def test_iter_text_decodes(self):
46
content_type = ContentType("text", "strange", {"charset":"utf8"})
47
content = Content(content_type, lambda:[u"bytes\xea".encode("utf8")])
48
self.assertEqual([u"bytes\xea"], list(content.iter_text()))
50
def test_iter_text_default_charset_iso_8859_1(self):
51
content_type = ContentType("text", "strange")
53
iso_version = text.encode("ISO-8859-1")
54
content = Content(content_type, lambda:[iso_version])
55
self.assertEqual([text], list(content.iter_text()))
58
class TestTracebackContent(unittest.TestCase):
60
def test___init___None_errors(self):
61
self.assertRaises(ValueError, TracebackContent, None, None)
63
def test___init___sets_ivars(self):
64
exc_info = sys.exc_info()
65
content = TracebackContent(exc_info, self)
66
content_type = ContentType("text", "x-traceback",
67
{"language":"python"})
68
self.assertEqual(content_type, content.content_type)
69
result = unittest.TestResult()
70
expected = result._exc_info_to_string(exc_info, self)
71
self.assertEqual(expected, ''.join(list(content.iter_bytes())))