source: test/test_tool.py @ 1721:3557f65ded13

Last change on this file since 1721:3557f65ded13 was 1721:3557f65ded13, checked in by Stefan Schwarzer <sschwarzer@…>, 7 months ago
Remove `compat.py` This module was for Python 2/3 compatibility, similar to the `six` package. Since the next version of ftputil will only support Python 3, hardcode the types from `compat.py` in the code that used to use the `compat` module. There may still be redundant code that isn't needed when running the tests under Python 3.
File size: 1.7 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.tool
6
7
8class TestSameStringTypeAs:
9
10    # The first check for equality is enough for Python 3, where
11    # comparing a byte string and unicode string would raise an
12    # exception. However, we need the second test for Python 2.
13
14    def test_to_bytes(self):
15        result = ftputil.tool.same_string_type_as(b"abc", "def")
16        assert result == b"def"
17
18    def test_to_unicode(self):
19        result = ftputil.tool.same_string_type_as("abc", b"def")
20        assert result == "def"
21
22    def test_both_bytes_type(self):
23        result = ftputil.tool.same_string_type_as(b"abc", b"def")
24        assert result == b"def"
25
26    def test_both_unicode_type(self):
27        result = ftputil.tool.same_string_type_as("abc", "def")
28        assert result == "def"
29
30
31class TestSimpleConversions:
32
33    def test_as_bytes(self):
34        result = ftputil.tool.as_bytes(b"abc")
35        assert result == b"abc"
36        result = ftputil.tool.as_bytes("abc")
37        assert result == b"abc"
38
39    def test_as_unicode(self):
40        result = ftputil.tool.as_unicode(b"abc")
41        assert result == "abc"
42        result = ftputil.tool.as_unicode("abc")
43        assert result == "abc"
44
45
46class TestEncodeIfUnicode:
47
48    def test_do_encode(self):
49        string = "abc"
50        converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
51        assert converted_string == b"abc"
52
53    def test_dont_encode(self):
54        string = b"abc"
55        not_converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
56        assert string == not_converted_string
Note: See TracBrowser for help on using the repository browser.