summaryrefslogtreecommitdiff
path: root/python/subunit/tests/test_details.py
blob: f3c70d2924609f59450d494d4595cd91ec9b5d77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
#  subunit: extensions to python unittest to get test results from subprocesses.
#  Copyright (C) 2005  Robert Collins <robertc@robertcollins.net>
#
#  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
#  license at the users choice. A copy of both licenses are available in the
#  project source as Apache-2.0 and BSD. You may not use this file except in
#  compliance with one of these two licences.
#  
#  Unless required by applicable law or agreed to in writing, software
#  distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
#  license you chose for the specific language governing permissions and
#  limitations under that license.
#

import unittest

from testtools.compat import _b
try:
    from testtools.compat import StringIO
except ImportError:
    from io import StringIO

import subunit.tests
from subunit import content, content_type, details


class TestSimpleDetails(unittest.TestCase):

    def test_lineReceived(self):
        parser = details.SimpleDetailsParser(None)
        parser.lineReceived(_b("foo\n"))
        parser.lineReceived(_b("bar\n"))
        self.assertEqual(_b("foo\nbar\n"), parser._message)

    def test_lineReceived_escaped_bracket(self):
        parser = details.SimpleDetailsParser(None)
        parser.lineReceived(_b("foo\n"))
        parser.lineReceived(_b(" ]are\n"))
        parser.lineReceived(_b("bar\n"))
        self.assertEqual(_b("foo\n]are\nbar\n"), parser._message)

    def test_get_message(self):
        parser = details.SimpleDetailsParser(None)
        self.assertEqual(_b(""), parser.get_message())

    def test_get_details(self):
        parser = details.SimpleDetailsParser(None)
        traceback = ""
        expected = {}
        expected['traceback'] = content.Content(
            content_type.ContentType("text", "x-traceback",
                {'charset': 'utf8'}),
            lambda:[_b("")])
        found = parser.get_details()
        self.assertEqual(expected.keys(), found.keys())
        self.assertEqual(expected['traceback'].content_type,
            found['traceback'].content_type)
        self.assertEqual(_b('').join(expected['traceback'].iter_bytes()),
            _b('').join(found['traceback'].iter_bytes()))

    def test_get_details_skip(self):
        parser = details.SimpleDetailsParser(None)
        traceback = ""
        expected = {}
        expected['reason'] = content.Content(
            content_type.ContentType("text", "plain"),
            lambda:[_b("")])
        found = parser.get_details("skip")
        self.assertEqual(expected, found)

    def test_get_details_success(self):
        parser = details.SimpleDetailsParser(None)
        traceback = ""
        expected = {}
        expected['message'] = content.Content(
            content_type.ContentType("text", "plain"),
            lambda:[_b("")])
        found = parser.get_details("success")
        self.assertEqual(expected, found)


class TestMultipartDetails(unittest.TestCase):

    def test_get_message_is_None(self):
        parser = details.MultipartDetailsParser(None)
        self.assertEqual(None, parser.get_message())

    def test_get_details(self):
        parser = details.MultipartDetailsParser(None)
        self.assertEqual({}, parser.get_details())

    def test_parts(self):
        parser = details.MultipartDetailsParser(None)
        parser.lineReceived(_b("Content-Type: text/plain\n"))
        parser.lineReceived(_b("something\n"))
        parser.lineReceived(_b("F\r\n"))
        parser.lineReceived(_b("serialised\n"))
        parser.lineReceived(_b("form0\r\n"))
        expected = {}
        expected['something'] = content.Content(
            content_type.ContentType("text", "plain"),
            lambda:[_b("serialised\nform")])
        found = parser.get_details()
        self.assertEqual(expected.keys(), found.keys())
        self.assertEqual(expected['something'].content_type,
            found['something'].content_type)
        self.assertEqual(_b('').join(expected['something'].iter_bytes()),
            _b('').join(found['something'].iter_bytes()))