1from __future__ import unicode_literals
2
3from prompt_toolkit.styles import (
4    Attrs,
5    Style,
6    SwapLightAndDarkStyleTransformation,
7)
8
9
10def test_style_from_dict():
11    style = Style.from_dict({
12        'a': '#ff0000 bold underline italic',
13        'b': 'bg:#00ff00 blink reverse',
14    })
15
16    # Lookup of class:a.
17    expected = Attrs(color='ff0000', bgcolor='', bold=True, underline=True,
18                     italic=True, blink=False, reverse=False, hidden=False)
19    assert style.get_attrs_for_style_str('class:a') == expected
20
21    # Lookup of class:b.
22    expected = Attrs(color='', bgcolor='00ff00', bold=False, underline=False,
23                     italic=False, blink=True, reverse=True, hidden=False)
24    assert style.get_attrs_for_style_str('class:b') == expected
25
26    # Test inline style.
27    expected = Attrs(color='ff0000', bgcolor='', bold=False, underline=False,
28                     italic=False, blink=False, reverse=False, hidden=False)
29    assert style.get_attrs_for_style_str('#ff0000') == expected
30
31    # Combine class name and inline style (Whatever is defined later gets priority.)
32    expected = Attrs(color='00ff00', bgcolor='', bold=True, underline=True,
33                     italic=True, blink=False, reverse=False, hidden=False)
34    assert style.get_attrs_for_style_str('class:a #00ff00') == expected
35
36    expected = Attrs(color='ff0000', bgcolor='', bold=True, underline=True,
37                     italic=True, blink=False, reverse=False, hidden=False)
38    assert style.get_attrs_for_style_str('#00ff00 class:a') == expected
39
40
41def test_class_combinations_1():
42    # In this case, our style has both class 'a' and 'b'.
43    # Given that the style for 'a b' is defined at the end, that one is used.
44    style = Style([
45        ('a', '#0000ff'),
46        ('b', '#00ff00'),
47        ('a b', '#ff0000'),
48    ])
49    expected = Attrs(color='ff0000', bgcolor='', bold=False, underline=False,
50                     italic=False, blink=False, reverse=False, hidden=False)
51    assert style.get_attrs_for_style_str('class:a class:b') == expected
52    assert style.get_attrs_for_style_str('class:a,b') == expected
53    assert style.get_attrs_for_style_str('class:a,b,c') == expected
54
55    # Changing the order shouldn't matter.
56    assert style.get_attrs_for_style_str('class:b class:a') == expected
57    assert style.get_attrs_for_style_str('class:b,a') == expected
58
59
60def test_class_combinations_2():
61    # In this case, our style has both class 'a' and 'b'.
62    # The style that is defined the latest get priority.
63    style = Style([
64        ('a b', '#ff0000'),
65        ('b', '#00ff00'),
66        ('a', '#0000ff'),
67    ])
68    expected = Attrs(color='00ff00', bgcolor='', bold=False, underline=False,
69                     italic=False, blink=False, reverse=False, hidden=False)
70    assert style.get_attrs_for_style_str('class:a class:b') == expected
71    assert style.get_attrs_for_style_str('class:a,b') == expected
72    assert style.get_attrs_for_style_str('class:a,b,c') == expected
73
74    # Defining 'a' latest should give priority to 'a'.
75    expected = Attrs(color='0000ff', bgcolor='', bold=False, underline=False,
76                     italic=False, blink=False, reverse=False, hidden=False)
77    assert style.get_attrs_for_style_str('class:b class:a') == expected
78    assert style.get_attrs_for_style_str('class:b,a') == expected
79
80
81def test_substyles():
82    style = Style([
83        ('a.b', '#ff0000 bold'),
84        ('a', '#0000ff'),
85        ('b', '#00ff00'),
86        ('b.c', '#0000ff italic'),
87    ])
88
89    # Starting with a.*
90    expected = Attrs(color='0000ff', bgcolor='', bold=False, underline=False,
91                     italic=False, blink=False, reverse=False, hidden=False)
92    assert style.get_attrs_for_style_str('class:a') == expected
93
94    expected = Attrs(color='ff0000', bgcolor='', bold=True, underline=False,
95                     italic=False, blink=False, reverse=False, hidden=False)
96    assert style.get_attrs_for_style_str('class:a.b') == expected
97    assert style.get_attrs_for_style_str('class:a.b.c') == expected
98
99    # Starting with b.*
100    expected = Attrs(color='00ff00', bgcolor='', bold=False, underline=False,
101                     italic=False, blink=False, reverse=False, hidden=False)
102    assert style.get_attrs_for_style_str('class:b') == expected
103    assert style.get_attrs_for_style_str('class:b.a') == expected
104
105    expected = Attrs(color='0000ff', bgcolor='', bold=False, underline=False,
106                     italic=True, blink=False, reverse=False, hidden=False)
107    assert style.get_attrs_for_style_str('class:b.c') == expected
108    assert style.get_attrs_for_style_str('class:b.c.d') == expected
109
110
111def test_swap_light_and_dark_style_transformation():
112    transformation = SwapLightAndDarkStyleTransformation()
113
114    # Test with 6 digit hex colors.
115    before = Attrs(color='440000', bgcolor='888844', bold=True, underline=True,
116                   italic=True, blink=False, reverse=False, hidden=False)
117    after = Attrs(color='ffbbbb', bgcolor='bbbb76', bold=True, underline=True,
118                   italic=True, blink=False, reverse=False, hidden=False)
119
120    assert transformation.transform_attrs(before) == after
121
122    # Test with ANSI colors.
123    before = Attrs(color='ansired', bgcolor='ansiblack', bold=True, underline=True,
124                   italic=True, blink=False, reverse=False, hidden=False)
125    after = Attrs(color='ansibrightred', bgcolor='ansiwhite', bold=True, underline=True,
126                   italic=True, blink=False, reverse=False, hidden=False)
127
128    assert transformation.transform_attrs(before) == after
129