File: test_parser.py

package info (click to toggle)
libvirt 11.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 211,168 kB
  • sloc: ansic: 537,214; xml: 335,516; python: 12,041; perl: 2,626; sh: 2,175; makefile: 448; javascript: 126; cpp: 22
file content (91 lines) | stat: -rw-r--r-- 2,415 bytes parent folder | download | duplicates (4)
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
# SPDX-License-Identifier: LGPL-2.1-or-later

from pathlib import Path

from rpcgen.ast import (
    XDRSpecification,
    XDRDefinitionConstant,
    XDRDefinitionEnum,
    XDRDefinitionUnion,
    XDRDefinitionStruct,
    XDRDeclarationScalar,
    XDRDeclarationVariableArray,
    XDREnumValue,
    XDREnumBody,
    XDRStructBody,
    XDRUnionCase,
    XDRUnionBody,
    XDRTypeCustom,
    XDRTypeVoid,
    XDRTypeString,
    XDRTypeOpaque,
)
from rpcgen.parser import XDRParser


def test_parser():
    p = Path(Path(__file__).parent, "simple.x")
    with p.open("r") as fp:
        parser = XDRParser(fp)

        got = parser.parse()

    enum = XDRDefinitionEnum(
        "filekind",
        XDREnumBody(
            [
                XDREnumValue("TEXT", "0"),
                XDREnumValue("DATA", "1"),
                XDREnumValue("EXEC", "2"),
            ],
        ),
    )

    union = XDRDefinitionUnion(
        "filetype",
        XDRUnionBody(
            XDRDeclarationScalar(XDRTypeCustom("filekind", enum), "kind"),
            [
                XDRUnionCase("TEXT", XDRDeclarationScalar(XDRTypeVoid(), None)),
                XDRUnionCase(
                    "DATA",
                    XDRDeclarationVariableArray(
                        XDRTypeString(), "creator", "MAXNAMELEN"
                    ),
                ),
                XDRUnionCase(
                    "EXEC",
                    XDRDeclarationVariableArray(
                        XDRTypeString(), "interpretor", "MAXNAMELEN"
                    ),
                ),
            ],
            None,
        ),
    )

    struct = XDRDefinitionStruct(
        "file",
        XDRStructBody(
            [
                XDRDeclarationVariableArray(XDRTypeString(), "filename", "MAXNAMELEN"),
                XDRDeclarationScalar(XDRTypeCustom("filetype", union), "type"),
                XDRDeclarationVariableArray(XDRTypeString(), "owner", "MAXUSERNAME"),
                XDRDeclarationVariableArray(XDRTypeOpaque(), "data", "MAXFILELEN"),
            ]
        ),
    )

    want = XDRSpecification()
    want.definitions.extend(
        [
            XDRDefinitionConstant("MAXUSERNAME", "32"),
            XDRDefinitionConstant("MAXFILELEN", "65535"),
            XDRDefinitionConstant("MAXNAMELEN", "255"),
            enum,
            union,
            struct,
        ]
    )

    assert str(got) == str(want)