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
|
# vim:ts=4 sw=4 expandtab softtabstop=4
import os
import locale
import unittest
import subprocess
import sys
import tempfile
import re
here = os.path.dirname(__file__)
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'), p.returncode
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, rc = run(['-e', 'utf8', f.name])
# Text after : ... can differ between Python versions
self.assertRegex(err, '^Unable to decode input line ')
self.assertEqual(rc, 1)
def test_file_specified_encoding(self):
f = temp(self.TEST_UNICODE.encode('sjis'))
out, err, rc = run(['-e', 'sjis', f.name])
self.assertEqual(out, self.TEST_ASCII)
self.assertEqual(rc, 0)
def test_file_default_encoding(self):
f = temp(self.TEST_UNICODE.encode(locale.getpreferredencoding()))
out, err, rc = run([f.name])
self.assertEqual(out, self.TEST_ASCII)
self.assertEqual(rc, 0)
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, err, rc = run(['-e', 'sjis', '-c', self.TEST_UNICODE.encode('sjis')])
self.assertEqual(out, self.TEST_ASCII + '\n')
self.assertEqual(rc, 0)
|