File: test_compat.py

package info (click to toggle)
python-webob 1%3A1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,200 kB
  • ctags: 2,604
  • sloc: python: 14,014; makefile: 169
file content (48 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download | duplicates (3)
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
import unittest

from webob.compat import text_type

class text_Tests(unittest.TestCase):
    def _callFUT(self, *arg, **kw):
        from webob.compat import text_
        return text_(*arg, **kw)

    def test_binary(self):
        result = self._callFUT(b'123')
        self.assertTrue(isinstance(result, text_type))
        self.assertEqual(result, text_type(b'123', 'ascii'))

    def test_binary_alternate_decoding(self):
        result = self._callFUT(b'La Pe\xc3\xb1a', 'utf-8')
        self.assertTrue(isinstance(result, text_type))
        self.assertEqual(result, text_type(b'La Pe\xc3\xb1a', 'utf-8'))

    def test_binary_decoding_error(self):
        self.assertRaises(UnicodeDecodeError, self._callFUT, b'\xff', 'utf-8')

    def test_text(self):
        result = self._callFUT(text_type(b'123', 'ascii'))
        self.assertTrue(isinstance(result, text_type))
        self.assertEqual(result, text_type(b'123', 'ascii'))

class bytes_Tests(unittest.TestCase):
    def _callFUT(self, *arg, **kw):
        from webob.compat import bytes_
        return bytes_(*arg, **kw)

    def test_binary(self):
        result = self._callFUT(b'123')
        self.assertTrue(isinstance(result, bytes))
        self.assertEqual(result, b'123')

    def test_text(self):
        val = text_type(b'123', 'ascii')
        result = self._callFUT(val)
        self.assertTrue(isinstance(result, bytes))
        self.assertEqual(result, b'123')

    def test_text_alternate_encoding(self):
        val = text_type(b'La Pe\xc3\xb1a', 'utf-8')
        result = self._callFUT(val, 'utf-8')
        self.assertTrue(isinstance(result, bytes))
        self.assertEqual(result, b'La Pe\xc3\xb1a')