| 1 | import typing |
| 2 | import unittest |
| 3 | |
| 4 | from importlib import resources |
| 5 | from importlib.abc import Traversable |
| 6 | from . import data01 |
| 7 | from .resources import util |
| 8 | |
| 9 | |
| 10 | class FilesTests: |
| 11 | def test_read_bytes(self): |
| 12 | files = resources.files(self.data) |
| 13 | actual = files.joinpath('utf-8.file').read_bytes() |
| 14 | assert actual == b'Hello, UTF-8 world!\n' |
| 15 | |
| 16 | def test_read_text(self): |
| 17 | files = resources.files(self.data) |
| 18 | actual = files.joinpath('utf-8.file').read_text(encoding='utf-8') |
| 19 | assert actual == 'Hello, UTF-8 world!\n' |
| 20 | |
| 21 | @unittest.skipUnless( |
| 22 | hasattr(typing, 'runtime_checkable'), |
| 23 | "Only suitable when typing supports runtime_checkable", |
| 24 | ) |
| 25 | def test_traversable(self): |
| 26 | assert isinstance(resources.files(self.data), Traversable) |
| 27 | |
| 28 | |
| 29 | class OpenDiskTests(FilesTests, unittest.TestCase): |
| 30 | def setUp(self): |
| 31 | self.data = data01 |
| 32 | |
| 33 | |
| 34 | class OpenZipTests(FilesTests, util.ZipSetup, unittest.TestCase): |
| 35 | pass |
| 36 | |
| 37 | |
| 38 | class OpenNamespaceTests(FilesTests, unittest.TestCase): |
| 39 | def setUp(self): |
| 40 | from . import namespacedata01 |
| 41 | |
| 42 | self.data = namespacedata01 |
| 43 | |
| 44 | |
| 45 | if __name__ == '__main__': |
| 46 | unittest.main() |