xref: /freebsd/contrib/file/python/tests.py (revision a2dfb7224ec9933ee804cae54d51848dce938b6b)
1# coding: utf-8
2
3import unittest
4
5import magic
6
7
8class MagicTestCase(unittest.TestCase):
9
10    filename = 'magic.py'
11    expected_mime_type = 'text/x-script.python'
12    expected_encoding = 'us-ascii'
13    expected_name = 'Python script, ASCII text executable'
14
15    def assert_result(self, result):
16        self.assertEqual(result.mime_type, self.expected_mime_type)
17        self.assertEqual(result.encoding, self.expected_encoding)
18        self.assertEqual(result.name, self.expected_name)
19
20    def test_detect_from_filename(self):
21        result = magic.detect_from_filename(self.filename)
22        self.assert_result(result)
23
24    def test_detect_from_fobj(self):
25        with open(self.filename, "rb") as fobj:
26            result = magic.detect_from_fobj(fobj)
27        self.assert_result(result)
28
29    def test_detect_from_content(self):
30        with open(self.filename, "rb") as fobj:
31            result = magic.detect_from_content(fobj.read(8192))
32        self.assert_result(result)
33