File: test_spatial_map.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 (64 lines) | stat: -rw-r--r-- 1,632 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
import pytest

from textual._spatial_map import SpatialMap
from textual.geometry import Offset, Region


@pytest.mark.parametrize(
    "region,grid",
    [
        (
            Region(0, 0, 10, 10),
            [
                (0, 0),
            ],
        ),
        (
            Region(10, 10, 10, 10),
            [
                (1, 1),
            ],
        ),
        (
            Region(0, 0, 11, 11),
            [(0, 0), (0, 1), (1, 0), (1, 1)],
        ),
        (
            Region(5, 5, 15, 3),
            [(0, 0), (1, 0)],
        ),
        (
            Region(5, 5, 2, 15),
            [(0, 0), (0, 1)],
        ),
    ],
)
def test_region_to_grid(region, grid):
    spatial_map = SpatialMap(10, 10)

    assert list(spatial_map._region_to_grid_coordinates(region)) == grid


def test_get_values_in_region() -> None:
    spatial_map: SpatialMap[str] = SpatialMap(20, 10)

    spatial_map.insert(
        [
            (Region(10, 5, 5, 5), Offset(), False, False, "foo"),
            (Region(5, 20, 5, 5), Offset(), False, False, "bar"),
            (Region(0, 0, 40, 1), Offset(), True, False, "title"),
        ]
    )

    assert spatial_map.get_values_in_region(Region(0, 0, 10, 5)) == [
        "title",
        "foo",
    ]
    assert spatial_map.get_values_in_region(Region(0, 1, 10, 5)) == ["title", "foo"]
    assert spatial_map.get_values_in_region(Region(0, 10, 10, 5)) == ["title"]
    assert spatial_map.get_values_in_region(Region(0, 20, 10, 5)) == ["title", "bar"]
    assert spatial_map.get_values_in_region(Region(5, 5, 50, 50)) == [
        "title",
        "foo",
        "bar",
    ]