xref: /freebsd/contrib/file/python/tests.py (revision a2dfb7224ec9933ee804cae54d51848dce938b6b)
12dc4dbb9SEitan Adler# coding: utf-8
22dc4dbb9SEitan Adler
32dc4dbb9SEitan Adlerimport unittest
42dc4dbb9SEitan Adler
52dc4dbb9SEitan Adlerimport magic
62dc4dbb9SEitan Adler
72dc4dbb9SEitan Adler
82dc4dbb9SEitan Adlerclass MagicTestCase(unittest.TestCase):
92dc4dbb9SEitan Adler
102dc4dbb9SEitan Adler    filename = 'magic.py'
11*a2dfb722SXin LI    expected_mime_type = 'text/x-script.python'
122dc4dbb9SEitan Adler    expected_encoding = 'us-ascii'
132dc4dbb9SEitan Adler    expected_name = 'Python script, ASCII text executable'
142dc4dbb9SEitan Adler
152dc4dbb9SEitan Adler    def assert_result(self, result):
162dc4dbb9SEitan Adler        self.assertEqual(result.mime_type, self.expected_mime_type)
172dc4dbb9SEitan Adler        self.assertEqual(result.encoding, self.expected_encoding)
182dc4dbb9SEitan Adler        self.assertEqual(result.name, self.expected_name)
192dc4dbb9SEitan Adler
202dc4dbb9SEitan Adler    def test_detect_from_filename(self):
212dc4dbb9SEitan Adler        result = magic.detect_from_filename(self.filename)
222dc4dbb9SEitan Adler        self.assert_result(result)
232dc4dbb9SEitan Adler
242dc4dbb9SEitan Adler    def test_detect_from_fobj(self):
25*a2dfb722SXin LI        with open(self.filename, "rb") as fobj:
262dc4dbb9SEitan Adler            result = magic.detect_from_fobj(fobj)
272dc4dbb9SEitan Adler        self.assert_result(result)
282dc4dbb9SEitan Adler
292dc4dbb9SEitan Adler    def test_detect_from_content(self):
30*a2dfb722SXin LI        with open(self.filename, "rb") as fobj:
31*a2dfb722SXin LI            result = magic.detect_from_content(fobj.read(8192))
322dc4dbb9SEitan Adler        self.assert_result(result)
33