File: test_parse.py

package info (click to toggle)
rapidyaml 0.10.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 53,676 kB
  • sloc: cpp: 73,851; python: 3,678; javascript: 414; xml: 253; makefile: 96; sh: 44
file content (525 lines) | stat: -rw-r--r-- 17,665 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
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
import ryml
try:
    from ryml.ryml import _same_ptr, _same_mem
except:
    from ryml import _same_ptr, _same_mem
import unittest


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class TestSubstrInterop(unittest.TestCase):

    # ------------------------------------------------
    # str

    # CAN create c4::csubstr from string object
    def test11_str2csubstr(self):
        s = "asdasd"
        m = ryml.as_csubstr(s)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, ryml.u(m))
        #
        m = ryml.as_csubstr(m)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, ryml.u(m))

    # CANNOT create c4::substr from string object
    def test12_str2substr(self):
        s = ""
        with self.assertRaises(TypeError) as context:
            _ = ryml.as_substr(s)
        self.assertTrue(type(context.exception), TypeError)

    # ------------------------------------------------
    # bytes

    # CAN create c4::csubstr from string object
    def test21_bytes2csubstr(self):
        s = b"foo21"
        m = ryml.as_csubstr(s)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, m)
        #
        m = ryml.as_csubstr(m)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, m)

    # CANNOT create c4::csubstr from string object
    def test22_bytes2substr(self):
        s = b"foo22"
        with self.assertRaises(TypeError) as context:
            _ = ryml.as_substr(s)
        self.assertTrue(type(context.exception), TypeError)

    # ------------------------------------------------
    # bytearray

    # CAN create c4::csubstr from string object
    def test31_bytes2csubstr(self):
        s = bytearray("foo31", "utf8")
        m = ryml.as_csubstr(s)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, m)
        #
        m = ryml.as_csubstr(m)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, m)

    # CANNOT create c4::csubstr from string object
    def test32_bytes2substr(self):
        s = bytearray("foo31", "utf8")
        m = ryml.as_csubstr(s)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, m)
        #
        m = ryml.as_csubstr(m)
        self.assertTrue(_same_ptr(s, m))
        self.assertTrue(_same_mem(s, m))
        self.assertEqual(s, m)


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

def _addmap(t, node, k=None):
    m = t.append_child(node)
    if k is None:
        t.to_map(m)
    else:
        t.to_map(m, k)
    return m


def _addseq(t, node, k=None):
    m = t.append_child(node)
    if k is None:
        t.to_seq(m)
    else:
        t.to_seq(m, k)
    return m


def _addval(t, node, k, v=None):
    ch = t.append_child(node)
    if v is None:
        t.to_val(ch, k)
    else:
        t.to_keyval(ch, k, v)
    return ch


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
def check_tree_mod(ut, t):
    # some convenient shorthands
    eq = ut.assertEqual
    def _addval_and_check(node, k, v=None):
        ch = _addval(t, node, k, v)
        pos = t.child_pos(node, ch)
        eq(t.child(node, pos), ch)
        if v is not None:
            eq(t.find_child(node, k), ch)
            eq(t.child(node, pos), t.find_child(node, k))
        return ch
    def _addseq_and_check(node, k):
        ch = _addseq(t, node, k)
        eq(t.find_child(node, k), ch)
        return ch
    def _addmap_and_check(node, k):
        ch = _addmap(t, node, k)
        eq(t.find_child(node, k), ch)
        return ch
    m = _addmap_and_check(t.root_id(), "check_tree_mod_map")
    _addval_and_check(m, "k1", "v1")
    _addval_and_check(m, "k2", "v2")
    _addval_and_check(m, "k3", "v3")
    eq(t.num_children(m), 3)
    eq(t.num_siblings(t.first_child(m)), 3)
    s = _addseq_and_check(t.root_id(), "check_tree_mod_seq")
    _addval_and_check(s, "v1")
    _addval_and_check(s, "v2")
    _addval_and_check(s, "v3")
    eq(t.num_children(s), 3)
    eq(t.num_siblings(t.first_child(m)), 3)


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class SimpleTestCase:

    yaml = "{'HELLO': a, foo: \"b\", bar: c, baz: d, seq: [0, 1, 2, 3]}"

    def check(self, ut, t, is_json=False):
        # some convenient shorthands
        eq = ut.assertEqual
        ne = ut.assertNotEqual
        fs = ut.assertFalse
        tr = ut.assertTrue
        #
        eq(t.size(), 10)
        tr(t.is_root(0))
        eq(t.num_children(0), 5)
        eq(t.find_child(0, b"HELLO"), 1)
        eq(t.find_child(0, b"foo"), 2)
        eq(t.find_child(0, b"bar"), 3)
        eq(t.find_child(0, b"baz"), 4)
        eq(t.find_child(0, b"seq"), 5)
        eq(t.parent(0), ryml.NONE)
        eq(t.parent(1), 0)
        eq(t.parent(2), 0)
        eq(t.parent(3), 0)
        eq(t.parent(4), 0)
        eq(t.parent(5), 0)
        fs(t.is_root(1))
        fs(t.is_root(2))
        fs(t.is_root(3))
        fs(t.is_root(4))
        fs(t.is_root(5))
        fs(t.has_child(0, b"foozzie"))
        fs(t.has_child(0, b"bark"))
        fs(t.has_child(0, b"bart"))
        fs(t.has_child(0, b"bazk"))
        eq(t.next_sibling(0), ryml.NONE)
        eq(t.prev_sibling(0), ryml.NONE)
        eq(t.prev_sibling(1), ryml.NONE)
        eq(t.next_sibling(5), ryml.NONE)
        tr(t.has_child(0, b"HELLO"))
        tr(t.has_child(0, b"foo"))
        tr(t.has_child(0, b"bar"))
        tr(t.has_child(0, b"baz"))
        eq(t.key(1), b"HELLO")
        eq(t.key(2), b"foo")
        eq(t.key(3), b"bar")
        eq(t.key(4), b"baz")
        eq(t.key(5), b"seq")
        eq(t.val(1), b"a")
        eq(t.val(2), b"b")
        eq(t.val(3), b"c")
        eq(t.val(4), b"d")
        eq(t.val(6), b"0")
        eq(t.val(7), b"1")
        eq(t.val(8), b"2")
        eq(t.val(9), b"3")
        if not is_json:
            tr(t.is_key_squo(1))
            fs(t.is_key_squo(2))
            fs(t.is_key_squo(3))
            fs(t.is_key_squo(4))
            fs(t.is_key_squo(5))
            tr(t.is_key_quoted(1))
            fs(t.is_key_quoted(2))
            fs(t.is_key_quoted(3))
            fs(t.is_key_quoted(4))
            fs(t.is_key_quoted(5))
        else:
            tr(t.is_key_dquo(1))
            tr(t.is_key_dquo(2))
            tr(t.is_key_dquo(3))
            tr(t.is_key_dquo(4))
            tr(t.is_key_dquo(5))
            tr(t.is_key_quoted(1))
            tr(t.is_key_quoted(2))
            tr(t.is_key_quoted(3))
            tr(t.is_key_quoted(4))
            tr(t.is_key_quoted(5))
        if not is_json:
            fs(t.is_val_quoted(1))
            tr(t.is_val_quoted(2))
            fs(t.is_val_quoted(3))
            fs(t.is_val_quoted(4))
            fs(t.is_val_quoted(5))
            fs(t.is_val_quoted(6))
            fs(t.is_val_quoted(7))
            fs(t.is_val_quoted(8))
            fs(t.is_val_quoted(9))
        else:
            tr(t.is_val_quoted(1))
            tr(t.is_val_quoted(2))
            tr(t.is_val_quoted(3))
            tr(t.is_val_quoted(4))
            fs(t.is_val_quoted(5))
            fs(t.is_val_quoted(6))
            fs(t.is_val_quoted(7))
            fs(t.is_val_quoted(8))
            fs(t.is_val_quoted(9))
        if not is_json:
            tr(t.is_quoted(1))
            tr(t.is_quoted(2))
            fs(t.is_quoted(3))
            fs(t.is_quoted(4))
            fs(t.is_quoted(5))
            fs(t.is_quoted(6))
            fs(t.is_quoted(7))
            fs(t.is_quoted(8))
            fs(t.is_quoted(9))
        else:
            tr(t.is_quoted(1))
            tr(t.is_quoted(2))
            tr(t.is_quoted(3))
            tr(t.is_quoted(4))
            tr(t.is_quoted(5))
            fs(t.is_quoted(6))
            fs(t.is_quoted(7))
            fs(t.is_quoted(8))
            fs(t.is_quoted(9))
        tr(t.has_sibling(1, b"bar"))
        tr(t.has_sibling(1, b"baz"))
        tr(t.has_sibling(2, b"foo"))
        tr(t.has_sibling(2, b"baz"))
        tr(t.has_sibling(3, b"foo"))
        tr(t.has_sibling(3, b"bar"))
        for i in (1, 2, 3, 4, 5):
            eq(t.find_sibling(i, b"HELLO"), 1)
            eq(t.find_sibling(i, b"foo"), 2)
            eq(t.find_sibling(i, b"bar"), 3)
            eq(t.find_sibling(i, b"baz"), 4)
            eq(t.find_sibling(i, b"seq"), 5)
        #
        num = 0
        for id in ryml.children(t):
            num += 1
            eq(id, num)
        eq(num, t.num_children(t.root_id()))
        eq(num, t.num_siblings(t.first_child(t.root_id())))
        #
        num = 0
        for id in ryml.children(t, 1):
            num += 1
        eq(num, 0)
        #
        num = 0
        for id in ryml.siblings(t, 1):
            num += 1
            eq(id, num)
        eq(num, t.num_children(t.root_id()))
        eq(num, t.num_siblings(t.first_child(t.root_id())))
        #
        num = 0
        for id in ryml.siblings(t, 3):
            num += 1
            eq(id, num)
        eq(num, 5)
        eq(num, t.num_siblings(t.first_child(t.root_id())))
        #
        for i, ch in enumerate(ryml.children(t, 5)):
            eq(t.val(ch), [b"0", b"1", b"2", b"3"][i])
        sibs = [b"HELLO", b"foo", b"bar", b"baz", b"seq"]
        sibs_s = ["HELLO", "foo", "bar", "baz", "seq"]
        for i, sib in enumerate(ryml.siblings(t, 5)):
            k = t.key(sib)
            k_s = str(k, "utf8")
            eq(k, sibs[i])
            eq(k_s, sibs_s[i])
            ne(k, sibs_s[i])
            ne(k_s, sibs[i])
            k_s = str(k)
            ne(k_s, sibs_s[i])
            ne(k_s, sibs[i])
        num = 0
        for id in ryml.siblings(t, 0):
            num += 1
        eq(num, 1)
        #
        num = 0
        for id, level in ryml.walk(t):
            num += 1
            if t.is_root(id):
                eq(id, 0)
                eq(level, 0)
            if t.is_map(id):
                eq(id, 0)
                eq(level, 0)
            if t.is_seq(id):
                eq(id, 5)
                eq(level, 1)
            if t.is_keyval(id):
                tr(id > 0 and id < 5)
            if t.is_val(id):
                tr(id > 5)
                eq(level, 2)
        eq(num, t.size())
        #
        num = 0
        for id in ryml.walk(t, 5):
            num += 1
        eq(num, 5)
        #
        num = 0
        for id in ryml.walk(t, 9):
            num += 1
        eq(num, 1)
        check_tree_mod(ut, t)


# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class TestRunner(unittest.TestCase):

    def setUp(self):
        self._setUp(SimpleTestCase())

    # allow creating this class with different cases
    # if they are added
    def _setUp(self, case):
        self.case = case
        self.src_as_str = str(case.yaml)
        self.src_as_bytes = bytes(case.yaml, "utf8")
        self.src_as_bytearray = bytearray(case.yaml, "utf8")

    # ----------------------------------------------------------
    def test11_str__arena(self):  # cannot read string buffers (or can we?)
        tree = ryml.parse_in_arena(self.src_as_str)
        self.case.check(self, tree)

    def test12_str__arena__reuse_tree(self):  # cannot read string buffers (or can we?)
        t = ryml.Tree()
        ryml.parse_in_arena(self.src_as_str, tree=t)
        self.case.check(self, t)

    def test13_str__inplace(self):  # cannot mutate string buffers (or can we?)
        with self.assertRaises(TypeError) as context:
            ryml.parse_in_place(self.src_as_str)
        self.assertTrue(type(context.exception), TypeError)

    # ----------------------------------------------------------
    def test21_bytes__arena(self):
        tree = ryml.parse_in_arena(self.src_as_bytes)
        self.case.check(self, tree)

    def test22_bytes__arena__reuse_tree(self):
        t = ryml.Tree()
        r = ryml.parse_in_arena(self.src_as_bytes, tree=t)
        self.assertTrue(r is t)
        self.case.check(self, t)

    def test23_bytes__inplace(self):  # cannot mutate bytes buffers
        with self.assertRaises(TypeError) as context:
            ryml.parse_in_place(self.src_as_bytes)
        self.assertTrue(type(context.exception), TypeError)

    # ----------------------------------------------------------
    def test31_bytearray__arena(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        self.case.check(self, tree)

    def test32_bytearray__arena__reuse_tree(self):
        t = ryml.Tree()
        r = ryml.parse_in_arena(self.src_as_bytearray, tree=t)
        self.assertTrue(r is t)
        self.case.check(self, t)

    def test33_bytearray__inplace(self):  # bytearray buffers are mutable
        tree = ryml.parse_in_place(self.src_as_bytearray)
        self.case.check(self, tree)

    def test34_bytearray__inplace__reuse_tree(self):  # bytearray buffers are mutable
        t = ryml.Tree()
        r = ryml.parse_in_place(self.src_as_bytearray, tree=t)
        self.assertTrue(r is t)
        self.case.check(self, t)

    # ----------------------------------------------------------
    def test41_emit_yaml(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        yaml = ryml.emit_yaml(tree)
        output_tree = ryml.parse_in_arena(yaml)
        self.case.check(self, output_tree)

    def test41_emit_json(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        json = ryml.emit_json(tree)
        output_tree = ryml.parse_in_arena(json)
        self.case.check(self, output_tree, is_json=True)

    def test42_compute_emit_yaml_length(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        yaml = ryml.emit_yaml(tree)
        length = ryml.compute_emit_yaml_length(tree)
        self.assertEqual(len(yaml), length)

    def test42_compute_emit_json_length(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        json = ryml.emit_json(tree)
        length = ryml.compute_emit_json_length(tree)
        self.assertEqual(len(json), length)

    def test43_emit_yaml_inplace(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        yaml = ryml.emit_yaml(tree)
        length = ryml.compute_emit_yaml_length(tree)
        self.assertEqual(len(yaml), length)
        buf = bytearray(length)
        s = ryml.emit_yaml_in_place(tree, buf)
        self.assertEqual(len(s), length)
        self.assertTrue(s.tobytes().decode('utf-8') == yaml)
        self.assertTrue(buf.decode('utf-8') == yaml)

    def test43_emit_json_inplace(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        json = ryml.emit_json(tree)
        length = ryml.compute_emit_json_length(tree)
        self.assertEqual(len(json), length)
        buf = bytearray(length)
        s = ryml.emit_json_in_place(tree, buf)
        self.assertEqual(len(s), length)
        self.assertTrue(s.tobytes().decode('utf-8') == json)
        self.assertTrue(buf.decode('utf-8') == json)

    def test44_emit_yaml_short_buf(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        length = ryml.compute_emit_yaml_length(tree)
        buf = bytearray(length-1)
        with self.assertRaises(IndexError):
            ryml.emit_yaml_in_place(tree, buf)

    def test44_emit_json_short_buf(self):
        tree = ryml.parse_in_arena(self.src_as_bytearray)
        length = ryml.compute_emit_json_length(tree)
        buf = bytearray(length-1)
        with self.assertRaises(IndexError):
            ryml.emit_json_in_place(tree, buf)



# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

class TestParseFailure(unittest.TestCase):

    yaml = "[:HELLO: b}"

    def setUp(self):
        self.src_as_str = str(__class__.yaml)
        self.src_as_bytearray = bytearray(__class__.yaml, "utf8")

    def test_in_arena(self):
        self.assertNotEqual(self.src_as_str, "")
        with self.assertRaises(RuntimeError):
            tree = ryml.parse_in_arena(self.src_as_str)

    def test_in_place(self):
        self.assertNotEqual(self.src_as_bytearray, "")
        with self.assertRaises(RuntimeError):
            tree = ryml.parse_in_place(self.src_as_bytearray)



# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
if __name__ == "__main__":
    unittest.main()