File: test_utility.py

package info (click to toggle)
unidecode 1.0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,608 kB
  • sloc: python: 49,149; perl: 30; sh: 24; makefile: 5
file content (72 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download | duplicates (2)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# vim:ts=4 sw=4 expandtab softtabstop=4
import os
import locale
import unittest
import subprocess
import sys
import tempfile

PY3 = sys.version_info[0] >= 3

here = os.path.dirname(__file__)

if PY3:
    def _u(x):
        return x
else:
    def _u(x):
        return x.decode('unicode-escape')

def get_cmd():
    sys_path = os.path.join(here, "..")

    return [sys.executable, "-c",
            "import sys; sys.path.insert(0, '%s'); from unidecode.util import main; main()" % (sys_path,)]

def run(argv):
    cmd = get_cmd()
    p = subprocess.Popen(cmd + argv, stderr=subprocess.PIPE, stdout=subprocess.PIPE)

    out, err = p.communicate()

    return out.decode('ascii'), err.decode('ascii')

def temp(content):
    f = tempfile.NamedTemporaryFile()
    f.write(content)
    f.flush()
    return f

class TestUnidecodeUtility(unittest.TestCase):

    TEST_UNICODE = _u('\u9769')
    TEST_ASCII = 'Ge '

    def test_encoding_error(self):
        f = temp(self.TEST_UNICODE.encode('sjis'))
        out, err = run(['-e', 'utf8', f.name])

        expected = 'Unable to decode input: invalid start byte, start: 0, end: 1\n'
        self.assertEqual(err, expected)

    def test_file_specified_encoding(self):
        f = temp(self.TEST_UNICODE.encode('sjis'))

        out, err = run(['-e', 'sjis', f.name])
        self.assertEqual(out, self.TEST_ASCII)

    def test_file_default_encoding(self):
        f = temp(self.TEST_UNICODE.encode(locale.getpreferredencoding()))
        out, err = run([f.name])
        self.assertEqual(out, self.TEST_ASCII)

    def test_file_stdin(self):
        cmd = get_cmd()
        p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

        out, err = p.communicate(self.TEST_UNICODE.encode(locale.getpreferredencoding()))
        self.assertEqual(out.decode('ascii'), self.TEST_ASCII)

    def test_commandline(self):
        out = run(['-e', 'sjis', '-c', self.TEST_UNICODE.encode('sjis')])[0]
        self.assertEqual(out, self.TEST_ASCII + '\n')