Merge branch 'master' of ssh://git.samba.org/data/git/samba
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / test_content.py
1 # Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
2
3 import unittest
4 from testtools.content import Content, TracebackContent
5 from testtools.content_type import ContentType
6 from testtools.utils import _u
7 from testtools.tests.helpers import an_exc_info
8
9
10 def test_suite():
11     from unittest import TestLoader
12     return TestLoader().loadTestsFromName(__name__)
13
14
15 class TestContent(unittest.TestCase):
16
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)
22
23     def test___init___sets_ivars(self):
24         content_type = ContentType("foo", "bar")
25         content = Content(content_type, lambda: ["bytes"])
26         self.assertEqual(content_type, content.content_type)
27         self.assertEqual(["bytes"], list(content.iter_bytes()))
28
29     def test___eq__(self):
30         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"])
36         self.assertEqual(content1, content2)
37         self.assertEqual(content1, content3)
38         self.assertNotEqual(content1, content4)
39         self.assertNotEqual(content1, content5)
40
41     def test_iter_text_not_text_errors(self):
42         content_type = ContentType("foo", "bar")
43         content = Content(content_type, lambda: ["bytes"])
44         self.assertRaises(ValueError, content.iter_text)
45
46     def test_iter_text_decodes(self):
47         content_type = ContentType("text", "strange", {"charset": "utf8"})
48         content = Content(
49             content_type, lambda: [_u("bytes\xea").encode("utf8")])
50         self.assertEqual([_u("bytes\xea")], list(content.iter_text()))
51
52     def test_iter_text_default_charset_iso_8859_1(self):
53         content_type = ContentType("text", "strange")
54         text = _u("bytes\xea")
55         iso_version = text.encode("ISO-8859-1")
56         content = Content(content_type, lambda: [iso_version])
57         self.assertEqual([text], list(content.iter_text()))
58
59
60 class TestTracebackContent(unittest.TestCase):
61
62     def test___init___None_errors(self):
63         self.assertRaises(ValueError, TracebackContent, None, None)
64
65     def test___init___sets_ivars(self):
66         content = TracebackContent(an_exc_info, self)
67         content_type = ContentType("text", "x-traceback",
68             {"language": "python", "charset": "utf8"})
69         self.assertEqual(content_type, content.content_type)
70         result = unittest.TestResult()
71         expected = result._exc_info_to_string(an_exc_info, self)
72         self.assertEqual(expected, ''.join(list(content.iter_text())))