File: test_styling.py

package info (click to toggle)
python-cloup 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: python: 5,371; makefile: 120
file content (63 lines) | stat: -rw-r--r-- 1,774 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
import inspect

import click
import pytest

from cloup.styling import Color, HelpTheme, Style


def test_help_theme_copy_with():
    s1, s2 = Style(), Style()
    r1, r2 = Style(), Style()
    theme = HelpTheme(heading=s1, col1=s2).with_(col1=r1, col2=r2)
    assert theme == HelpTheme(heading=s1, col1=r1, col2=r2)


def test_help_theme_copy_with_takes_the_same_parameters_of_constructor():
    def get_param_names(func):
        params = inspect.signature(func).parameters
        return list(params.keys())

    constructor_params = get_param_names(HelpTheme)
    method_params = get_param_names(HelpTheme.with_)[1:]  # skip self
    assert method_params == constructor_params


def test_help_theme_default_themes():
    assert isinstance(HelpTheme.dark(), HelpTheme)
    assert isinstance(HelpTheme.light(), HelpTheme)


def test_style():
    text = 'hi there'
    kwargs = dict(fg=Color.green, bold=True, blink=True)
    assert Style(**kwargs)(text) == click.style(text, **kwargs)


def test_unsupported_style_args_are_ignored_in_click_7():
    Style(overline=True, italic=True, strikethrough=True)


def test_color_class():
    # Check values of some attributes
    assert Color.red == 'red'
    assert Color.bright_blue == 'bright_blue'

    # Check it's not instantiable
    with pytest.raises(Exception, match="it's not instantiable"):
        Color()

    # Check only __dunder__ fields are settable
    Color.__annotations__ = "whatever"
    with pytest.raises(Exception, match="you can't set attributes on this class"):
        Color.red = 'blue'

    # Test __contains__
    assert 'red' in Color
    assert 'pippo' not in Color

    # Test Color.asdict()
    d = Color.asdict()
    for k, v in d.items():
        assert k in Color
        assert Color[k] == v