testtools: Import new upstream snapshot.
[nivanova/samba-autobuild/.git] / lib / testtools / testtools / tests / test_content.py
index 741256ef7a911b5e2b6bdbc155e93c4c11aaf0a7..eaf50c7f373e41cdfe7d2e7bbfa1346c8b1224fc 100644 (file)
@@ -2,19 +2,24 @@
 
 import unittest
 from testtools import TestCase
-from testtools.compat import _u
-from testtools.content import Content, TracebackContent
-from testtools.content_type import ContentType
+from testtools.compat import _b, _u
+from testtools.content import Content, TracebackContent, text_content
+from testtools.content_type import ContentType, UTF8_TEXT
+from testtools.matchers import MatchesException, Raises
 from testtools.tests.helpers import an_exc_info
 
 
+raises_value_error = Raises(MatchesException(ValueError))
+
+
 class TestContent(TestCase):
 
     def test___init___None_errors(self):
-        self.assertRaises(ValueError, Content, None, None)
-        self.assertRaises(ValueError, Content, None, lambda: ["traceback"])
-        self.assertRaises(ValueError, Content,
-            ContentType("text", "traceback"), None)
+        self.assertThat(lambda:Content(None, None), raises_value_error)
+        self.assertThat(lambda:Content(None, lambda: ["traceback"]),
+            raises_value_error)
+        self.assertThat(lambda:Content(ContentType("text", "traceback"), None),
+            raises_value_error)
 
     def test___init___sets_ivars(self):
         content_type = ContentType("foo", "bar")
@@ -24,20 +29,27 @@ class TestContent(TestCase):
 
     def test___eq__(self):
         content_type = ContentType("foo", "bar")
-        content1 = Content(content_type, lambda: ["bytes"])
-        content2 = Content(content_type, lambda: ["bytes"])
-        content3 = Content(content_type, lambda: ["by", "tes"])
-        content4 = Content(content_type, lambda: ["by", "te"])
-        content5 = Content(ContentType("f", "b"), lambda: ["by", "tes"])
+        one_chunk = lambda: [_b("bytes")]
+        two_chunk = lambda: [_b("by"), _b("tes")]
+        content1 = Content(content_type, one_chunk)
+        content2 = Content(content_type, one_chunk)
+        content3 = Content(content_type, two_chunk)
+        content4 = Content(content_type, lambda: [_b("by"), _b("te")])
+        content5 = Content(ContentType("f", "b"), two_chunk)
         self.assertEqual(content1, content2)
         self.assertEqual(content1, content3)
         self.assertNotEqual(content1, content4)
         self.assertNotEqual(content1, content5)
 
+    def test___repr__(self):
+        content = Content(ContentType("application", "octet-stream"),
+            lambda: [_b("\x00bin"), _b("ary\xff")])
+        self.assertIn("\\x00binary\\xff", repr(content))
+
     def test_iter_text_not_text_errors(self):
         content_type = ContentType("foo", "bar")
         content = Content(content_type, lambda: ["bytes"])
-        self.assertRaises(ValueError, content.iter_text)
+        self.assertThat(content.iter_text, raises_value_error)
 
     def test_iter_text_decodes(self):
         content_type = ContentType("text", "strange", {"charset": "utf8"})
@@ -56,7 +68,8 @@ class TestContent(TestCase):
 class TestTracebackContent(TestCase):
 
     def test___init___None_errors(self):
-        self.assertRaises(ValueError, TracebackContent, None, None)
+        self.assertThat(lambda:TracebackContent(None, None),
+            raises_value_error) 
 
     def test___init___sets_ivars(self):
         content = TracebackContent(an_exc_info, self)
@@ -68,6 +81,14 @@ class TestTracebackContent(TestCase):
         self.assertEqual(expected, ''.join(list(content.iter_text())))
 
 
+class TestBytesContent(TestCase):
+
+    def test_bytes(self):
+        data = _u("some data")
+        expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
+        self.assertEqual(expected, text_content(data))
+
+
 def test_suite():
     from unittest import TestLoader
     return TestLoader().loadTestsFromName(__name__)