python (3.12.0)
1 import unittest
2
3 from importlib import import_module, resources
4 from . import data01
5 from . 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(encoding='utf-8')
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 = (
25 resources.files(self.data)
26 .joinpath('utf-8.file')
27 .read_text(encoding='utf-8')
28 )
29 self.assertEqual(result, 'Hello, UTF-8 world!\n')
30
31 def test_read_text_given_encoding(self):
32 result = (
33 resources.files(self.data)
34 .joinpath('utf-16.file')
35 .read_text(encoding='utf-16')
36 )
37 self.assertEqual(result, 'Hello, UTF-16 world!\n')
38
39 def test_read_text_with_errors(self):
40 """
41 Raises UnicodeError without the 'errors' argument.
42 """
43 target = resources.files(self.data) / 'utf-16.file'
44 self.assertRaises(UnicodeError, target.read_text, encoding='utf-8')
45 result = target.read_text(encoding='utf-8', errors='ignore')
46 self.assertEqual(
47 result,
48 'H\x00e\x00l\x00l\x00o\x00,\x00 '
49 '\x00U\x00T\x00F\x00-\x001\x006\x00 '
50 '\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00',
51 )
52
53
54 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):
55 data = data01
56
57
58 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):
59 def test_read_submodule_resource(self):
60 submodule = import_module('ziptestdata.subdirectory')
61 result = resources.files(submodule).joinpath('binary.file').read_bytes()
62 self.assertEqual(result, b'\0\1\2\3')
63
64 def test_read_submodule_resource_by_name(self):
65 result = (
66 resources.files('ziptestdata.subdirectory')
67 .joinpath('binary.file')
68 .read_bytes()
69 )
70 self.assertEqual(result, b'\0\1\2\3')
71
72
73 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):
74 def setUp(self):
75 from . import namespacedata01
76
77 self.data = namespacedata01
78
79
80 if __name__ == '__main__':
81 unittest.main()