~ubuntu-branches/ubuntu/oneiric/python-testtools/oneiric

« back to all changes in this revision

Viewing changes to testtools/tests/test_content.py

  • Committer: Bazaar Package Importer
  • Author(s): Robert Collins
  • Date: 2010-12-18 21:11:40 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20101218211140-dy3zzyhqpc1yvp8s
Tags: 0.9.8-1
New upstream release. Closes: #606479

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
 
1
# Copyright (c) 2008-2010 Jonathan M. Lange. See LICENSE for details.
2
2
 
3
3
import unittest
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
8
10
 
9
11
 
10
 
def test_suite():
11
 
    from unittest import TestLoader
12
 
    return TestLoader().loadTestsFromName(__name__)
13
 
 
14
 
 
15
 
class TestContent(unittest.TestCase):
 
12
raises_value_error = Raises(MatchesException(ValueError))
 
13
 
 
14
 
 
15
class TestContent(TestCase):
16
16
 
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"]),
 
20
            raises_value_error)
 
21
        self.assertThat(lambda:Content(ContentType("text", "traceback"), None),
 
22
            raises_value_error)
22
23
 
23
24
    def test___init___sets_ivars(self):
24
25
        content_type = ContentType("foo", "bar")
28
29
 
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)
40
43
 
 
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))
 
48
 
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)
45
53
 
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()))
58
66
 
59
67
 
60
 
class TestTracebackContent(unittest.TestCase):
 
68
class TestTracebackContent(TestCase):
61
69
 
62
70
    def test___init___None_errors(self):
63
 
        self.assertRaises(ValueError, TracebackContent, None, None)
 
71
        self.assertThat(lambda:TracebackContent(None, None),
 
72
            raises_value_error) 
64
73
 
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())))
 
82
 
 
83
 
 
84
class TestBytesContent(TestCase):
 
85
 
 
86
    def test_bytes(self):
 
87
        data = _u("some data")
 
88
        expected = Content(UTF8_TEXT, lambda: [data.encode('utf8')])
 
89
        self.assertEqual(expected, text_content(data))
 
90
 
 
91
 
 
92
def test_suite():
 
93
    from unittest import TestLoader
 
94
    return TestLoader().loadTestsFromName(__name__)