1#!/usr/bin/env python
2# encoding: utf-8
3from __future__ import absolute_import, division, unicode_literals
4import unittest
5
6from cola import core
7
8from . import helper
9
10
11class CoreColaUnicodeTestCase(unittest.TestCase):
12    """Tests the cola.core module's unicode handling"""
13
14    def test_core_decode(self):
15        """Test the core.decode function"""
16        filename = helper.fixture('unicode.txt')
17        expect = core.decode(core.encode('unicøde'))
18        actual = core.read(filename).strip()
19        self.assertEqual(expect, actual)
20
21    def test_core_encode(self):
22        """Test the core.encode function"""
23        filename = helper.fixture('unicode.txt')
24        expect = core.encode('unicøde')
25        actual = core.encode(core.read(filename).strip())
26        self.assertEqual(expect, actual)
27
28    def test_decode_None(self):
29        """Ensure that decode(None) returns None"""
30        expect = None
31        actual = core.decode(None)
32        self.assertEqual(expect, actual)
33
34    def test_decode_utf8(self):
35        filename = helper.fixture('cyrillic-utf-8.txt')
36        actual = core.read(filename)
37        self.assertEqual(actual.encoding, 'utf-8')
38
39    def test_decode_non_utf8(self):
40        filename = helper.fixture('cyrillic-cp1251.txt')
41        actual = core.read(filename)
42        self.assertEqual(actual.encoding, 'iso-8859-15')
43
44    def test_decode_non_utf8_string(self):
45        filename = helper.fixture('cyrillic-cp1251.txt')
46        with open(filename, 'rb') as f:
47            content = f.read()
48        actual = core.decode(content)
49        self.assertEqual(actual.encoding, 'iso-8859-15')
50
51    def test_guess_mimetype(self):
52        value = '字龍.txt'
53        expect = 'text/plain'
54        actual = core.guess_mimetype(value)
55        self.assertEqual(expect, actual)
56        # This function is robust to bytes vs. unicode
57        actual = core.guess_mimetype(core.encode(value))
58        self.assertEqual(expect, actual)
59
60
61if __name__ == '__main__':
62    unittest.main()
63