test_astring.py 5.6 KB
Newer Older
1
import sys
2 3 4 5 6 7 8
import unittest

from avocado.utils import astring


class AstringTest(unittest.TestCase):

9
    def test_tabular_output(self):
10 11
        matrix = [('foo', 'bar'), ('/bin/bar/sbrubles',
                                   '/home/myuser/sbrubles')]
12
        self.assertEqual(astring.tabular_output(matrix),
13 14
                         ('foo               bar\n'
                          '/bin/bar/sbrubles /home/myuser/sbrubles'))
15
        header = ['id', 'path']
16
        self.assertEqual(astring.tabular_output(matrix, header),
17 18 19
                         ('id                path\n'
                          'foo               bar\n'
                          '/bin/bar/sbrubles /home/myuser/sbrubles'))
20

21
    def test_tabular_with_console_codes(self):
22
        matrix = [("a", "an", "dog", "word", "last"),
23
                  ("\x1b[94ma",             # {BLUE}a
24
                   "\033[0man",             # {END}an
25
                   "cc\033[91mc",   # cc{RED}c
26
                   # {RED}d{GREEN}d{BLUE}d{GRAY}d{END}
27 28 29 30 31
                   "\033[91md\033[92md\033[94md\033[90md\033[0m",
                   "last")]
        header = ['0', '1', '2', '3', '4']
        self.assertEqual(astring.tabular_output(matrix, header),
                         "0 1  2   3    4\n"
32 33 34
                         "a an dog word last\n"
                         "a an ccc "
                         "dddd last")
35

36
    def test_tabular_output_different_no_cols(self):
37 38 39 40 41 42
        matrix = [[], [1], [2, 2], [333, 333, 333], [4, 4, 4, 4444]]
        self.assertEqual(astring.tabular_output(matrix),
                         "1\n"
                         "2   2\n"
                         "333 333 333\n"
                         "4   4   4   4444")
43

44 45 46 47 48
    # This could be a skip based on the Python version, but this is more
    # specific to the exact reason why it does/doesn't make sense to run it
    @unittest.skipUnless(sys.getdefaultencoding() == 'ascii',
                         "Test verifies conversion behavior of between ascii "
                         "and utf-8 only")
49
    def test_unicode_tabular(self):
50 51 52 53 54 55 56 57
        """
        Verifies tabular can handle utf-8 chars properly

        It tries valid encoded utf-8 string as well as unicode ones of
        various lengths and verifies calculates the right length and reports
        the correct results. (the string_safe_encode function is in use here)
        """

58
        matrix = [("\xd0\xb0\xd0\xb2\xd0\xbe\xd0\xba\xd0\xb0\xd0\xb4\xff",
59
                   123),
60
                  (u'\u0430\u0432\u043e\u043a\u0430\u0434\xff', 123),
61
                  ("avok\xc3\xa1do", 123),
62
                  ("a\u0430", 123)]
63
        str_matrix = ("\xd0\xb0\xd0\xb2\xd0\xbe\xd0\xba\xd0\xb0\xd0\xb4"
64
                      "\xef\xbf\xbd 123\n"
65
                      "\xd0\xb0\xd0\xb2\xd0\xbe\xd0\xba\xd0\xb0\xd0\xb4"
66
                      "\xc3\xbf 123\n"
67
                      "avok\xc3\xa1do 123\n"
68
                      "a\u0430 123")
69 70
        self.assertEqual(astring.tabular_output(matrix), str_matrix)

71
    def test_safe_path(self):
L
Lukáš Doktor 已提交
72
        self.assertEqual(astring.string_to_safe_path('a<>:"/\\|\\?*b'),
73 74 75
                         "a__________b")
        self.assertEqual(astring.string_to_safe_path('..'), "_.")
        self.assertEqual(len(astring.string_to_safe_path(" " * 300)), 255)
76 77 78
        avocado = u'\u0430\u0432\u043e\u043a\u0430\u0434\xff<>'
        self.assertEqual(astring.string_to_safe_path(avocado),
                         "%s__" % avocado[:-2])
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    def test_is_bytes(self):
        """
        Verifies what bytes means, basically that they are the same
        thing accross Python 2 and 3 and can be decoded into "text"
        """
        binary = b''
        text = u''
        self.assertTrue(astring.is_bytes(binary))
        self.assertFalse(astring.is_bytes(text))
        self.assertTrue(hasattr(binary, 'decode'))
        self.assertTrue(astring.is_text(binary.decode()))
        # on Python 2, each str member is also a single byte char
        if sys.version_info[0] < 3:
            self.assertTrue(astring.is_bytes(str('')))
        else:
            self.assertFalse(astring.is_bytes(str('')))

    def test_is_text(self):
        """
        Verifies what text means, basically that they can represent
        extended set of characters and can be encoded into "bytes"
        """
        binary = b''
        text = u''
        self.assertTrue(astring.is_text(text))
        self.assertFalse(astring.is_text(binary))
        self.assertTrue(hasattr(text, 'encode'))
        self.assertTrue(astring.is_bytes(text.encode()))

109 110 111 112 113 114 115 116 117 118
    def test_to_text_is_text(self):
        self.assertTrue(astring.is_text(astring.to_text(b'')))
        self.assertTrue(astring.is_text(astring.to_text('')))
        self.assertTrue(astring.is_text(astring.to_text(u'')))

    def test_to_text_decode_is_text(self):
        self.assertTrue(astring.is_text(astring.to_text(b'', 'ascii')))
        self.assertTrue(astring.is_text(astring.to_text('', 'ascii')))
        self.assertTrue(astring.is_text(astring.to_text(u'', 'ascii')))

119
    def test_to_text(self):
120 121 122 123
        text_1 = astring.to_text(b'\xc3\xa1', 'utf-8')
        text_2 = astring.to_text(u'\u00e1', 'utf-8')
        self.assertTrue(astring.is_text(text_1))
        self.assertEqual(text_1, text_2)
124 125 126 127 128
        self.assertEqual(astring.to_text(Exception(u'\u00e1')),
                         u"\xe1")
        # For tuple, dict and others astring.to_text is equivalent of str()
        # because on py3 it's unicode and on py2 it uses __repr__ (is encoded)
        self.assertEqual(astring.to_text({u'\xe1': 1}), str({u'\xe1': 1}))
129

L
Lukáš Doktor 已提交
130

131 132
if __name__ == '__main__':
    unittest.main()