File: test_fscompat.py

package info (click to toggle)
python-fs 2.4.16-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,944 kB
  • sloc: python: 13,048; makefile: 226; sh: 3
file content (61 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download | duplicates (2)
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
from __future__ import unicode_literals

import six
import unittest

from fs._fscompat import fsdecode, fsencode, fspath


class PathMock(object):
    def __init__(self, path):
        self._path = path

    def __fspath__(self):
        return self._path


class BrokenPathMock(object):
    def __init__(self, path):
        self._path = path

    def __fspath__(self):
        return self.broken


class TestFSCompact(unittest.TestCase):
    def test_fspath(self):
        path = PathMock("foo")
        self.assertEqual(fspath(path), "foo")
        path = PathMock(b"foo")
        self.assertEqual(fspath(path), b"foo")
        path = "foo"
        assert path is fspath(path)

        with self.assertRaises(TypeError):
            fspath(100)

        with self.assertRaises(TypeError):
            fspath(PathMock(5))

        with self.assertRaises(AttributeError):
            fspath(BrokenPathMock("foo"))

    def test_fsencode(self):
        encode_bytes = fsencode(b"foo")
        assert isinstance(encode_bytes, bytes)
        self.assertEqual(encode_bytes, b"foo")

        encode_bytes = fsencode("foo")
        assert isinstance(encode_bytes, bytes)
        self.assertEqual(encode_bytes, b"foo")

        with self.assertRaises(TypeError):
            fsencode(5)

    def test_fsdecode(self):
        decode_text = fsdecode(b"foo")
        assert isinstance(decode_text, six.text_type)
        decode_text = fsdecode("foo")
        assert isinstance(decode_text, six.text_type)
        with self.assertRaises(TypeError):
            fsdecode(5)