File: test_resolve.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (168 lines) | stat: -rw-r--r-- 4,110 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from fractions import Fraction

import pytest

from textual._resolve import resolve, resolve_fraction_unit
from textual.css.scalar import Scalar
from textual.geometry import Size
from textual.widget import Widget


def test_resolve_empty():
    assert resolve([], 10, 1, Size(20, 10), Size(80, 24)) == []


@pytest.mark.parametrize(
    "scalars,total,gutter,result",
    [
        (["10"], 100, 0, [(0, 10)]),
        (
            ["10", "20"],
            100,
            0,
            [(0, 10), (10, 20)],
        ),
        (
            ["10", "20"],
            100,
            1,
            [(0, 10), (11, 20)],
        ),
        (
            ["10", "1fr"],
            100,
            1,
            [(0, 10), (11, 89)],
        ),
        (
            ["1fr", "1fr"],
            100,
            0,
            [(0, 50), (50, 50)],
        ),
        (
            ["3", "1fr", "1fr", "1"],
            100,
            1,
            [(0, 3), (4, 46), (51, 47), (99, 1)],
        ),
    ],
)
def test_resolve(scalars, total, gutter, result):
    assert (
        resolve(
            [Scalar.parse(scalar) for scalar in scalars],
            total,
            gutter,
            Size(40, 20),
            Size(80, 24),
        )
        == result
    )


def test_resolve_fraction_unit():
    """Test resolving fraction units in combination with minimum widths."""
    widget1 = Widget()
    widget2 = Widget()
    widget3 = Widget()

    widget1.styles.width = "1fr"
    widget1.styles.min_width = 20

    widget2.styles.width = "2fr"
    widget2.styles.min_width = 10

    widget3.styles.width = "1fr"

    styles = (widget1.styles, widget2.styles, widget3.styles)

    # Try with width 80.
    # Fraction unit should one fourth of width
    assert resolve_fraction_unit(
        styles,
        Size(80, 24),
        Size(80, 24),
        Fraction(80),
        resolve_dimension="width",
    ) == Fraction(20)

    # Try with width 50
    # First widget is fixed at 20
    # Remaining three widgets have 30 to play with
    # Fraction is 10
    assert resolve_fraction_unit(
        styles,
        Size(80, 24),
        Size(80, 24),
        Fraction(50),
        resolve_dimension="width",
    ) == Fraction(10)

    # Try with width 35
    # First widget fixed at 20
    # Fraction is 5
    assert resolve_fraction_unit(
        styles,
        Size(80, 24),
        Size(80, 24),
        Fraction(35),
        resolve_dimension="width",
    ) == Fraction(5)

    # Try with width 32
    # First widget fixed at 20
    # Second widget is fixed at 10
    # Remaining widget has all the space
    # Fraction is 2
    assert resolve_fraction_unit(
        styles,
        Size(80, 24),
        Size(80, 24),
        Fraction(32),
        resolve_dimension="width",
    ) == Fraction(2)


def test_resolve_fraction_unit_stress_test():
    """Check for zero division errors."""
    # https://github.com/Textualize/textual/issues/2673
    widget = Widget()
    styles = widget.styles
    styles.width = "1fr"

    # We're mainly checking for the absence of zero division errors,
    # which is a reoccurring theme for this code.
    for remaining_space in range(1, 51, 10):
        for max_width in range(1, remaining_space):
            styles.max_width = max_width

            for width in range(1, remaining_space, 2):
                size = Size(width, 24)
                resolved_unit = resolve_fraction_unit(
                    [styles, styles, styles],
                    size,
                    size,
                    Fraction(remaining_space),
                    "width",
                )
                assert resolved_unit <= remaining_space


def test_resolve_issue_2502():
    """Test https://github.com/Textualize/textual/issues/2502"""

    widget = Widget()
    widget.styles.width = "1fr"
    widget.styles.min_width = 11

    assert isinstance(
        resolve_fraction_unit(
            (widget.styles,),
            Size(80, 24),
            Size(80, 24),
            Fraction(10),
            resolve_dimension="width",
        ),
        Fraction,
    )