1
# Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
1
# Copyright (c) 2008-2010 Jonathan M. Lange. See LICENSE for details.
4
from testtools.compat import _u
5
from testtools.content import Content, TracebackContent
6
from testtools.content_type import ContentType
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
7
9
from testtools.tests.helpers import an_exc_info
11
from unittest import TestLoader
12
return TestLoader().loadTestsFromName(__name__)
15
class TestContent(unittest.TestCase):
12
raises_value_error = Raises(MatchesException(ValueError))
15
class TestContent(TestCase):
17
17
def test___init___None_errors(self):
18
self.assertRaises(ValueError, Content, None, None)
19
self.assertRaises(ValueError, Content, None, lambda: ["traceback"])
20
self.assertRaises(ValueError, Content,
21
ContentType("text", "traceback"), None)
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),
23
24
def test___init___sets_ivars(self):
24
25
content_type = ContentType("foo", "bar")
29
30
def test___eq__(self):
30
31
content_type = ContentType("foo", "bar")
31
content1 = Content(content_type, lambda: ["bytes"])
32
content2 = Content(content_type, lambda: ["bytes"])
33
content3 = Content(content_type, lambda: ["by", "tes"])
34
content4 = Content(content_type, lambda: ["by", "te"])
35
content5 = Content(ContentType("f", "b"), lambda: ["by", "tes"])
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)
36
39
self.assertEqual(content1, content2)
37
40
self.assertEqual(content1, content3)
38
41
self.assertNotEqual(content1, content4)
39
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))
41
49
def test_iter_text_not_text_errors(self):
42
50
content_type = ContentType("foo", "bar")
43
51
content = Content(content_type, lambda: ["bytes"])
44
self.assertRaises(ValueError, content.iter_text)
52
self.assertThat(content.iter_text, raises_value_error)
46
54
def test_iter_text_decodes(self):
47
55
content_type = ContentType("text", "strange", {"charset": "utf8"})
57
65
self.assertEqual([text], list(content.iter_text()))
60
class TestTracebackContent(unittest.TestCase):
68
class TestTracebackContent(TestCase):
62
70
def test___init___None_errors(self):
63
self.assertRaises(ValueError, TracebackContent, None, None)
71
self.assertThat(lambda:TracebackContent(None, None),
65
74
def test___init___sets_ivars(self):
66
75
content = TracebackContent(an_exc_info, self)
70
79
result = unittest.TestResult()
71
80
expected = result._exc_info_to_string(an_exc_info, self)
72
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__)