source: test/test_tool.py @ 1660:93ea351f922b

Last change on this file since 1660:93ea351f922b was 1660:93ea351f922b, checked in by Stefan Schwarzer <sschwarzer@…>, 3 years ago
Remove `unittest.main` calls Since we now use pytest as test runner and not a shell loop in the makefile, there's no longer a need for explicit `unittest.main` calls. ticket: 98
File size: 2.3 KB
Line 
1# Copyright (C) 2013-2016, Stefan Schwarzer
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5from __future__ import unicode_literals
6
7import unittest
8
9import ftputil.compat as compat
10import ftputil.tool
11
12
13class TestSameStringTypeAs(unittest.TestCase):
14
15    # The first check for equality is enough for Python 3, where
16    # comparing a byte string and unicode string would raise an
17    # exception. However, we need the second test for Python 2.
18
19    def test_to_bytes(self):
20        result = ftputil.tool.same_string_type_as(b"abc", "def")
21        assert result == b"def"
22        assert isinstance(result, compat.bytes_type)
23
24    def test_to_unicode(self):
25        result = ftputil.tool.same_string_type_as("abc", b"def")
26        assert result == "def"
27        assert isinstance(result, compat.unicode_type)
28
29    def test_both_bytes_type(self):
30        result = ftputil.tool.same_string_type_as(b"abc", b"def")
31        assert result == b"def"
32        assert isinstance(result, compat.bytes_type)
33
34    def test_both_unicode_type(self):
35        result = ftputil.tool.same_string_type_as("abc", "def")
36        assert result == "def"
37        assert isinstance(result, compat.unicode_type)
38
39
40class TestSimpleConversions(unittest.TestCase):
41
42    def test_as_bytes(self):
43        result = ftputil.tool.as_bytes(b"abc")
44        assert result == b"abc"
45        assert isinstance(result, compat.bytes_type)
46        result = ftputil.tool.as_bytes("abc")
47        assert result == b"abc"
48        assert isinstance(result, compat.bytes_type)
49       
50    def test_as_unicode(self):
51        result = ftputil.tool.as_unicode(b"abc")
52        assert result == "abc"
53        assert isinstance(result, compat.unicode_type)
54        result = ftputil.tool.as_unicode("abc")
55        assert result == "abc"
56        assert isinstance(result, compat.unicode_type)
57
58
59class TestEncodeIfUnicode(unittest.TestCase):
60
61    def test_do_encode(self):
62        string = "abc"
63        converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
64        assert isinstance(converted_string, compat.bytes_type)
65
66    def test_dont_encode(self):
67        string = b"abc"
68        not_converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
69        assert string == not_converted_string
70        assert isinstance(not_converted_string, compat.bytes_type)
Note: See TracBrowser for help on using the repository browser.