aboutsummaryrefslogtreecommitdiff
blob: 5cbefb23698d2d57b140fd6e6149c0db880c657a (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
# TODO: deprecated, remove in 0.9.0

import pytest

from snakeoil import stringio


class base:

    encoding = None
    kls = None

    def convert_data(self, data):
        if self.encoding is None:
            return data
        return data.encode(self.encoding)

    def unconvert_data(self, data):
        if self.encoding is None:
            return data
        return data.decode(self.encoding)


class readonly_mixin(base):

    def test_nonwritable(self):
        convert = self.convert_data
        obj = self.kls(convert("adsf"))
        with pytest.raises(TypeError):
            obj.write(convert("bow ties"))
        with pytest.raises(TypeError):
            obj.writelines(convert("are cool"), convert(" so says the doctor"))
        with pytest.raises(TypeError):
            obj.truncate()


class writable_mixin(base):

    def test_writable(self):
        convert = self.convert_data
        obj = self.kls(convert("bow ties"))
        assert obj.getvalue() == convert("bow ties")
        # assert we start at 0
        assert obj.tell() == 0
        obj.write(convert("are cool"))
        assert obj.getvalue() == convert("are cool")
        obj.seek(0)
        obj.truncate(0)
        assert obj.getvalue() == convert("")


class Test_text_readonly(readonly_mixin):
    kls = stringio.text_readonly

class Test_text_writable(writable_mixin):
    kls = stringio.text_writable

class Test_bytes_readonly(readonly_mixin ):
    kls = stringio.bytes_readonly
    encoding = 'utf8'

class Test_bytes_writable(writable_mixin ):
    kls = stringio.bytes_writable
    encoding = 'utf8'