Make text_content more explicit about the types that it accepts.
authorThomi Richards <thomi.richards@canonical.com>
Tue, 14 Oct 2014 00:16:51 +0000 (13:16 +1300)
committerThomi Richards <thomi.richards@canonical.com>
Wed, 15 Oct 2014 21:17:23 +0000 (10:17 +1300)
NEWS
testtools/content.py
testtools/tests/test_content.py

diff --git a/NEWS b/NEWS
index c79b6cc739296785ae41babac44ee8e3bb818434..640a95232ecf7515e28ca654d965115f952e990e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -13,6 +13,9 @@ Changes
 * Fixed unit tests which were failing under pypy due to a change in the way
   pypy formats tracebacks. (Thomi Richards)
 
+* Make `testtools.content.text_content` error if anything other than text
+  is given as content. (Thomi Richards)
+
 1.1.0
 ~~~~~
 
index 401004bd1505351787095c24e009092d504b138e..04f515177f09365fa0343733920c2aad224c3f42 100644 (file)
@@ -25,9 +25,9 @@ from testtools.compat import (
     _b,
     _format_exception_only,
     _format_stack_list,
-    _isbytes,
     _TB_HEADER,
     _u,
+    istext,
     str_is_unicode,
 )
 from testtools.content_type import ContentType, JSON, UTF8_TEXT
@@ -264,8 +264,10 @@ def text_content(text):
 
     This is useful for adding details which are short strings.
     """
-    if _isbytes(text):
-        raise TypeError('text_content must be given a string, not bytes.')
+    if not istext(text):
+        raise TypeError(
+            "text_content must be given text, not '%s'." % type(text).__name__
+        )
     return Content(UTF8_TEXT, lambda: [text.encode('utf8')])
 
 
index 342ae239a5128beac03761636a33201d0be17604..09feebd8f2b1c61738764adabb94959a61bc8d6d 100644 (file)
@@ -196,6 +196,17 @@ class TestContent(TestCase):
         data = _b("Some Bytes")
         self.assertRaises(TypeError, text_content, data)
 
+    def test_text_content_raises_TypeError_when_passed_non_text(self):
+        bad_values = (None, list(), dict(), 42, 1.23)
+        for value in bad_values:
+            self.assertThat(
+                lambda: text_content(value),
+                raises(
+                    TypeError("text_content must be given text, not '%s'." %
+                        type(value).__name__)
+                ),
+            )
+
     def test_json_content(self):
         data = {'foo': 'bar'}
         expected = Content(JSON, lambda: [_b('{"foo": "bar"}')])