File: test_geometry.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 (582 lines) | stat: -rw-r--r-- 16,534 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
from typing import Literal

import pytest

from textual.geometry import Offset, Region, Size, Spacing, clamp


def test_dimensions_region():
    assert Size(30, 40).region == Region(0, 0, 30, 40)


def test_dimensions_contains():
    assert Size(10, 10).contains(5, 5)
    assert Size(10, 10).contains(9, 9)
    assert Size(10, 10).contains(0, 0)
    assert not Size(10, 10).contains(10, 9)
    assert not Size(10, 10).contains(9, 10)
    assert not Size(10, 10).contains(-1, 0)
    assert not Size(10, 10).contains(0, -1)


def test_dimensions_contains_point():
    assert Size(10, 10).contains_point(Offset(5, 5))
    assert Size(10, 10).contains_point(Offset(9, 9))
    assert Size(10, 10).contains_point(Offset(0, 0))
    assert not Size(10, 10).contains_point(Offset(10, 9))
    assert not Size(10, 10).contains_point(Offset(9, 10))
    assert not Size(10, 10).contains_point(Offset(-1, 0))
    assert not Size(10, 10).contains_point(Offset(0, -1))


def test_dimensions_contains_special():
    with pytest.raises(TypeError):
        (1, 2, 3) in Size(10, 10)

    assert (5, 5) in Size(10, 10)
    assert (9, 9) in Size(10, 10)
    assert (0, 0) in Size(10, 10)
    assert (10, 9) not in Size(10, 10)
    assert (9, 10) not in Size(10, 10)
    assert (-1, 0) not in Size(10, 10)
    assert (0, -1) not in Size(10, 10)


def test_dimensions_bool():
    assert Size(1, 1)
    assert Size(3, 4)
    assert not Size(0, 1)
    assert not Size(1, 0)


def test_dimensions_area():
    assert Size(0, 0).area == 0
    assert Size(1, 0).area == 0
    assert Size(1, 1).area == 1
    assert Size(4, 5).area == 20


def test_clamp():
    assert clamp(5, 0, 10) == 5
    assert clamp(-1, 0, 10) == 0
    assert clamp(11, 0, 10) == 10
    assert clamp(0, 0, 10) == 0
    assert clamp(10, 0, 10) == 10
    assert clamp(5, 10, 0) == 5

    # range in reverse order
    assert clamp(5, 10, 0) == 5
    assert clamp(-1, 10, 0) == 0
    assert clamp(11, 10, 0) == 10
    assert clamp(0, 10, 0) == 0
    assert clamp(10, 10, 0) == 10
    assert clamp(5, 0, 10) == 5


def test_offset_bool():
    assert Offset(1, 0)
    assert Offset(0, 1)
    assert Offset(0, -1)
    assert not Offset(0, 0)


def test_offset_transpose():
    assert Offset(1, 2).transpose == (2, 1)
    assert Offset(5, 10).transpose == (10, 5)


def test_offset_is_origin():
    assert Offset(0, 0).is_origin
    assert not Offset(1, 0).is_origin


def test_clamped():
    assert Offset(-10, 0).clamped == Offset(0, 0)
    assert Offset(-10, -5).clamped == Offset(0, 0)
    assert Offset(5, -5).clamped == Offset(5, 0)
    assert Offset(5, 10).clamped == Offset(5, 10)


def test_offset_add():
    assert Offset(1, 1) + Offset(2, 2) == Offset(3, 3)
    assert Offset(1, 2) + Offset(3, 4) == Offset(4, 6)
    with pytest.raises(TypeError):
        Offset(1, 1) + "foo"


def test_offset_sub():
    assert Offset(1, 1) - Offset(2, 2) == Offset(-1, -1)
    assert Offset(3, 4) - Offset(2, 1) == Offset(1, 3)
    with pytest.raises(TypeError):
        Offset(1, 1) - "foo"


def test_offset_neg():
    assert Offset(0, 0) == Offset(0, 0)
    assert -Offset(2, -3) == Offset(-2, 3)


def test_offset_mul():
    assert Offset(2, 1) * 2 == Offset(4, 2)
    assert Offset(2, 1) * -2 == Offset(-4, -2)
    assert Offset(2, 1) * 0 == Offset(0, 0)
    with pytest.raises(TypeError):
        Offset(10, 20) * "foo"


def test_offset_blend():
    assert Offset(1, 2).blend(Offset(3, 4), 0) == Offset(1, 2)
    assert Offset(1, 2).blend(Offset(3, 4), 1) == Offset(3, 4)
    assert Offset(1, 2).blend(Offset(3, 4), 0.5) == Offset(2, 3)


def test_offset_get_distance_to():
    assert Offset(20, 30).get_distance_to(Offset(20, 30)) == 0
    assert Offset(0, 0).get_distance_to(Offset(1, 0)) == 1.0
    assert Offset(2, 1).get_distance_to(Offset(5, 5)) == 5.0


def test_region_null():
    assert Region() == Region(0, 0, 0, 0)
    assert not Region()


def test_region_from_union():
    with pytest.raises(ValueError):
        Region.from_union([])
    regions = [
        Region(10, 20, 30, 40),
        Region(15, 25, 5, 5),
        Region(30, 25, 20, 10),
    ]
    assert Region.from_union(regions) == Region(10, 20, 40, 40)


def test_region_from_offset():
    assert Region.from_offset(Offset(3, 4), (5, 6)) == Region(3, 4, 5, 6)


@pytest.mark.parametrize(
    "window,region,scroll",
    [
        (Region(0, 0, 200, 100), Region(0, 0, 200, 100), Offset(0, 0)),
        (Region(0, 0, 200, 100), Region(0, -100, 10, 10), Offset(0, -100)),
        (Region(10, 15, 20, 10), Region(0, 0, 50, 50), Offset(-10, -15)),
    ],
)
def test_get_scroll_to_visible(window, region, scroll):
    assert Region.get_scroll_to_visible(window, region) == scroll
    assert region.overlaps(window + scroll)


def test_region_area():
    assert Region(3, 4, 0, 0).area == 0
    assert Region(3, 4, 5, 6).area == 30


def test_region_size():
    assert isinstance(Region(3, 4, 5, 6).size, Size)
    assert Region(3, 4, 5, 6).size == Size(5, 6)


def test_region_origin():
    assert Region(1, 2, 3, 4).offset == Offset(1, 2)


def test_region_bottom_left():
    assert Region(1, 2, 3, 4).bottom_left == Offset(1, 6)


def test_region_top_right():
    assert Region(1, 2, 3, 4).top_right == Offset(4, 2)


def test_region_bottom_right():
    assert Region(1, 2, 3, 4).bottom_right == Offset(4, 6)
    assert Region(1, 2, 3, 4).bottom_right_inclusive == Offset(3, 5)


def test_region_add():
    assert Region(1, 2, 3, 4) + (10, 20) == Region(11, 22, 3, 4)
    with pytest.raises(TypeError):
        Region(1, 2, 3, 4) + "foo"


def test_region_sub():
    assert Region(11, 22, 3, 4) - (10, 20) == Region(1, 2, 3, 4)
    with pytest.raises(TypeError):
        Region(1, 2, 3, 4) - "foo"


def test_region_at_offset():
    assert Region(10, 10, 30, 40).at_offset((0, 0)) == Region(0, 0, 30, 40)
    assert Region(10, 10, 30, 40).at_offset((-15, 30)) == Region(-15, 30, 30, 40)


def test_crop_size():
    assert Region(10, 20, 100, 200).crop_size((50, 40)) == Region(10, 20, 50, 40)
    assert Region(10, 20, 100, 200).crop_size((500, 40)) == Region(10, 20, 100, 40)


def test_region_overlaps():
    assert Region(10, 10, 30, 20).overlaps(Region(0, 0, 20, 20))
    assert not Region(10, 10, 5, 5).overlaps(Region(15, 15, 20, 20))

    assert not Region(10, 10, 5, 5).overlaps(Region(0, 0, 50, 10))
    assert Region(10, 10, 5, 5).overlaps(Region(0, 0, 50, 11))
    assert not Region(10, 10, 5, 5).overlaps(Region(0, 15, 50, 10))
    assert Region(10, 10, 5, 5).overlaps(Region(0, 14, 50, 10))


def test_region_contains():
    assert Region(10, 10, 20, 30).contains(10, 10)
    assert Region(10, 10, 20, 30).contains(29, 39)
    assert not Region(10, 10, 20, 30).contains(30, 40)


def test_region_contains_point():
    assert Region(10, 10, 20, 30).contains_point((10, 10))
    assert Region(10, 10, 20, 30).contains_point((29, 39))
    assert not Region(10, 10, 20, 30).contains_point((30, 40))
    with pytest.raises(TypeError):
        Region(10, 10, 20, 30).contains_point((1, 2, 3))


def test_region_contains_region():
    assert Region(10, 10, 20, 30).contains_region(Region(10, 10, 5, 5))
    assert not Region(10, 10, 20, 30).contains_region(Region(10, 9, 5, 5))
    assert not Region(10, 10, 20, 30).contains_region(Region(9, 10, 5, 5))
    assert Region(10, 10, 20, 30).contains_region(Region(10, 10, 20, 30))
    assert not Region(10, 10, 20, 30).contains_region(Region(10, 10, 21, 30))
    assert not Region(10, 10, 20, 30).contains_region(Region(10, 10, 20, 31))


def test_region_translate():
    assert Region(1, 2, 3, 4).translate((10, 20)) == Region(11, 22, 3, 4)
    assert Region(1, 2, 3, 4).translate((0, 20)) == Region(1, 22, 3, 4)


def test_region_contains_special():
    assert (10, 10) in Region(10, 10, 20, 30)
    assert (9, 10) not in Region(10, 10, 20, 30)
    assert Region(10, 10, 5, 5) in Region(10, 10, 20, 30)
    assert Region(5, 5, 5, 5) not in Region(10, 10, 20, 30)
    assert "foo" not in Region(0, 0, 10, 10)


def test_clip():
    assert Region(10, 10, 20, 30).clip(20, 25) == Region(10, 10, 10, 15)


def test_region_shrink():
    margin = Spacing(top=1, right=2, bottom=3, left=4)
    region = Region(x=10, y=10, width=50, height=50)
    assert region.shrink(margin) == Region(x=14, y=11, width=44, height=46)


def test_region_grow():
    margin = Spacing(top=1, right=2, bottom=3, left=4)
    region = Region(x=10, y=10, width=50, height=50)
    assert region.grow(margin) == Region(x=6, y=9, width=56, height=54)


def test_region_intersection():
    assert Region(0, 0, 100, 50).intersection(Region(10, 10, 10, 10)) == Region(
        10, 10, 10, 10
    )
    assert Region(10, 10, 30, 20).intersection(Region(20, 15, 60, 40)) == Region(
        20, 15, 20, 15
    )

    assert not Region(10, 10, 20, 30).intersection(Region(50, 50, 100, 200))


def test_region_union():
    assert Region(5, 5, 10, 10).union(Region(20, 30, 10, 5)) == Region(5, 5, 25, 30)


def test_size_add():
    assert Size(5, 10) + Size(2, 3) == Size(7, 13)
    with pytest.raises(TypeError):
        Size(1, 2) + "foo"


def test_size_sub():
    assert Size(5, 10) - Size(2, 3) == Size(3, 7)
    with pytest.raises(TypeError):
        Size(1, 2) - "foo"


def test_size_line_range():
    assert Size(20, 0).line_range == range(0)
    assert Size(0, 20).line_range == range(20)


def test_region_x_extents():
    assert Region(5, 10, 20, 30).column_span == (5, 25)


def test_region_y_extents():
    assert Region(5, 10, 20, 30).line_span == (10, 40)


def test_region_x_max():
    assert Region(5, 10, 20, 30).right == 25


def test_region_y_max():
    assert Region(5, 10, 20, 30).bottom == 40


def test_region_columns_range():
    assert Region(5, 10, 20, 30).column_range == range(5, 25)


def test_region_lines_range():
    assert Region(5, 10, 20, 30).line_range == range(10, 40)


def test_region_reset_offset():
    assert Region(5, 10, 20, 30).reset_offset == Region(0, 0, 20, 30)


def test_region_expand():
    assert Region(50, 10, 10, 5).expand((2, 3)) == Region(48, 7, 14, 11)


def test_spacing_bool():
    assert Spacing(1, 0, 0, 0)
    assert Spacing(0, 1, 0, 0)
    assert Spacing(0, 1, 0, 0)
    assert Spacing(0, 0, 1, 0)
    assert Spacing(0, 0, 0, 1)
    assert not Spacing(0, 0, 0, 0)


def test_spacing_width():
    assert Spacing(2, 3, 4, 5).width == 8


def test_spacing_height():
    assert Spacing(2, 3, 4, 5).height == 6


def test_spacing_top_left():
    assert Spacing(2, 3, 4, 5).top_left == (5, 2)


def test_spacing_bottom_right():
    assert Spacing(2, 3, 4, 5).bottom_right == (3, 4)


def test_spacing_totals():
    assert Spacing(2, 3, 4, 5).totals == (8, 6)


def test_spacing_css():
    assert Spacing(1, 1, 1, 1).css == "1"
    assert Spacing(1, 2, 1, 2).css == "1 2"
    assert Spacing(1, 2, 3, 4).css == "1 2 3 4"


def test_spacing_unpack():
    assert Spacing.unpack(1) == Spacing(1, 1, 1, 1)
    assert Spacing.unpack((1,)) == Spacing(1, 1, 1, 1)
    assert Spacing.unpack((1, 2)) == Spacing(1, 2, 1, 2)
    assert Spacing.unpack((1, 2, 3, 4)) == Spacing(1, 2, 3, 4)

    with pytest.raises(ValueError):
        assert Spacing.unpack(()) == Spacing(1, 2, 1, 2)

    with pytest.raises(ValueError):
        assert Spacing.unpack((1, 2, 3)) == Spacing(1, 2, 1, 2)

    with pytest.raises(ValueError):
        assert Spacing.unpack((1, 2, 3, 4, 5)) == Spacing(1, 2, 1, 2)


def test_spacing_add():
    assert Spacing(1, 2, 3, 4) + Spacing(5, 6, 7, 8) == Spacing(6, 8, 10, 12)

    with pytest.raises(TypeError):
        Spacing(1, 2, 3, 4) + "foo"


def test_spacing_sub():
    assert Spacing(1, 2, 3, 4) - Spacing(5, 6, 7, 8) == Spacing(-4, -4, -4, -4)

    with pytest.raises(TypeError):
        Spacing(1, 2, 3, 4) - "foo"


def test_spacing_convenience_constructors():
    assert Spacing.vertical(2) == Spacing(2, 0, 2, 0)
    assert Spacing.horizontal(2) == Spacing(0, 2, 0, 2)
    assert Spacing.all(2) == Spacing(2, 2, 2, 2)


def test_split():
    assert Region(10, 5, 22, 15).split(10, 5) == (
        Region(10, 5, 10, 5),
        Region(20, 5, 12, 5),
        Region(10, 10, 10, 10),
        Region(20, 10, 12, 10),
    )


def test_split_negative():
    assert Region(10, 5, 22, 15).split(-1, -1) == (
        Region(10, 5, 21, 14),
        Region(31, 5, 1, 14),
        Region(10, 19, 21, 1),
        Region(31, 19, 1, 1),
    )


def test_split_vertical():
    assert Region(10, 5, 22, 15).split_vertical(10) == (
        Region(10, 5, 10, 15),
        Region(20, 5, 12, 15),
    )


def test_split_vertical_negative():
    assert Region(10, 5, 22, 15).split_vertical(-1) == (
        Region(10, 5, 21, 15),
        Region(31, 5, 1, 15),
    )


def test_split_horizontal():
    assert Region(10, 5, 22, 15).split_horizontal(5) == (
        Region(10, 5, 22, 5),
        Region(10, 10, 22, 10),
    )


def test_split_horizontal_negative():
    assert Region(10, 5, 22, 15).split_horizontal(-1) == (
        Region(10, 5, 22, 14),
        Region(10, 19, 22, 1),
    )


def test_translate_inside():
    # Needs to be moved up
    assert Region(10, 20, 10, 20).translate_inside(Region(0, 0, 30, 25)) == Region(
        10, 5, 10, 20
    )

    # Already inside
    assert Region(10, 10, 20, 5).translate_inside(Region(0, 0, 100, 100)) == Region(
        10, 10, 20, 5
    )


def test_inflect():
    assert Region(0, 0, 1, 1).inflect() == Region(1, 1, 1, 1)
    assert Region(0, 0, 1, 1).inflect(margin=Spacing.unpack(1)) == Region(2, 2, 1, 1)

    # Default inflect positive
    assert Region(10, 10, 30, 20).inflect(margin=Spacing(2, 2, 2, 2)) == Region(
        42, 32, 30, 20
    )

    # Inflect y axis negative
    assert Region(10, 10, 30, 20).inflect(
        y_axis=-1, margin=Spacing(2, 2, 2, 2)
    ) == Region(42, -12, 30, 20)

    # Inflect y axis negative
    assert Region(10, 10, 30, 20).inflect(
        x_axis=-1, margin=Spacing(2, 2, 2, 2)
    ) == Region(-22, 32, 30, 20)


def test_size_with_height():
    """Test Size.with_height"""
    assert Size(1, 2).with_height(10) == Size(1, 10)


def test_size_with_width():
    """Test Size.with_width"""
    assert Size(1, 2).with_width(10) == Size(10, 2)


def test_offset_clamp():
    assert Offset(1, 2).clamp(3, 3) == Offset(1, 2)
    assert Offset(3, 2).clamp(3, 3) == Offset(2, 2)
    assert Offset(-3, 2).clamp(3, 3) == Offset(0, 2)
    assert Offset(5, 4).clamp(3, 3) == Offset(2, 2)


def test_size_clamp_offset():
    assert Size(3, 3).clamp_offset(Offset(1, 2)) == Offset(1, 2)
    assert Size(3, 3).clamp_offset(Offset(3, 2)) == Offset(2, 2)
    assert Size(3, 3).clamp_offset(Offset(-3, 2)) == Offset(0, 2)
    assert Size(3, 3).clamp_offset(Offset(5, 4)) == Offset(2, 2)


@pytest.mark.parametrize(
    ("region1", "region2", "expected"),
    [
        (Region(0, 0, 100, 80), Region(0, 0, 100, 80), Spacing(0, 0, 0, 0)),
        (Region(0, 0, 100, 80), Region(10, 10, 10, 10), Spacing(10, 80, 60, 10)),
    ],
)
def test_get_spacing_between(region1: Region, region2: Region, expected: Spacing):
    spacing = region1.get_spacing_between(region2)
    assert spacing == expected
    assert region1.shrink(spacing) == region2


@pytest.mark.parametrize(
    "constrain_x,constrain_y,margin,region,container,expected",
    [
        # A null-op
        (
            "none",
            "none",
            Spacing.unpack(0),
            Region(0, 0, 10, 10),
            Region(0, 0, 100, 100),
            Region(0, 0, 10, 10),
        ),
        # Negative offset gets moved to 0, 0 + margin
        (
            "inside",
            "inside",
            Spacing.unpack(1),
            Region(-5, -5, 10, 10),
            Region(0, 0, 100, 100),
            Region(1, 1, 10, 10),
        ),
        # Overlapping region gets moved in, with offset
        (
            "inside",
            "inside",
            Spacing.unpack(1),
            Region(95, 95, 10, 10),
            Region(0, 0, 100, 100),
            Region(89, 89, 10, 10),
        ),
        # X coordinate moved inside, region reflected around it's Y axis
        (
            "inside",
            "inflect",
            Spacing.unpack(1),
            Region(-5, -5, 10, 10),
            Region(0, 0, 100, 100),
            Region(1, 6, 10, 10),
        ),
    ],
)
def test_constrain(
    constrain_x: Literal["none", "inside", "inflect"],
    constrain_y: Literal["none", "inside", "inflect"],
    margin: Spacing,
    region: Region,
    container: Region,
    expected: Region,
) -> None:
    assert region.constrain(constrain_x, constrain_y, margin, container) == expected