File: test_clint.py

package info (click to toggle)
python-clint 0.5.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 328 kB
  • sloc: python: 1,815; makefile: 3
file content (87 lines) | stat: -rwxr-xr-x 2,558 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Clint Test Suite."""

import os
import unittest


class ClintTestCase(unittest.TestCase):
    """Clint test cases."""

    def setUp(self):
        import clint


    def tearDown(self):
        pass

class ColoredStringTestCase(unittest.TestCase):
    
    def setUp(self):
        from clint.textui.colored import ColoredString
    
    def tearDown(self):
        pass
    
    def test_split(self):
        from clint.textui.colored import ColoredString
        new_str = ColoredString('red', "hello world")
        output = new_str.split()
        assert output[0].s == "hello"
    
    def test_find(self):
        from clint.textui.colored import ColoredString
        new_str = ColoredString('blue', "hello world")
        output = new_str.find('h')
        self.assertEqual(output, 0)
        
    def test_replace(self):
        from clint.textui.colored import ColoredString
        new_str = ColoredString('green', "hello world")
        output = new_str.replace("world", "universe")
        assert output.s == "hello universe"

    def test_py2_bytes_not_mangled(self):
        from clint.textui.colored import ColoredString
        # On python 2 make sure the same bytes come out as went in
        new_str = ColoredString('RED', '\xe4')
        assert '\xe4' in str(new_str)
        from clint.textui import puts
        puts(new_str)

    def test_clint_force_color_env_var(self):
        from clint.textui.colored import ColoredString
        os.environ['CLINT_FORCE_COLOR'] = "1"
        new_str = ColoredString('RED', 'hello world')
        assert new_str.always_color == True


class TextuiFormatterTestCase(unittest.TestCase):

    def test_max_width(self):

        def _test_n_rows_width(ins, rows, n_rows, max_width):
            ins.assertEqual(len(rows), n_rows)
            for row in rows:
                ins.assertLessEqual(len(row), max_width)

        from clint.textui.formatters import max_width
        from clint.textui import colored
        # normal text
        text = ' '.join(['XXX'] * 3)
        rows = max_width(text, 6).split('\n')
        _test_n_rows_width(self, rows, 3, 6)
        rows = max_width(text, 7).split('\n')
        _test_n_rows_width(self, rows, 2, 7)
        # colored text
        c_text = colored.yellow(text)
        rows = max_width(c_text, 6).split('\n')
        _test_n_rows_width(self, rows, 3, 6)
        rows = max_width(c_text, 7).split('\n')
        _test_n_rows_width(self, rows, 2, 7)


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