1 # Copyright (c) 2008-2010 Jonathan M. Lange. See LICENSE for details.
4 from testtools import TestCase
5 from testtools.compat import _b, _u
6 from testtools.content import Content, TracebackContent, text_content
7 from testtools.content_type import ContentType, UTF8_TEXT
8 from testtools.matchers import MatchesException, Raises
9 from testtools.tests.helpers import an_exc_info
12 raises_value_error = Raises(MatchesException(ValueError))
15 class TestContent(TestCase):
17 def test___init___None_errors(self):
18 self.assertThat(lambda:Content(None, None), raises_value_error)
19 self.assertThat(lambda:Content(None, lambda: ["traceback"]),
21 self.assertThat(lambda:Content(ContentType("text", "traceback"), None),
24 def test___init___sets_ivars(self):
25 content_type = ContentType("foo", "bar")
26 content = Content(content_type, lambda: ["bytes"])
27 self.assertEqual(content_type, content.content_type)
28 self.assertEqual(["bytes"], list(content.iter_bytes()))
30 def test___eq__(self):
31 content_type = ContentType("foo", "bar")
32 one_chunk = lambda: [_b("bytes")]
33 two_chunk = lambda: [_b("by"), _b("tes")]
34 content1 = Content(content_type, one_chunk)
35 content2 = Content(content_type, one_chunk)
36 content3 = Content(content_type, two_chunk)
37 content4 = Content(content_type, lambda: [_b("by"), _b("te")])
38 content5 = Content(ContentType("f", "b"), two_chunk)
39 self.assertEqual(content1, content2)
40 self.assertEqual(content1, content3)
41 self.assertNotEqual(content1, content4)
42 self.assertNotEqual(content1, content5)
44 def test___repr__(self):
45 content = Content(ContentType("application", "octet-stream"),
46 lambda: [_b("\x00bin"), _b("ary\xff")])
47 self.assertIn("\\x00binary\\xff", repr(content))
49 def test_iter_text_not_text_errors(self):
50 content_type = ContentType("foo", "bar")
51 content = Content(content_type, lambda: ["bytes"])
52 self.assertThat(content.iter_text, raises_value_error)
54 def test_iter_text_decodes(self):
55 content_type = ContentType("text", "strange", {"charset": "utf8"})
57 content_type, lambda: [_u("bytes\xea").encode("utf8")])
58 self.assertEqual([_u("bytes\xea")], list(content.iter_text()))
60 def test_iter_text_default_charset_iso_8859_1(self):
61 content_type = ContentType("text", "strange")
62 text = _u("bytes\xea")
63 iso_version = text.encode("ISO-8859-1")
64 content = Content(content_type, lambda: [iso_version])
65 self.assertEqual([text], list(content.iter_text()))
68 class TestTracebackContent(TestCase):
70 def test___init___None_errors(self):
71 self.assertThat(lambda:TracebackContent(None, None),
74 def test___init___sets_ivars(self):
75 content = TracebackContent(an_exc_info, self)
76 content_type = ContentType("text", "x-traceback",
77 {"language": "python", "charset": "utf8"})
78 self.assertEqual(content_type, content.content_type)
79 result = unittest.TestResult()
80 expected = result._exc_info_to_string(an_exc_info, self)
81 self.assertEqual(expected, ''.join(list(content.iter_text())))
84 class TestBytesContent(TestCase):
87 data = _u("some data")
88 expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
89 self.assertEqual(expected, text_content(data))
93 from unittest import TestLoader
94 return TestLoader().loadTestsFromName(__name__)