xref: /netbsd-src/external/bsd/file/dist/python/tests.py (revision a77ebd868432a4d7e595fb7709cfc1b8f144789b)
1006f8008Schristos# coding: utf-8
2006f8008Schristos
3006f8008Schristosimport unittest
4006f8008Schristos
5006f8008Schristosimport magic
6006f8008Schristos
7006f8008Schristos
8006f8008Schristosclass MagicTestCase(unittest.TestCase):
9006f8008Schristos
10006f8008Schristos    filename = 'magic.py'
11*a77ebd86Schristos    expected_mime_type = 'text/x-script.python'
12006f8008Schristos    expected_encoding = 'us-ascii'
13006f8008Schristos    expected_name = 'Python script, ASCII text executable'
14006f8008Schristos
15006f8008Schristos    def assert_result(self, result):
16006f8008Schristos        self.assertEqual(result.mime_type, self.expected_mime_type)
17006f8008Schristos        self.assertEqual(result.encoding, self.expected_encoding)
18006f8008Schristos        self.assertEqual(result.name, self.expected_name)
19006f8008Schristos
20006f8008Schristos    def test_detect_from_filename(self):
21006f8008Schristos        result = magic.detect_from_filename(self.filename)
22006f8008Schristos        self.assert_result(result)
23006f8008Schristos
24006f8008Schristos    def test_detect_from_fobj(self):
25*a77ebd86Schristos        with open(self.filename, "rb") as fobj:
26006f8008Schristos            result = magic.detect_from_fobj(fobj)
27006f8008Schristos        self.assert_result(result)
28006f8008Schristos
29006f8008Schristos    def test_detect_from_content(self):
30*a77ebd86Schristos        with open(self.filename, "rb") as fobj:
31*a77ebd86Schristos            result = magic.detect_from_content(fobj.read(8192))
32006f8008Schristos        self.assert_result(result)
33