source: test/test_tool.py @ 1718:8bed138bc404

Last change on this file since 1718:8bed138bc404 was 1718:8bed138bc404, checked in by Stefan Schwarzer <sschwarzer@…>, 9 months ago
Don't inherit from `object` This is no longer needed because we're targeting Python 3 only now.
File size: 2.2 KB
Line 
1# Copyright (C) 2013-2018, Stefan Schwarzer
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5import ftputil.compat as compat
6import ftputil.tool
7
8
9class TestSameStringTypeAs:
10
11    # The first check for equality is enough for Python 3, where
12    # comparing a byte string and unicode string would raise an
13    # exception. However, we need the second test for Python 2.
14
15    def test_to_bytes(self):
16        result = ftputil.tool.same_string_type_as(b"abc", "def")
17        assert result == b"def"
18        assert isinstance(result, compat.bytes_type)
19
20    def test_to_unicode(self):
21        result = ftputil.tool.same_string_type_as("abc", b"def")
22        assert result == "def"
23        assert isinstance(result, compat.unicode_type)
24
25    def test_both_bytes_type(self):
26        result = ftputil.tool.same_string_type_as(b"abc", b"def")
27        assert result == b"def"
28        assert isinstance(result, compat.bytes_type)
29
30    def test_both_unicode_type(self):
31        result = ftputil.tool.same_string_type_as("abc", "def")
32        assert result == "def"
33        assert isinstance(result, compat.unicode_type)
34
35
36class TestSimpleConversions:
37
38    def test_as_bytes(self):
39        result = ftputil.tool.as_bytes(b"abc")
40        assert result == b"abc"
41        assert isinstance(result, compat.bytes_type)
42        result = ftputil.tool.as_bytes("abc")
43        assert result == b"abc"
44        assert isinstance(result, compat.bytes_type)
45       
46    def test_as_unicode(self):
47        result = ftputil.tool.as_unicode(b"abc")
48        assert result == "abc"
49        assert isinstance(result, compat.unicode_type)
50        result = ftputil.tool.as_unicode("abc")
51        assert result == "abc"
52        assert isinstance(result, compat.unicode_type)
53
54
55class TestEncodeIfUnicode:
56
57    def test_do_encode(self):
58        string = "abc"
59        converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
60        assert isinstance(converted_string, compat.bytes_type)
61
62    def test_dont_encode(self):
63        string = b"abc"
64        not_converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
65        assert string == not_converted_string
66        assert isinstance(not_converted_string, compat.bytes_type)
Note: See TracBrowser for help on using the repository browser.