summaryrefslogtreecommitdiff
path: root/subvertpy/tests/test_marshall.py
blob: 47d659dcc0ed90a17ba9257b2af74beab2497241 (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
# Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@jelmer.uk>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA

"""Tests for subvertpy.marshall."""

from subvertpy.marshall import (
    MarshallError,
    literal,
    marshall,
    unmarshall,
    )
from subvertpy.tests import TestCase


class TestMarshalling(TestCase):

    def test_literal_txt(self):
        line = literal("foo")
        self.assertEqual("foo", line.txt)

    def test_literal_str(self):
        line = literal("foo bar")
        self.assertEqual("foo bar", line.__str__())

    def test_literal_rep(self):
        line = literal("foo bar")
        self.assertEqual("foo bar", line.__repr__())

    def test_marshall_error(self):
        err = MarshallError("bla bla")
        self.assertEqual("bla bla", err.__str__())

    def test_marshall_int(self):
        self.assertEqual(b"1 ", marshall(1))

    def test_marshall_list(self):
        self.assertEqual(b"( 1 2 3 4 ) ", marshall([1, 2, 3, 4]))

    def test_marshall_list_mixed(self):
        self.assertEqual(b"( 1 3 4 3:str ) ", marshall([1, 3, 4, "str"]))

    def test_marshall_literal(self):
        self.assertEqual(b"foo ", marshall(literal("foo")))

    def test_marshall_string(self):
        self.assertEqual(b"3:foo ", marshall("foo"))

    def test_marshall_raises(self):
        self.assertRaises(MarshallError, marshall, dict())

    def test_marshall_list_nested(self):
        self.assertEqual(b"( ( ( 3 ) 4 ) ) ", marshall([[[3], 4]]))

    def test_marshall_string_space(self):
        self.assertEqual(b"5:bla l ", marshall("bla l"))

    def test_unmarshall_string(self):
        self.assertEqual((b'', b"bla l"), unmarshall(b"5:bla l"))

    def test_unmarshall_list(self):
        self.assertEqual((b'', [4, 5]), unmarshall(b"( 4 5 ) "))

    def test_unmarshall_int(self):
        self.assertEqual((b'', 2), unmarshall(b"2 "))

    def test_unmarshall_literal(self):
        self.assertEqual((b'', literal("x")), unmarshall(b"x "))

    def test_unmarshall_empty(self):
        self.assertRaises(MarshallError, unmarshall, b"")

    def test_unmarshall_nospace(self):
        self.assertRaises(MarshallError, unmarshall, b"nospace")

    def test_unmarshall_toolong(self):
        self.assertRaises(MarshallError, unmarshall, b"43432432:bla")

    def test_unmarshall_literal_negative(self):
        self.assertRaises(MarshallError, unmarshall, b":-3213")

    def test_unmarshall_open_list(self):
        self.assertRaises(MarshallError, unmarshall, b"( 3 4 ")