summaryrefslogtreecommitdiff
path: root/src/scripts/testgen/suitegen.py
blob: cdae6770529d3a3119d1d5852665a08dca086419 (plain)
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
import itertools
import os
from pyolian.eolian import Eolian_Function_Type, Eolian_Class_Type, Eolian_Object_Scope
from .ekeys import GetKey, Function_List_Type
from pyolian import eolian

from testgen import name_helpers


class BaseItem:
    def __init__(self, path, keys, prefix=""):
        self.path = path
        self.keys = keys
        self.prefix = prefix

    def __getattr__(self, attr):
        if not attr.split("_")[-1] in self.keys.keyloads:
            raise AttributeError("Error getting {}".format(attr))

        filename = os.path.join(self.path, self.prefix + attr) + self.keys.ext
        if os.path.isfile(filename):
            with open(filename, "r") as f:
                return f.read()
        return None


class ComItem(BaseItem):
    def __init__(self, comp, path, keys):
        super().__init__(path, keys)
        self.comp = comp

    def __getattr__(self, attr):
        if hasattr(self.comp, attr):
            return getattr(self.comp, attr)
        return super().__getattr__(attr)


class FuncItem(ComItem):
    def __init__(self, comp, path, keys):
        super().__init__(comp, os.path.join(path, comp.name), keys)

        self.has_getter = (
            comp.type in (Eolian_Function_Type.PROP_GET, Eolian_Function_Type.PROPERTY)
            and comp.full_c_getter_name not in keys.blacklist
            and not os.path.isfile("{}_get".format(os.path.join(path, comp.name)))
        )
        self.has_setter = (
            comp.type in (Eolian_Function_Type.PROP_SET, Eolian_Function_Type.PROPERTY)
            and comp.full_c_setter_name not in keys.blacklist
            and not os.path.isfile("{}_set".format(os.path.join(path, comp.name)))
        )

        self.is_enum = (
            lambda arg: arg.type
            and arg.type.typedecl
            and arg.type.typedecl.type == arg.type.typedecl.type.ENUM
        )
        self.is_number = lambda arg: arg.type and arg.type.builtin_type in (
            arg.type.builtin_type.INT,
            arg.type.builtin_type.UINT,
            arg.type.builtin_type.LONG,
            arg.type.builtin_type.ULONG,
            arg.type.builtin_type.LLONG,
            arg.type.builtin_type.ULLONG,
            arg.type.builtin_type.INT8,
            arg.type.builtin_type.UINT8,
            arg.type.builtin_type.INT16,
            arg.type.builtin_type.UINT16,
            arg.type.builtin_type.INT32,
            arg.type.builtin_type.UINT32,
            arg.type.builtin_type.INT64,
            arg.type.builtin_type.UINT64,
            arg.type.builtin_type.INT128,
            arg.type.builtin_type.UINT128,
        )

    @property
    def getter_args(self):
        return itertools.chain(self.getter_values, self.getter_keys)

    @property
    def setter_args(self):
        return itertools.chain(self.setter_values, self.setter_keys)

    @property
    def format_name(self):
        return self.keys.format_name(self)


class EventItem(ComItem):
    def __init__(self, comp, path, keys):
        self.myname = comp.name.replace(",", "_")
        super().__init__(comp, os.path.join(path, self.myname), keys)
        self.format_name = self.keys.event_convert(self)


class ClassItem(ComItem):
    def __init__(self, comp, path, keys):
        self.myname = os.path.splitext(comp.file)[0]
        super().__init__(comp, os.path.join(path, self.myname), keys)

        def mfilter(f):
            if f.full_c_method_name in self.keys.blacklist:
                return False

            if os.path.isfile(os.path.join(self.path, f.name)):
                return False

            if f.method_scope != Eolian_Object_Scope.PUBLIC:
                return False

            return True

        def get_all_inherited(leaf, getter):
            print("Getting all items for leaf", leaf)
            names = set()
            for item in getter(leaf):
                print("Adding item", item.full_c_method_name)
                names.add(item.full_c_method_name)
                yield item

            for inherit in leaf.inherits_full:
                print("Inherit", inherit)
                for item in getter(inherit):
                    print("Checking item", item.full_c_method_name)
                    if item.full_c_method_name not in names:
                        print("Adding item", item.full_c_method_name)
                        names.add(item.full_c_method_name)
                        yield item

        self.methods = [
            FuncItem(m, self.path, keys) for m in filter(mfilter, self.comp.methods)
        ]
        self._properties = [
            FuncItem(p, self.path, keys) for p in filter(mfilter, self.comp.properties)
        ]
        self.events = [EventItem(s, self.path, keys) for s in self.comp.events]

        if self.keys.funclist in (
            Function_List_Type.INHERITS,
            Function_List_Type.INHERITS_FULL,
        ):
            for eoclass in (
                self.comp.inherits
                if self.keys.funclist == Function_List_Type.INHERITS
                else self.comp.inherits_full
            ):
                for f in filter(mfilter, eoclass.methods):
                    self.methods.append(FuncItem(f, self.path, keys))
                for f in filter(mfilter, eoclass.properties):
                    self._properties.append(FuncItem(f, self.path, keys))
        elif self.keys.funclist == Function_List_Type.CLASS_IMPLEMENTS:
            for imp in comp.implements:

                if (
                    imp.namespace == self.name
                    or imp.short_name.lower() in self.keys.implementsbl
                ):
                    continue

                f = imp.function
                if f.type == Eolian_Function_Type.METHOD and mfilter(f):
                    self.methods.append(FuncItem(f, self.path, keys))
                if f.type in (
                    Eolian_Function_Type.PROPERTY,
                    Eolian_Function_Type.PROP_GET,
                    Eolian_Function_Type.PROP_SET,
                ) and mfilter(f):
                    self._properties.append(FuncItem(f, self.path, keys))

    @property
    def properties(self):
        return filter(lambda p: p.has_setter or p.has_getter, self._properties)

    @property
    def properties_get(self):
        return filter(lambda p: p.has_getter, self._properties)

    @property
    def properties_set(self):
        return filter(lambda p: p.has_setter, self._properties)

    def __iter__(self):
        return itertools.chain(self.methods, self.properties)


class SuiteGen(BaseItem):
    def __init__(self, name, testname, filename, path, template=None):
        keys = GetKey(os.path.splitext(filename)[1])
        super().__init__(path, keys, name + "_")
        self.name = name
        self.testname = testname
        self.fullname = "_".join([name, testname]) if testname else name
        self.filename = filename
        self.template = template
        self.clslist = []

        if not self.template:
            script_path = os.path.dirname(os.path.realpath(__file__))
            self.template = os.path.join(
                script_path, "testgenerator{}.template".format(self.keys.ext)
            )

    def __iter__(self):
        return iter(self.clslist)

    def type_convert(self, eotype):
        if eotype.type == eolian.Eolian_Type_Type.CLASS:
            return name_helpers.class_managed_name(eotype.class_)

        if eotype.typedecl:
            return name_helpers.type_managed_name(eotype)

        return self.keys.type_convert(eotype)

    def constructor_params(self, cls):
        ret = []

        constructors = itertools.chain(
            cls.constructors, *[base.constructors for base in cls.inherits_full]
        )

        for constructor in constructors:
            # Skip optional constructors for now
            if constructor.is_optional:
                continue
            func = constructor.function

            if func.type == eolian.Eolian_Function_Type.PROPERTY:
                first_param = list(func.setter_values)[0]
            else:
                first_param = list(func.parameters)[0]
            param_type = first_param.type

            ret.append("default({})".format(name_helpers.type_managed_name(param_type)))

        return (", " if ret else "") + ", ".join(ret)

    def print_arg(self, eoarg):
        return self.keys.print_arg(eoarg)

    def loadFiles(self, eolian_db, eofiles):
        self.clslist.clear()
        for eofile in eofiles:
            eocls = eolian_db.class_by_file_get(os.path.basename(eofile))
            if not eocls or eocls.type != Eolian_Class_Type.REGULAR:
                continue
            self.loadObj(eocls)

    def loadObj(self, eocls):
        cls = ClassItem(eocls, self.path, self.keys)
        if not os.path.isfile(cls.path):
            cls.myfullname = "{}_{}".format(self.fullname, cls.myname)
            self.clslist.append(cls)
        else:
            print("removing {} Class from generated list".format(cls.name))