File: test_binhex.py

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; awk: 4
file content (68 lines) | stat: -rw-r--r-- 2,009 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
62
63
64
65
66
67
68
"""Test script for the binhex C module

   Uses the mechanism of the python binhex module
   Based on an original test by Roger E. Masse.
"""
import unittest
from test import support

with support.check_warnings(('', DeprecationWarning)):
    binhex = support.import_fresh_module('binhex')


class BinHexTestCase(unittest.TestCase):

    def setUp(self):
        # binhex supports only file names encodable to Latin1
        self.fname1 = support.TESTFN_ASCII + "1"
        self.fname2 = support.TESTFN_ASCII + "2"
        self.fname3 = support.TESTFN_ASCII + "very_long_filename__very_long_filename__very_long_filename__very_long_filename__"

    def tearDown(self):
        support.unlink(self.fname1)
        support.unlink(self.fname2)
        support.unlink(self.fname3)

    DATA = b'Jack is my hero'

    def test_binhex(self):
        with open(self.fname1, 'wb') as f:
            f.write(self.DATA)

        binhex.binhex(self.fname1, self.fname2)

        binhex.hexbin(self.fname2, self.fname1)

        with open(self.fname1, 'rb') as f:
            finish = f.readline()

        self.assertEqual(self.DATA, finish)

    def test_binhex_error_on_long_filename(self):
        """
        The testcase fails if no exception is raised when a filename parameter provided to binhex.binhex()
        is too long, or if the exception raised in binhex.binhex() is not an instance of binhex.Error.
        """
        f3 = open(self.fname3, 'wb')
        f3.close()

        self.assertRaises(binhex.Error, binhex.binhex, self.fname3, self.fname2)

    def test_binhex_line_endings(self):
        # bpo-29566: Ensure the line endings are those for macOS 9
        with open(self.fname1, 'wb') as f:
            f.write(self.DATA)

        binhex.binhex(self.fname1, self.fname2)

        with open(self.fname2, 'rb') as fp:
            contents = fp.read()

        self.assertNotIn(b'\n', contents)

def test_main():
    support.run_unittest(BinHexTestCase)


if __name__ == "__main__":
    test_main()