|
Packit Service |
21c75c |
# -*- coding: utf-8 -*-
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
# Copyright (C) 2012-2018 Red Hat, Inc.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
# This copyrighted material is made available to anyone wishing to use,
|
|
Packit Service |
21c75c |
# modify, copy, or redistribute it subject to the terms and conditions of
|
|
Packit Service |
21c75c |
# the GNU General Public License v.2, or (at your option) any later version.
|
|
Packit Service |
21c75c |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
Packit Service |
21c75c |
# ANY WARRANTY expressed or implied, including the implied warranties of
|
|
Packit Service |
21c75c |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
Packit Service |
21c75c |
# Public License for more details. You should have received a copy of the
|
|
Packit Service |
21c75c |
# GNU General Public License along with this program; if not, write to the
|
|
Packit Service |
21c75c |
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
Packit Service |
21c75c |
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
|
|
Packit Service |
21c75c |
# source code or documentation are not subject to the GNU General Public
|
|
Packit Service |
21c75c |
# License and may only be used or replicated with the express permission of
|
|
Packit Service |
21c75c |
# Red Hat, Inc.
|
|
Packit Service |
21c75c |
#
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
from __future__ import absolute_import
|
|
Packit Service |
21c75c |
from __future__ import unicode_literals
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
import sys
|
|
Packit Service |
21c75c |
import unittest
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
import dnf.i18n
|
|
Packit Service |
21c75c |
from dnf.pycomp import PY3
|
|
Packit Service |
21c75c |
from dnf.i18n import fill_exact_width, textwrap_fill
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
import tests.support
|
|
Packit Service |
21c75c |
from tests.support import mock
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
UC_TEXT = 'Šířka' # means 'Width' in Czech
|
|
Packit Service |
21c75c |
UC_TEXT_OSERROR = 'Soubor již existuje' # 'File already exists'
|
|
Packit Service |
21c75c |
STR_TEXT_OSERROR = 'Soubor již existuje'
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
@mock.patch('locale.setlocale')
|
|
Packit Service |
21c75c |
class TestLocale(tests.support.TestCase):
|
|
Packit Service |
21c75c |
def test_setup_locale(self, mock_setlocale):
|
|
Packit Service |
21c75c |
dnf.i18n.setup_locale()
|
|
Packit Service |
21c75c |
self.assertTrue(1 <= mock_setlocale.call_count <= 2)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class TestStdout(tests.support.TestCase):
|
|
Packit Service |
21c75c |
def test_setup_stdout(self):
|
|
Packit Service |
21c75c |
# No stdout output can be seen when sys.stdout is patched, debug msgs,
|
|
Packit Service |
21c75c |
# etc. included.
|
|
Packit Service |
21c75c |
with mock.patch('sys.stdout', spec=('write', 'isatty')):
|
|
Packit Service |
21c75c |
retval = dnf.i18n.setup_stdout()
|
|
Packit Service |
21c75c |
self.assertFalse(retval)
|
|
Packit Service |
21c75c |
with mock.patch('sys.stdout') as mock_stdout:
|
|
Packit Service |
21c75c |
mock_stdout.encoding = None
|
|
Packit Service |
21c75c |
retval = dnf.i18n.setup_stdout()
|
|
Packit Service |
21c75c |
self.assertFalse(retval)
|
|
Packit Service |
21c75c |
with mock.patch('sys.stdout') as mock_stdout:
|
|
Packit Service |
21c75c |
mock_stdout.encoding = 'UTF-8'
|
|
Packit Service |
21c75c |
retval = dnf.i18n.setup_stdout()
|
|
Packit Service |
21c75c |
self.assertTrue(retval)
|
|
Packit Service |
21c75c |
with mock.patch('sys.stdout') as mock_stdout:
|
|
Packit Service |
21c75c |
mock_stdout.encoding = 'ISO-8859-2'
|
|
Packit Service |
21c75c |
retval = dnf.i18n.setup_stdout()
|
|
Packit Service |
21c75c |
self.assertFalse(retval)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def test_stream(self):
|
|
Packit Service |
21c75c |
fileobj = dnf.pycomp.StringIO()
|
|
Packit Service |
21c75c |
stream = dnf.i18n.UnicodeStream(fileobj, "ISO-8859-2")
|
|
Packit Service |
21c75c |
stream.write(UC_TEXT)
|
|
Packit Service |
21c75c |
output = fileobj.getvalue()
|
|
Packit Service |
21c75c |
self.assertEqual(output, u'\u0160\xed\u0159ka' if PY3 else b'\xa9\xed\xf8ka')
|
|
Packit Service |
21c75c |
self.assertEqual(len(output), len(UC_TEXT))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class TestInput(tests.support.TestCase):
|
|
Packit Service |
21c75c |
@unittest.skipIf(PY3, "builtin input accepts unicode and bytes")
|
|
Packit Service |
21c75c |
def test_assumption(self):
|
|
Packit Service |
21c75c |
""" Test that raw_input() always fails on a unicode string with accented
|
|
Packit Service |
21c75c |
characters. If this is not the case we might not need i18n.input()
|
|
Packit Service |
21c75c |
as a raw_input() wrapper.
|
|
Packit Service |
21c75c |
"""
|
|
Packit Service |
21c75c |
if sys.stdout.isatty():
|
|
Packit Service |
21c75c |
# Only works when stdout is a terminal (and not captured in some
|
|
Packit Service |
21c75c |
# way, for instance when nosetests is run without the -s switch).
|
|
Packit Service |
21c75c |
self.assertRaises(UnicodeEncodeError, raw_input, UC_TEXT)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class TestConversion(tests.support.TestCase):
|
|
Packit Service |
21c75c |
@mock.patch('dnf.i18n._guess_encoding', return_value='utf-8')
|
|
Packit Service |
21c75c |
def test_ucd(self, _unused):
|
|
Packit Service |
21c75c |
s = UC_TEXT.encode('utf8')
|
|
Packit Service |
21c75c |
# the assumption is this string can't be simply converted back to
|
|
Packit Service |
21c75c |
# unicode:
|
|
Packit Service |
21c75c |
u = dnf.i18n.ucd(s)
|
|
Packit Service |
21c75c |
self.assertEqual(u, UC_TEXT)
|
|
Packit Service |
21c75c |
# test a sample OSError, typically constructed with an error code and a
|
|
Packit Service |
21c75c |
# utf-8 encoded string:
|
|
Packit Service |
21c75c |
obj = OSError(17, 'Soubor již existuje')
|
|
Packit Service |
21c75c |
expected = u"[Errno 17] %s" % UC_TEXT_OSERROR
|
|
Packit Service |
21c75c |
self.assertEqual(dnf.i18n.ucd(obj), expected)
|
|
Packit Service |
21c75c |
# ucd() should return unicode unmodified
|
|
Packit Service |
21c75c |
self.assertEqual(dnf.i18n.ucd(expected), expected)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def test_download_error_unicode(self):
|
|
Packit Service |
21c75c |
err_map = {"e1": ["x", "y"]}
|
|
Packit Service |
21c75c |
err = dnf.exceptions.DownloadError(err_map)
|
|
Packit Service |
21c75c |
self.assertEqual("e1: x\ne1: y", str(err))
|
|
Packit Service |
21c75c |
self.assertEqual("e1: x\ne1: y", dnf.i18n.ucd(err))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
@mock.patch('locale.getpreferredencoding', return_value='ANSI_X3.4-1968')
|
|
Packit Service |
21c75c |
def test_ucd_acii(self, _unused):
|
|
Packit Service |
21c75c |
s = UC_TEXT.encode('utf8')
|
|
Packit Service |
21c75c |
# ascii coding overridden by utf8
|
|
Packit Service |
21c75c |
u = dnf.i18n.ucd(s)
|
|
Packit Service |
21c75c |
self.assertEqual(u, UC_TEXT)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
@mock.patch('dnf.i18n._guess_encoding', return_value='utf-8')
|
|
Packit Service |
21c75c |
def test_ucd_skip(self, _unused):
|
|
Packit Service |
21c75c |
s = UC_TEXT.encode('iso-8859-2')
|
|
Packit Service |
21c75c |
# not decoded chars are skipped
|
|
Packit Service |
21c75c |
u = dnf.i18n.ucd(s)
|
|
Packit Service |
21c75c |
self.assertEqual(u, "ka")
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
class TestFormatedOutput(tests.support.TestCase):
|
|
Packit Service |
21c75c |
def test_fill_exact_width(self):
|
|
Packit Service |
21c75c |
msg = "message"
|
|
Packit Service |
21c75c |
pre = "<"
|
|
Packit Service |
21c75c |
suf = ">"
|
|
Packit Service |
21c75c |
self.assertEqual("%-*.*s" % (5, 10, msg), fill_exact_width(msg, 5, 10))
|
|
Packit Service |
21c75c |
self.assertEqual("重uř ", fill_exact_width("重uř", 5, 10))
|
|
Packit Service |
21c75c |
self.assertEqual("%10.5s" % msg,
|
|
Packit Service |
21c75c |
fill_exact_width(msg, 10, 5, left=False))
|
|
Packit Service |
21c75c |
self.assertEqual("%s%.5s%s" % (pre, msg, suf),
|
|
Packit Service |
21c75c |
fill_exact_width(msg, 0, 5, prefix=pre, suffix=suf))
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def test_exact_width(self):
|
|
Packit Service |
21c75c |
self.assertEqual(dnf.i18n.exact_width("重uř"), 4)
|
|
Packit Service |
21c75c |
|
|
Packit Service |
21c75c |
def test_textwrap_fill(self):
|
|
Packit Service |
21c75c |
msg = "12345 67890"
|
|
Packit Service |
21c75c |
one_line = textwrap_fill(msg, 12)
|
|
Packit Service |
21c75c |
self.assertEqual(one_line, "12345 67890")
|
|
Packit Service |
21c75c |
two_lines = textwrap_fill(msg, 7, subsequent_indent=">>")
|
|
Packit Service |
21c75c |
self.assertEqual(two_lines,
|
|
Packit Service |
21c75c |
"12345\n>>67890")
|
|
Packit Service |
21c75c |
asian_msg = "重重 uř"
|
|
Packit Service |
21c75c |
self.assertEqual(textwrap_fill(asian_msg, 7), asian_msg)
|
|
Packit Service |
21c75c |
asian_two_lines = textwrap_fill("重重\nuř", 5, subsequent_indent=">>")
|
|
Packit Service |
21c75c |
self.assertEqual(asian_two_lines, "重重\n>>uř")
|