← Back to team overview

testtools-dev team mailing list archive

[Merge] lp:~gz/testtools/bytes_to_join_bytes_675331 into lp:testtools

 

Martin [gz] has proposed merging lp:~gz/testtools/bytes_to_join_bytes_675331 into lp:testtools.

Requested reviews:
  testtools developers (testtools-dev)
Related bugs:
  #675331 testtools.tests.test_content.TestBytesContent.test_bytes fails on Python 3
  https://bugs.launchpad.net/bugs/675331


Make some ugly code uglier so it has the right semantics on Python 3. As well as the problem raised in the bug, the __repr__ method had a similar issue.

Noting the obvious:
* Content objects want (but don't really validate that they get) byte strings, which means str for Python 2 and bytes for Python 3.
* A __repr__ method must return the native str type, which is a byte string on Python 2 and a unicode string on Python 3.
-- 
https://code.launchpad.net/~gz/testtools/bytes_to_join_bytes_675331/+merge/40922
Your team testtools developers is requested to review the proposed merge of lp:~gz/testtools/bytes_to_join_bytes_675331 into lp:testtools.
=== modified file 'testtools/content.py'
--- testtools/content.py	2010-10-28 22:06:41 +0000
+++ testtools/content.py	2010-11-16 00:28:16 +0000
@@ -9,6 +9,9 @@
 from testtools.testresult import TestResult
 
 
+_join_b = _b("").join
+
+
 class Content(object):
     """A MIME-like Content object.
 
@@ -31,7 +34,7 @@
 
     def __eq__(self, other):
         return (self.content_type == other.content_type and
-            ''.join(self.iter_bytes()) == ''.join(other.iter_bytes()))
+            _join_b(self.iter_bytes()) == _join_b(other.iter_bytes()))
 
     def iter_bytes(self):
         """Iterate over bytestrings of the serialised content."""
@@ -68,7 +71,7 @@
 
     def __repr__(self):
         return "<Content type=%r, value=%r>" % (
-            self.content_type, ''.join(self.iter_bytes()))
+            self.content_type, _join_b(self.iter_bytes()))
 
 
 class TracebackContent(Content):

=== modified file 'testtools/tests/test_content.py'
--- testtools/tests/test_content.py	2010-11-11 10:59:48 +0000
+++ testtools/tests/test_content.py	2010-11-16 00:28:16 +0000
@@ -2,7 +2,7 @@
 
 import unittest
 from testtools import TestCase
-from testtools.compat import _u
+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
@@ -29,16 +29,23 @@
 
     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"])


Follow ups