1 import unittest
2
3 from importlib import import_module, resources
4 from . import data01
5 from .resources import util
6
7
8 class ESC[4;38;5;81mCommonBinaryTests(ESC[4;38;5;149mutilESC[4;38;5;149m.ESC[4;38;5;149mCommonTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
9 def execute(self, package, path):
10 resources.files(package).joinpath(path).read_bytes()
11
12
13 class ESC[4;38;5;81mCommonTextTests(ESC[4;38;5;149mutilESC[4;38;5;149m.ESC[4;38;5;149mCommonTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
14 def execute(self, package, path):
15 resources.files(package).joinpath(path).read_text()
16
17
18 class ESC[4;38;5;81mReadTests:
19 def test_read_bytes(self):
20 result = resources.files(self.data).joinpath('binary.file').read_bytes()
21 self.assertEqual(result, b'\0\1\2\3')
22
23 def test_read_text_default_encoding(self):
24 result = resources.files(self.data).joinpath('utf-8.file').read_text()
25 self.assertEqual(result, 'Hello, UTF-8 world!\n')
26
27 def test_read_text_given_encoding(self):
28 result = (
29 resources.files(self.data)
30 .joinpath('utf-16.file')
31 .read_text(encoding='utf-16')
32 )
33 self.assertEqual(result, 'Hello, UTF-16 world!\n')
34
35 def test_read_text_with_errors(self):
36 # Raises UnicodeError without the 'errors' argument.
37 target = resources.files(self.data) / 'utf-16.file'
38 self.assertRaises(UnicodeError, target.read_text, encoding='utf-8')
39 result = target.read_text(encoding='utf-8', errors='ignore')
40 self.assertEqual(
41 result,
42 'H\x00e\x00l\x00l\x00o\x00,\x00 '
43 '\x00U\x00T\x00F\x00-\x001\x006\x00 '
44 '\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00',
45 )
46
47
48 class ESC[4;38;5;81mReadDiskTests(ESC[4;38;5;149mReadTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
49 data = data01
50
51
52 class ESC[4;38;5;81mReadZipTests(ESC[4;38;5;149mReadTests, ESC[4;38;5;149mutilESC[4;38;5;149m.ESC[4;38;5;149mZipSetup, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
53 def test_read_submodule_resource(self):
54 submodule = import_module('ziptestdata.subdirectory')
55 result = resources.files(submodule).joinpath('binary.file').read_bytes()
56 self.assertEqual(result, b'\0\1\2\3')
57
58 def test_read_submodule_resource_by_name(self):
59 result = (
60 resources.files('ziptestdata.subdirectory')
61 .joinpath('binary.file')
62 .read_bytes()
63 )
64 self.assertEqual(result, b'\0\1\2\3')
65
66
67 class ESC[4;38;5;81mReadNamespaceTests(ESC[4;38;5;149mReadTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
68 def setUp(self):
69 from . import namespacedata01
70
71 self.data = namespacedata01
72
73
74 if __name__ == '__main__':
75 unittest.main()