Skip to content

test_card_file_decoder_positions

Unit tests of CardFileDecoder positions decoding

Functions:

decoder

decoder()

Initialize decoder with value position parsing

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

test_compound_positions

test_compound_positions(decoder)

Test decoding of various positions

Source code in tests/test_card_file_decoder_positions.py
74
75
76
77
78
79
80
81
82
83
84
85
def test_compound_positions(decoder):
    """Test decoding of various positions"""
    assert decoder.decode("a=1\nb='text'\nc=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": 3.14,
            "key_start_pos": 13,
            "start_pos": 15,
            "end_pos": 19,
        },
    }

test_float_positions

test_float_positions(decoder)

Test decoded positions of float numbers

Source code in tests/test_card_file_decoder_positions.py
308
309
310
311
312
313
314
315
def test_float_positions(decoder):
    """Test decoded positions of float numbers"""
    assert decoder.decode("a=3.14e-2") == {
        "a": {"value": 0.0314, "start_pos": 2, "end_pos": 9, "key_start_pos": 0}
    }
    assert decoder.decode("a=-3.14e-2") == {
        "a": {"value": -0.0314, "start_pos": 2, "end_pos": 10, "key_start_pos": 0}
    }

test_integer_positions

test_integer_positions(decoder)

Test decoded positions of integer numbers

Source code in tests/test_card_file_decoder_positions.py
298
299
300
301
302
303
304
305
def test_integer_positions(decoder):
    """Test decoded positions of integer numbers"""
    assert decoder.decode("a=-1") == {
        "a": {"value": -1, "start_pos": 2, "end_pos": 4, "key_start_pos": 0}
    }
    assert decoder.decode("a=6123") == {
        "a": {"value": 6123, "start_pos": 2, "end_pos": 6, "key_start_pos": 0}
    }

test_list_positions

test_list_positions(decoder)

Test decoded positions of various lists

Source code in tests/test_card_file_decoder_positions.py
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
def test_list_positions(decoder):
    """Test decoded positions of various lists"""
    assert decoder.decode("a= ()") == {
        "a": {"value": [], "start_pos": 3, "end_pos": 5, "key_start_pos": 0}
    }
    assert decoder.decode("a = (1,2)") == {
        "a": {
            "value": [
                {"value": 1, "start_pos": 5, "end_pos": 6},
                {"value": 2, "start_pos": 7, "end_pos": 8},
            ],
            "start_pos": 4,
            "end_pos": 9,
            "key_start_pos": 0,
        }
    }
    assert decoder.decode("a = (1,2,3)") == {
        "a": {
            "value": [
                {"value": 1, "start_pos": 5, "end_pos": 6},
                {"value": 2, "start_pos": 7, "end_pos": 8},
                {"value": 3, "start_pos": 9, "end_pos": 10},
            ],
            "start_pos": 4,
            "end_pos": 11,
            "key_start_pos": 0,
        }
    }

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

test_literal_positions

test_literal_positions(decoder)

Test decoded positions of all literals

Source code in tests/test_card_file_decoder_positions.py
88
89
90
91
92
93
94
95
96
97
98
def test_literal_positions(decoder):
    """Test decoded positions of all literals"""
    assert decoder.decode("k=") == {
        "k": {"value": None, "start_pos": 2, "end_pos": 2, "key_start_pos": 0}
    }
    assert decoder.decode("k=y") == {
        "k": {"value": True, "start_pos": 2, "end_pos": 3, "key_start_pos": 0}
    }
    assert decoder.decode("k=false") == {
        "k": {"value": False, "start_pos": 2, "end_pos": 7, "key_start_pos": 0}
    }

test_logical_positions

test_logical_positions(decoder)

Test decoded positions of logical values

Source code in tests/test_card_file_decoder_positions.py
318
319
320
321
322
323
324
def test_logical_positions(decoder):
    """Test decoded positions of logical values"""
    assert decoder.decode("a = true\nb=y\nc=TRUE") == {
        "a": {"value": True, "start_pos": 4, "end_pos": 8, "key_start_pos": 0},
        "b": {"value": True, "start_pos": 11, "end_pos": 12, "key_start_pos": 9},
        "c": {"value": True, "start_pos": 15, "end_pos": 19, "key_start_pos": 13},
    }

test_nested_list_positions

test_nested_list_positions(decoder)

Test decoding of nested lists (lists of lists)

Source code in tests/test_card_file_decoder_positions.py
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
def test_nested_list_positions(decoder):
    """Test decoding of nested lists (lists of lists)"""
    assert decoder.decode("a = (),\\()") == {
        "a": {
            "start_pos": 4,
            "end_pos": 10,
            "key_start_pos": 0,
            "value": [
                {
                    "value": [],
                    "start_pos": 4,
                    "end_pos": 6,
                },
                {
                    "value": [],
                    "start_pos": 8,
                    "end_pos": 10,
                },
            ],
        }
    }
    assert decoder.decode("a = (1,2),\\(3,4)") == {
        "a": {
            "start_pos": 4,
            "end_pos": 16,
            "key_start_pos": 0,
            "value": [
                {
                    "value": [
                        {
                            "value": 1,
                            "start_pos": 5,
                            "end_pos": 6,
                        },
                        {
                            "value": 2,
                            "start_pos": 7,
                            "end_pos": 8,
                        },
                    ],
                    "start_pos": 4,
                    "end_pos": 9,
                },
                {
                    "value": [
                        {
                            "value": 3,
                            "start_pos": 12,
                            "end_pos": 13,
                        },
                        {
                            "value": 4,
                            "start_pos": 14,
                            "end_pos": 15,
                        },
                    ],
                    "start_pos": 11,
                    "end_pos": 16,
                },
            ],
        }
    }
    assert decoder.decode("a=(1,2,3),\\(a,b),\\(y)") == {
        "a": {
            "start_pos": 2,
            "end_pos": 21,
            "key_start_pos": 0,
            "value": [
                {
                    "value": [
                        {
                            "value": 1,
                            "start_pos": 3,
                            "end_pos": 4,
                        },
                        {
                            "value": 2,
                            "start_pos": 5,
                            "end_pos": 6,
                        },
                        {
                            "value": 3,
                            "start_pos": 7,
                            "end_pos": 8,
                        },
                    ],
                    "start_pos": 2,
                    "end_pos": 9,
                },
                {
                    "value": [
                        {
                            "value": "a",
                            "start_pos": 12,
                            "end_pos": 13,
                        },
                        {
                            "value": "b",
                            "start_pos": 14,
                            "end_pos": 15,
                        },
                    ],
                    "start_pos": 11,
                    "end_pos": 16,
                },
                {
                    "value": [
                        {
                            "value": True,
                            "start_pos": 19,
                            "end_pos": 20,
                        }
                    ],
                    "start_pos": 18,
                    "end_pos": 21,
                },
            ],
        }
    }

test_range_positions

test_range_positions(decoder)

Test decoded positions of ranges in the form START:STEP:END

Source code in tests/test_card_file_decoder_positions.py
265
266
267
268
269
270
271
272
273
274
275
276
277
def test_range_positions(decoder):
    """Test decoded positions of ranges in the form START:STEP:END"""
    assert decoder.decode("a=1:2:3") == {
        "a": {"value": (1, 2, 3), "start_pos": 2, "end_pos": 7, "key_start_pos": 0}
    }
    assert decoder.decode("a=-100:-5:-200") == {
        "a": {
            "value": (-100, -5, -200),
            "start_pos": 2,
            "end_pos": 14,
            "key_start_pos": 0,
        }
    }

test_section_positions

test_section_positions(decoder)

Test decoded positions of sections

Source code in tests/test_card_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
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
def test_section_positions(decoder):
    """Test decoded positions of sections"""
    assert decoder.decode("[section1][section2]") == {
        "section1": {"value": {}, "section_start_pos": 0, "section_end_pos": 10},
        "section2": {"value": {}, "section_start_pos": 10, "section_end_pos": 20},
    }
    assert decoder.decode("[section1]\n[section2]") == {
        "section1": {"value": {}, "section_start_pos": 0, "section_end_pos": 10},
        "section2": {"value": {}, "section_start_pos": 11, "section_end_pos": 21},
    }
    assert decoder.decode("[section1]\na=3\nb=(1,2,3)[section2]c='some text'") == {
        "section1": {
            "section_start_pos": 0,
            "section_end_pos": 24,
            "value": {
                "a": {"value": 3, "key_start_pos": 11, "start_pos": 13, "end_pos": 14},
                "b": {
                    "value": [
                        {"value": 1, "start_pos": 18, "end_pos": 19},
                        {"value": 2, "start_pos": 20, "end_pos": 21},
                        {"value": 3, "start_pos": 22, "end_pos": 23},
                    ],
                    "key_start_pos": 15,
                    "start_pos": 17,
                    "end_pos": 24,
                },
            },
        },
        "section2": {
            "section_start_pos": 24,
            "section_end_pos": 47,
            "value": {
                "c": {
                    "value": "'some text'",
                    "key_start_pos": 34,
                    "start_pos": 36,
                    "end_pos": 47,
                }
            },
        },
    }
    assert decoder.decode("a=1\n[section1]b=1\n[section2]\nc=3") == {
        "a": {"value": 1, "key_start_pos": 0, "start_pos": 2, "end_pos": 3},
        "section1": {
            "section_start_pos": 4,
            "section_end_pos": 17,
            "value": {
                "b": {"value": 1, "key_start_pos": 14, "start_pos": 16, "end_pos": 17}
            },
        },
        "section2": {
            "section_start_pos": 18,
            "section_end_pos": 32,
            "value": {
                "c": {"value": 3, "key_start_pos": 29, "start_pos": 31, "end_pos": 32}
            },
        },
    }

test_string_positions

test_string_positions(decoder)

Test decoded positions of strings (quoted and unqoted)

Source code in tests/test_card_file_decoder_positions.py
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def test_string_positions(decoder):
    """Test decoded positions of strings (quoted and unqoted)"""
    assert decoder.decode("key='text'") == {
        "key": {"value": "'text'", "start_pos": 4, "end_pos": 10, "key_start_pos": 0}
    }
    assert decoder.decode('a="text"') == {
        "a": {"value": '"text"', "start_pos": 2, "end_pos": 8, "key_start_pos": 0}
    }
    assert decoder.decode("file1=${SUBMIT_DIR}/../${year}/some_file.ext") == {
        "file1": {
            "value": "${SUBMIT_DIR}/../${year}/some_file.ext",
            "start_pos": 6,
            "end_pos": 44,
            "key_start_pos": 0,
        }
    }