Skip to content

test_def_file_decoder_positions

Unit tests of DefFileDecoder key-value positions

Functions:

decoder

decoder()

Initialize decoder with value position parsing

Source code in tests/test_def_file_decoder_positions.py
 8
 9
10
11
@pytest.fixture
def decoder():
    """Initialize decoder with value position parsing"""
    return DefFileDecoder(include_positions=True)

test_array_positions

test_array_positions(decoder)

Test positions of decoded arrays

Source code in tests/test_def_file_decoder_positions.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def test_array_positions(decoder):
    """Test positions of decoded arrays"""
    assert decoder.decode("a = 1,2") == {
        "a": {"value": [1, 2], "key_start_pos": 0, "start_pos": 4, "end_pos": 7}
    }

    assert decoder.decode("a = 1,2,3") == {
        "a": {"value": [1, 2, 3], "key_start_pos": 0, "start_pos": 4, "end_pos": 9}
    }

    assert decoder.decode("a = a, 2, y") == {
        "a": {
            "value": ["a", 2, True],
            "key_start_pos": 0,
            "start_pos": 4,
            "end_pos": 11,
        }
    }

test_auto_positions

test_auto_positions(decoder)

Test positions of decoded auto values

Source code in tests/test_def_file_decoder_positions.py
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
def test_auto_positions(decoder):
    """Test positions of decoded auto values"""
    assert decoder.decode("a=_AUTO_") == {
        "a": {
            "value": ("_AUTO_", None),
            "key_start_pos": 0,
            "start_pos": 2,
            "end_pos": 8,
        }
    }
    assert decoder.decode("a=_AUTO_:DEFAULT=3.14") == {
        "a": {
            "value": ("_AUTO_", 3.14),
            "key_start_pos": 0,
            "start_pos": 2,
            "end_pos": 21,
        }
    }
    assert decoder.decode("a=_AUTOBLOCKER_") == {
        "a": {
            "value": ("_AUTOBLOCKER_", None),
            "key_start_pos": 0,
            "start_pos": 2,
            "end_pos": 15,
        }
    }
    assert decoder.decode("a=_AUTOBLOCKER_:DEFAULT='text'") == {
        "a": {
            "value": ("_AUTOBLOCKER_", "'text'"),
            "key_start_pos": 0,
            "start_pos": 2,
            "end_pos": 30,
        }
    }

test_compound_positions

test_compound_positions(decoder)

Test decoding of various positions

Source code in tests/test_def_file_decoder_positions.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def test_compound_positions(decoder):
    """Test decoding of various positions"""
    assert decoder.decode("a=1\nb='text'\nc=_AUTO_:DEFAULT=3.14") == {
        "a": {"value": 1, "key_start_pos": 0, "start_pos": 2, "end_pos": 3},
        "b": {"value": "'text'", "key_start_pos": 4, "start_pos": 6, "end_pos": 12},
        "c": {
            "value": ("_AUTO_", 3.14),
            "key_start_pos": 13,
            "start_pos": 15,
            "end_pos": 34,
        },
    }
    # TODO: consider keeping start/end positions for each array element
    assert decoder.decode("a=1,2,3") == {
        "a": {"end_pos": 7, "key_start_pos": 0, "start_pos": 2, "value": [1, 2, 3]}
    }

test_float_positions

test_float_positions(decoder)

Test positions of decoded float numbers

Source code in tests/test_def_file_decoder_positions.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def test_float_positions(decoder):
    """Test positions of decoded float numbers"""
    assert decoder.decode(" a=3.1415") == {
        "a": {"value": 3.1415, "key_start_pos": 1, "start_pos": 3, "end_pos": 9}
    }
    assert decoder.decode("  a=3.14e-2") == {
        "a": {"value": 0.0314, "key_start_pos": 2, "start_pos": 4, "end_pos": 11}
    }
    assert decoder.decode("a =-3.1415") == {
        "a": {"value": -3.1415, "key_start_pos": 0, "start_pos": 3, "end_pos": 10}
    }
    assert decoder.decode("a= -3.14e-2") == {
        "a": {"value": -0.0314, "key_start_pos": 0, "start_pos": 3, "end_pos": 11}
    }

test_include_positions

test_include_positions(decoder)

Test positions of decoded INCLUDEDEF statements

Source code in tests/test_def_file_decoder_positions.py
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
def test_include_positions(decoder):
    """Test positions of decoded INCLUDEDEF statements"""
    assert decoder.decode("INCLUDEDEF=module1") == {
        "INCLUDEDEF": [
            {
                "value": "module1",
                "key_start_pos": 0,
                "start_pos": 11,
                "end_pos": 18,
            }
        ]
    }
    assert decoder.decode("INCLUDEDEF=module1\nINCLUDEDEF=module2") == {
        "INCLUDEDEF": [
            {
                "value": "module1",
                "key_start_pos": 0,
                "start_pos": 11,
                "end_pos": 18,
            },
            {
                "value": "module2",
                "key_start_pos": 19,
                "start_pos": 30,
                "end_pos": 37,
            },
        ]
    }

test_integer_positions

test_integer_positions(decoder)

Test position of decoded integer numbers

Source code in tests/test_def_file_decoder_positions.py
74
75
76
77
78
79
80
81
82
83
84
85
86
def test_integer_positions(decoder):
    """Test position of decoded integer numbers"""
    assert decoder.decode("a=1") == {
        "a": {"value": 1, "key_start_pos": 0, "start_pos": 2, "end_pos": 3}
    }

    assert decoder.decode("a=-1") == {
        "a": {"value": -1, "key_start_pos": 0, "start_pos": 2, "end_pos": 4}
    }

    assert decoder.decode("a=6123") == {
        "a": {"value": 6123, "key_start_pos": 0, "start_pos": 2, "end_pos": 6}
    }

test_logical_positions

test_logical_positions(decoder)

Test positions of decoded logical values

Source code in tests/test_def_file_decoder_positions.py
44
45
46
47
48
49
50
51
52
53
54
55
def test_logical_positions(decoder):
    """Test positions of decoded logical values"""
    assert decoder.decode("a = true\nb=y\nc=TRUE") == {
        "a": {"value": True, "key_start_pos": 0, "start_pos": 4, "end_pos": 8},
        "b": {"value": True, "key_start_pos": 9, "start_pos": 11, "end_pos": 12},
        "c": {"value": True, "key_start_pos": 13, "start_pos": 15, "end_pos": 19},
    }
    assert decoder.decode("a = false\nb=n\nc=FALSE") == {
        "a": {"value": False, "key_start_pos": 0, "start_pos": 4, "end_pos": 9},
        "b": {"value": False, "key_start_pos": 10, "start_pos": 12, "end_pos": 13},
        "c": {"value": False, "key_start_pos": 14, "start_pos": 16, "end_pos": 21},
    }

test_string_positions

test_string_positions(decoder)

Test positions of decoded strings (quoted and unqoted)

Source code in tests/test_def_file_decoder_positions.py
89
90
91
92
93
94
95
96
97
98
99
def test_string_positions(decoder):
    """Test positions of decoded strings (quoted and unqoted)"""
    assert decoder.decode("a=text") == {
        "a": {"value": "text", "key_start_pos": 0, "start_pos": 2, "end_pos": 6}
    }
    assert decoder.decode("a='text'") == {
        "a": {"value": "'text'", "key_start_pos": 0, "start_pos": 2, "end_pos": 8}
    }
    assert decoder.decode('a="text"') == {
        "a": {"value": '"text"', "key_start_pos": 0, "start_pos": 2, "end_pos": 8}
    }