File: test_tool.py

package info (click to toggle)
python-ftputil 3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 848 kB
  • sloc: python: 3,308; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 2,331 bytes parent folder | download
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
# Copyright (C) 2013-2016, Stefan Schwarzer
# and ftputil contributors (see `doc/contributors.txt`)
# See the file LICENSE for licensing terms.

from __future__ import unicode_literals

import ftputil.compat as compat
import ftputil.tool


class TestSameStringTypeAs(object):

    # The first check for equality is enough for Python 3, where
    # comparing a byte string and unicode string would raise an
    # exception. However, we need the second test for Python 2.

    def test_to_bytes(self):
        result = ftputil.tool.same_string_type_as(b"abc", "def")
        assert result == b"def"
        assert isinstance(result, compat.bytes_type)

    def test_to_unicode(self):
        result = ftputil.tool.same_string_type_as("abc", b"def")
        assert result == "def"
        assert isinstance(result, compat.unicode_type)

    def test_both_bytes_type(self):
        result = ftputil.tool.same_string_type_as(b"abc", b"def")
        assert result == b"def"
        assert isinstance(result, compat.bytes_type)

    def test_both_unicode_type(self):
        result = ftputil.tool.same_string_type_as("abc", "def")
        assert result == "def"
        assert isinstance(result, compat.unicode_type)


class TestSimpleConversions(object):

    def test_as_bytes(self):
        result = ftputil.tool.as_bytes(b"abc")
        assert result == b"abc"
        assert isinstance(result, compat.bytes_type)
        result = ftputil.tool.as_bytes("abc")
        assert result == b"abc"
        assert isinstance(result, compat.bytes_type)
        
    def test_as_unicode(self):
        result = ftputil.tool.as_unicode(b"abc")
        assert result == "abc"
        assert isinstance(result, compat.unicode_type)
        result = ftputil.tool.as_unicode("abc")
        assert result == "abc"
        assert isinstance(result, compat.unicode_type)


class TestEncodeIfUnicode(object):

    def test_do_encode(self):
        string = "abc"
        converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
        assert isinstance(converted_string, compat.bytes_type)

    def test_dont_encode(self):
        string = b"abc"
        not_converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
        assert string == not_converted_string
        assert isinstance(not_converted_string, compat.bytes_type)