File: test_module.py

package info (click to toggle)
python-colorful 0.5.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,124 kB
  • sloc: python: 1,343; sh: 8; makefile: 3
file content (106 lines) | stat: -rw-r--r-- 3,245 bytes parent folder | download
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-

"""
    colorful
    ~~~~~~~~

    Terminal string styling done right, in Python.

    :copyright: (c) 2017 by Timo Furrer <tuxtimo@gmail.com>
    :license: MIT, see LICENSE for more details.
"""

import os

import pytest

# do not overwrite module
os.environ['COLORFUL_NO_MODULE_OVERWRITE'] = '1'

import colorful  # noqa
import colorful.terminal as terminal  # noqa

# replace colorful name with ColorfulModule instance
ColorfulError = colorful.core.ColorfulError
colorful = colorful.ColorfulModule(
    colorful.Colorful(colormode=terminal.ANSI_8_COLORS), 'colorful')


def test_color_method_resolution():
    """
    Test the color method resolution on the package level
    """
    #
    assert str(colorful.black) == '\033[30m'


def test_setup_contextmanager():
    """
    Test the package level setup context manager
    """
    with colorful.with_setup(colormode=terminal.ANSI_16_COLORS,
                             colorpalette={'testColor': (0, 0, 0)}, extend_colors=False) as c:
        assert str(c.testColor) == '\033[30m'

        with pytest.raises(ColorfulError) as exc:
            c.black('Black does not exist anymore')
        assert str(exc.value).startswith('the color "black" is unknown.')

    with pytest.raises(ColorfulError) as exc:
        colorful.testColor('The testColor only existed in the with block above.')
    assert str(exc.value).startswith('the color "testColor" is unknown.')


@pytest.mark.parametrize('ctxmgr_name, colorname, expected, expected_8', [
    ('with_8_ansi_colors', 'black', '\033[30m', '\033[30m'),
    ('with_16_ansi_colors', 'black', '\033[30m', '\033[30m'),
    ('with_256_ansi_colors', 'black', '\033[38;5;16m', '\033[30m'),
    ('with_true_colors', 'black', '\033[38;2;0;0;0m', '\033[30m')
])
def test_set_color_mode_methods(ctxmgr_name, colorname, expected, expected_8):
    """
    Test changing the color mode in a with-block
    """
    contextmanager = getattr(colorful, ctxmgr_name)

    with contextmanager() as c:
        assert str(getattr(c, colorname)) == expected

    assert str(getattr(colorful, colorname)) == expected_8


def test_change_color_palette():
    """
    Test changing the color palette in a with-block
    """
    NEW_COLOR_PALETTE = {
        'testColor': (0, 0, 0)
    }

    # updating existing color palette
    with colorful.with_updated_palette(NEW_COLOR_PALETTE) as c:
        # old and new colors should exist
        assert str(c.testColor) == '\033[30m'
        assert str(c.black) == '\033[30m'

    # set color palette and overwrite the old one
    with colorful.with_palette(NEW_COLOR_PALETTE) as c:
        assert str(c.testColor) == '\033[30m'

        with pytest.raises(ColorfulError) as exc:
            c.black('the color "black" was overwritten by the new palette')

        expected = ('the color "black" is unknown. '
                    'Use a color in your color palette (by default: X11 rgb.txt)')
        assert str(exc.value) == expected


def test_use_styles():
    """
    Test using a predefined style in a with-block
    """
    with colorful.with_style('solarized') as c:
        c.use_true_colors()
        assert str(c.red) == '\033[38;2;220;50;47m'

    assert str(colorful.red) == '\033[31m'