File: test_bounding_box.py

package info (click to toggle)
python-redbaron 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 772 kB
  • sloc: python: 6,650; makefile: 145; sh: 28
file content (149 lines) | stat: -rw-r--r-- 5,641 bytes parent folder | download
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
#!/usr/bin/python
# -*- coding:Utf-8 -*-

""" Tests the bounding_box feature """

import pytest
# pylint: disable=redefined-outer-name
from redbaron import RedBaron


def red():
    return RedBaron("""\
@deco
def a(c, d):
    b = c + d
""")


@pytest.fixture(name="red")
def red_fixture():
    return red()


fst = red()
bounding_boxes = [
    (((1, 1),  (4, 0)),  ((1, 1), (4, 0)), fst),
    (((1, 1),  (4, 0)),  ((1, 1), (4, 0)), fst.def_),
    (((1, 1),  (2, 0)),  ((1, 1), (2, 0)), fst.def_.decorators.node_list),
    (((1, 1),  (1, 5)),  ((1, 1), (1, 5)), fst.def_.decorators.node_list[0]),
    (((1, 2),  (1, 5)),  ((1, 1), (1, 4)), fst.def_.decorators.node_list[0].value),
    (((1, 2),  (1, 5)),  ((1, 1), (1, 4)), fst.def_.decorators.node_list[0].value.value),
    (((1, 2),  (1, 5)),  ((1, 1), (1, 4)), fst.def_.decorators.node_list[0].value.value[0]),
    (((1, 6),  (2, 0)),  ((1, 1), (2, 0)), fst.def_.decorators.node_list[1]),
    (((2, 4),  (2, 4)),  ((1, 1), (1, 1)), fst.def_.first_formatting),
    (((2, 4),  (2, 4)),  ((1, 1), (1, 1)), fst.def_.first_formatting[0]),
    (((2, 6),  (2, 5)),  ((1, 1), (1, 0)), fst.def_.second_formatting),
    (((2, 7),  (2, 6)),  ((1, 1), (1, 0)), fst.def_.third_formatting),
    (((2, 7),  (2, 10)), ((1, 1), (1, 4)), fst.def_.arguments),
    (((2, 7),  (2, 7)),  ((1, 1), (1, 1)), fst.def_.arguments.node_list[0]),
    (((2, 8),  (2, 9)),  ((1, 1), (1, 2)), fst.def_.arguments.node_list[1]),
    (((2, 10), (2, 10)), ((1, 1), (1, 1)), fst.def_.arguments.node_list[2]),
    (((2, 11), (2, 10)), ((1, 1), (1, 0)), fst.def_.fourth_formatting),
    (((2, 12), (2, 11)), ((1, 1), (1, 0)), fst.def_.fifth_formatting),
    (((2, 13), (2, 12)), ((1, 1), (1, 0)), fst.def_.sixth_formatting),
    (((2, 13), (4, 0)),  ((1, 1), (3, 0)), fst.def_.value),
    (((2, 13), (3, 4)),  ((1, 1), (2, 4)), fst.def_.value.node_list[0]),
    (((3, 5),  (3, 13)), ((1, 1), (1, 9)), fst.def_.value.node_list[1]),
    (((3, 5),  (3, 5)),  ((1, 1), (1, 1)), fst.def_.value.node_list[1].target),
    (((3, 9),  (3, 13)), ((1, 1), (1, 5)), fst.def_.value.node_list[1].value),
    (((3, 9),  (3, 9)),  ((1, 1), (1, 1)), fst.def_.value.node_list[1].value.first),
    (((3, 13), (3, 13)), ((1, 1), (1, 1)), fst.def_.value.node_list[1].value.second),
    (((3, 14), (4, 0)),  ((1, 1), (2, 0)), fst.def_.value.node_list[2])
]

@pytest.fixture(params=bounding_boxes)
def bounding_box_fixture(request):
    return request.param

def test_bounding_box(bounding_box_fixture):
    absolute_bounding_box, bounding_box, node = bounding_box_fixture
    assert bounding_box == node.bounding_box
    assert absolute_bounding_box == node.absolute_bounding_box


def test_bounding_box_of_attribute(red):
    assert ((2, 1), (2, 3)) == red.def_.get_absolute_bounding_box_of_attribute("def")


def test_bounding_box_of_attribute_no_attribute(red):
    with pytest.raises(KeyError):
        red.def_.get_absolute_bounding_box_of_attribute("xxx")


def test_bounding_box_of_attribute_no_index(red):
    with pytest.raises(IndexError):
        red.get_absolute_bounding_box_of_attribute(1)

    with pytest.raises(IndexError):
        red.get_absolute_bounding_box_of_attribute(-1)


def test_bounding_box_empty():
    red = RedBaron("a()")
    assert ((1, 3), (1, 2)) == red.atomtrailers.value[1].value.absolute_bounding_box


RED = RedBaron("""\
class A:
    ''' Class docstring

    Class description
    '''
    attrA = [ a,
         b,
            c,
               d]

    def a(self):
        ''' Function a docstring

        Function description
        '''
        pass

    attrB = valB

    @myDecorator
    def b(self):
        ''' Function b docstring

        Function description
        '''
        pass

    attrC = [ a,
         b,
            c,
               d]\
""")


def test_bounding_box_with_proxy_list():
    assert ((1, 1), (32, 0)) == RED.absolute_bounding_box
    assert ((1, 1), (32, 0)) == RED.class_.absolute_bounding_box
    assert ((2, 5), (5, 7)) == RED.class_.value[0].absolute_bounding_box
    assert ((6, 5), (9, 17)) == RED.class_.value[1].absolute_bounding_box
    assert ((10, 1), (11, 4)) == RED.class_.value[2].absolute_bounding_box
    assert ((11, 5), (18, 4)) == RED.class_.value[3].absolute_bounding_box
    assert ((18, 5), (18, 16)) == RED.class_.value[4].absolute_bounding_box
    assert ((19, 1), (20, 4)) == RED.class_.value[5].absolute_bounding_box
    assert ((20, 5), (28, 4)) == RED.class_.value[6].absolute_bounding_box
    assert ((28, 5), (31, 17)) == RED.class_.value[7].absolute_bounding_box
    with pytest.raises(IndexError):
        RED.class_.value[8]


def test_bounding_box_of_attribute_with_proxy_list():
    assert ((1, 1), (32, 0)) == RED.absolute_bounding_box
    assert ((1, 1), (32, 0)) == RED.class_.absolute_bounding_box
    assert ((2, 5), (5, 7)) == RED.class_.value.get_absolute_bounding_box_of_attribute(0)
    assert ((6, 5), (9, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(1)
    assert ((10, 1), (11, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(2)
    assert ((11, 5), (18, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(3)
    assert ((18, 5), (18, 16)) == RED.class_.value.get_absolute_bounding_box_of_attribute(4)
    assert ((19, 1), (20, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(5)
    assert ((20, 5), (28, 4)) == RED.class_.value.get_absolute_bounding_box_of_attribute(6)
    assert ((28, 5), (31, 17)) == RED.class_.value.get_absolute_bounding_box_of_attribute(7)
    with pytest.raises(IndexError):
        RED.class_.value.get_absolute_bounding_box_of_attribute(8)