~bzr/ubuntu/natty/python-testtools/bzr-ppa

« back to all changes in this revision

Viewing changes to testtools/tests/test_content.py

  • Committer: Robert Collins
  • Date: 2009-11-22 03:48:30 UTC
  • mfrom: (16.11.1 upstream)
  • Revision ID: robertc@robertcollins.net-20091122034830-itaynkycem2mchy9
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2008 Jonathan M. Lange. See LICENSE for details.
 
2
 
 
3
import sys
 
4
import unittest
 
5
from testtools.content import Content, TracebackContent
 
6
from testtools.content_type import ContentType
 
7
 
 
8
 
 
9
def test_suite():
 
10
    from unittest import TestLoader
 
11
    return TestLoader().loadTestsFromName(__name__)
 
12
 
 
13
 
 
14
class TestContent(unittest.TestCase):
 
15
 
 
16
    def test___init___None_errors(self):
 
17
        self.assertRaises(ValueError, Content, None, None)
 
18
        self.assertRaises(ValueError, Content, None, lambda:["traceback"])
 
19
        self.assertRaises(ValueError, Content,
 
20
            ContentType("text", "traceback"), None)
 
21
 
 
22
    def test___init___sets_ivars(self):
 
23
        content_type = ContentType("foo", "bar")
 
24
        content = Content(content_type, lambda:["bytes"])
 
25
        self.assertEqual(content_type, content.content_type)
 
26
        self.assertEqual(["bytes"], list(content.iter_bytes()))
 
27
 
 
28
    def test___eq__(self):
 
29
        content_type = ContentType("foo", "bar")
 
30
        content1 = Content(content_type, lambda:["bytes"])
 
31
        content2 = Content(content_type, lambda:["bytes"])
 
32
        content3 = Content(content_type, lambda:["by", "tes"])
 
33
        content4 = Content(content_type, lambda:["by", "te"])
 
34
        content5 = Content(ContentType("f","b"), lambda:["by", "tes"])
 
35
        self.assertEqual(content1, content2)
 
36
        self.assertEqual(content1, content3)
 
37
        self.assertNotEqual(content1, content4)
 
38
        self.assertNotEqual(content1, content5)
 
39
 
 
40
    def test_iter_text_not_text_errors(self):
 
41
        content_type = ContentType("foo", "bar")
 
42
        content = Content(content_type, lambda:["bytes"])
 
43
        self.assertRaises(ValueError, content.iter_text)
 
44
 
 
45
    def test_iter_text_decodes(self):
 
46
        content_type = ContentType("text", "strange", {"charset":"utf8"})
 
47
        content = Content(content_type, lambda:[u"bytes\xea".encode("utf8")])
 
48
        self.assertEqual([u"bytes\xea"], list(content.iter_text()))
 
49
 
 
50
    def test_iter_text_default_charset_iso_8859_1(self):
 
51
        content_type = ContentType("text", "strange")
 
52
        text = u"bytes\xea"
 
53
        iso_version = text.encode("ISO-8859-1")
 
54
        content = Content(content_type, lambda:[iso_version])
 
55
        self.assertEqual([text], list(content.iter_text()))
 
56
 
 
57
 
 
58
class TestTracebackContent(unittest.TestCase):
 
59
 
 
60
    def test___init___None_errors(self):
 
61
        self.assertRaises(ValueError, TracebackContent, None, None)
 
62
 
 
63
    def test___init___sets_ivars(self):
 
64
        exc_info = sys.exc_info()
 
65
        content = TracebackContent(exc_info, self)
 
66
        content_type = ContentType("text", "x-traceback",
 
67
            {"language":"python"})
 
68
        self.assertEqual(content_type, content.content_type)
 
69
        result = unittest.TestResult()
 
70
        expected = result._exc_info_to_string(exc_info, self)
 
71
        self.assertEqual(expected, ''.join(list(content.iter_bytes())))