source: test/test_tool.py @ 1663:40280d912474

Last change on this file since 1663:40280d912474 was 1663:40280d912474, checked in by Stefan Schwarzer <sschwarzer@…>, 3 years ago
Remove `unittest` dependency - Remove `unittest` imports - Let test case classes inherit from `object` instead of `unittest.TestCase` - Use `setup_method` and `teardown_method` instead of `unittest`'s `setUp` and `tearDown` - Use `pytest.mark.skipif` instead of `unittest.skipIf` - Use `pytest.mark.slow_test` instead of decorator in `test/__init__.py`. Exclude slow tests with py.test -m "not slow_test" test - Replace some leftover `assert*` calls with `assert` statements - Replace `assert False, ...` with `pytest.fail` 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 ftputil.compat as compat
8import ftputil.tool
9
10
11class TestSameStringTypeAs(object):
12
13    # The first check for equality is enough for Python 3, where
14    # comparing a byte string and unicode string would raise an
15    # exception. However, we need the second test for Python 2.
16
17    def test_to_bytes(self):
18        result = ftputil.tool.same_string_type_as(b"abc", "def")
19        assert result == b"def"
20        assert isinstance(result, compat.bytes_type)
21
22    def test_to_unicode(self):
23        result = ftputil.tool.same_string_type_as("abc", b"def")
24        assert result == "def"
25        assert isinstance(result, compat.unicode_type)
26
27    def test_both_bytes_type(self):
28        result = ftputil.tool.same_string_type_as(b"abc", b"def")
29        assert result == b"def"
30        assert isinstance(result, compat.bytes_type)
31
32    def test_both_unicode_type(self):
33        result = ftputil.tool.same_string_type_as("abc", "def")
34        assert result == "def"
35        assert isinstance(result, compat.unicode_type)
36
37
38class TestSimpleConversions(object):
39
40    def test_as_bytes(self):
41        result = ftputil.tool.as_bytes(b"abc")
42        assert result == b"abc"
43        assert isinstance(result, compat.bytes_type)
44        result = ftputil.tool.as_bytes("abc")
45        assert result == b"abc"
46        assert isinstance(result, compat.bytes_type)
47       
48    def test_as_unicode(self):
49        result = ftputil.tool.as_unicode(b"abc")
50        assert result == "abc"
51        assert isinstance(result, compat.unicode_type)
52        result = ftputil.tool.as_unicode("abc")
53        assert result == "abc"
54        assert isinstance(result, compat.unicode_type)
55
56
57class TestEncodeIfUnicode(object):
58
59    def test_do_encode(self):
60        string = "abc"
61        converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
62        assert isinstance(converted_string, compat.bytes_type)
63
64    def test_dont_encode(self):
65        string = b"abc"
66        not_converted_string = ftputil.tool.encode_if_unicode(string, "latin1")
67        assert string == not_converted_string
68        assert isinstance(not_converted_string, compat.bytes_type)
Note: See TracBrowser for help on using the repository browser.