python (3.12.0)
1 import unittest
2
3 from importlib import 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 target = resources.files(package).joinpath(path)
11 with target.open('rb'):
12 pass
13
14
15 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):
16 def execute(self, package, path):
17 target = resources.files(package).joinpath(path)
18 with target.open(encoding='utf-8'):
19 pass
20
21
22 class ESC[4;38;5;81mOpenTests:
23 def test_open_binary(self):
24 target = resources.files(self.data) / 'binary.file'
25 with target.open('rb') as fp:
26 result = fp.read()
27 self.assertEqual(result, b'\x00\x01\x02\x03')
28
29 def test_open_text_default_encoding(self):
30 target = resources.files(self.data) / 'utf-8.file'
31 with target.open(encoding='utf-8') as fp:
32 result = fp.read()
33 self.assertEqual(result, 'Hello, UTF-8 world!\n')
34
35 def test_open_text_given_encoding(self):
36 target = resources.files(self.data) / 'utf-16.file'
37 with target.open(encoding='utf-16', errors='strict') as fp:
38 result = fp.read()
39 self.assertEqual(result, 'Hello, UTF-16 world!\n')
40
41 def test_open_text_with_errors(self):
42 """
43 Raises UnicodeError without the 'errors' argument.
44 """
45 target = resources.files(self.data) / 'utf-16.file'
46 with target.open(encoding='utf-8', errors='strict') as fp:
47 self.assertRaises(UnicodeError, fp.read)
48 with target.open(encoding='utf-8', errors='ignore') as fp:
49 result = fp.read()
50 self.assertEqual(
51 result,
52 'H\x00e\x00l\x00l\x00o\x00,\x00 '
53 '\x00U\x00T\x00F\x00-\x001\x006\x00 '
54 '\x00w\x00o\x00r\x00l\x00d\x00!\x00\n\x00',
55 )
56
57 def test_open_binary_FileNotFoundError(self):
58 target = resources.files(self.data) / 'does-not-exist'
59 with self.assertRaises(FileNotFoundError):
60 target.open('rb')
61
62 def test_open_text_FileNotFoundError(self):
63 target = resources.files(self.data) / 'does-not-exist'
64 with self.assertRaises(FileNotFoundError):
65 target.open(encoding='utf-8')
66
67
68 class ESC[4;38;5;81mOpenDiskTests(ESC[4;38;5;149mOpenTests, ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
69 def setUp(self):
70 self.data = data01
71
72
73 class ESC[4;38;5;81mOpenDiskNamespaceTests(ESC[4;38;5;149mOpenTests, 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 class ESC[4;38;5;81mOpenZipTests(ESC[4;38;5;149mOpenTests, 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):
81 pass
82
83
84 if __name__ == '__main__':
85 unittest.main()