summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/genIPDLUnitTests.py
blob: 160b90a031c39a76d471146fff2ca83e86be8bb8 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import string
import sys


def usage():
    print(
        """
%s template_file -t unit_tests... -e extra_protocols...

  TEMPLATE_FILE is used to generate to generate the unit-tester .cpp
  UNIT_TESTS are the top-level protocols defining unit tests
  EXTRA_PROTOCOLS are top-level protocols for subprocesses that can be
                  spawned in tests but are not unit tests in and of
                  themselves
"""
        % (sys.argv[0]),
        file=sys.stderr,
    )
    sys.exit(1)


def main(argv):
    template = argv[1]

    if argv[2] != "-t":
        usage()
    i = 3
    unittests = []
    while argv[i] != "-e":
        unittests.append(argv[i])
        i += 1

    extras = argv[(i + 1) :]

    includes = "\n".join(['#include "%s.h"' % (t) for t in unittests])

    enum_values = "\n".join(["    %s," % (t) for t in unittests + extras])
    last_enum = unittests[-1]

    string_to_enums = "\n".join(
        [
            """    else if (!strcmp(aString, "%s"))
        return %s;"""
            % (t, t)
            for t in unittests + extras
        ]
    )

    enum_to_strings = "\n".join(
        [
            """    case %s:
        return "%s";"""
            % (t, t)
            for t in unittests + extras
        ]
    )

    parent_delete_cases = "\n".join(
        [
            """    case %s: {
           delete reinterpret_cast<%sParent*>(gParentActor);
           return;
       }
"""
            % (t, t)
            for t in unittests
        ]
    )

    parent_enabled_cases_proc = "\n".join(
        [
            """    case %s: {
        if (!%sParent::RunTestInProcesses()) {
            passed("N/A to proc");
            DeferredParentShutdown();
            return;
        }
        break;
       }
"""
            % (t, t)
            for t in unittests
        ]
    )

    parent_main_cases_proc = "\n".join(
        [
            """    case %s: {
        %sParent** parent =
        reinterpret_cast<%sParent**>(&gParentActor);
        *parent = new %sParent();
        (*parent)->Open(transport, child);
        return (*parent)->Main();
        }
"""
            % (t, t, t, t)
            for t in unittests
        ]
    )

    parent_enabled_cases_thread = "\n".join(
        [
            """    case %s: {
        if (!%sParent::RunTestInThreads()) {
            passed("N/A to threads");
            DeferredParentShutdown();
            return;
        }
        break;
       }
"""
            % (t, t)
            for t in unittests
        ]
    )

    parent_main_cases_thread = "\n".join(
        [
            """    case %s: {
        %sParent** parent =
        reinterpret_cast<%sParent**>(&gParentActor);
        *parent = new %sParent();

        %sChild** child =
        reinterpret_cast<%sChild**>(&gChildActor);
        *child = new %sChild();

        ::mozilla::ipc::MessageChannel *childChannel = (*child)->GetIPCChannel();
        ::mozilla::ipc::Side parentSide =
            ::mozilla::ipc::ParentSide;

        (*parent)->Open(childChannel, childMessageLoop, parentSide);
        return (*parent)->Main();
        }
"""
            % (t, t, t, t, t, t, t)
            for t in unittests
        ]
    )

    child_delete_cases = "\n".join(
        [
            """    case %s: {
           delete reinterpret_cast<%sChild*>(gChildActor);
           return;
       }
"""
            % (t, t)
            for t in unittests + extras
        ]
    )

    child_init_cases = "\n".join(
        [
            """    case %s: {
        %sChild** child =
            reinterpret_cast<%sChild**>(&gChildActor);
        *child = new %sChild();
        (*child)->Open(transport, parentPid, worker);
        return;
    }
"""
            % (t, t, t, t)
            for t in unittests + extras
        ]
    )

    templatefile = open(template, "r", encoding="utf-8")
    sys.stdout.write(
        string.Template(templatefile.read()).substitute(
            INCLUDES=includes,
            ENUM_VALUES=enum_values,
            LAST_ENUM=last_enum,
            STRING_TO_ENUMS=string_to_enums,
            ENUM_TO_STRINGS=enum_to_strings,
            PARENT_DELETE_CASES=parent_delete_cases,
            PARENT_ENABLED_CASES_PROC=parent_enabled_cases_proc,
            PARENT_MAIN_CASES_PROC=parent_main_cases_proc,
            PARENT_ENABLED_CASES_THREAD=parent_enabled_cases_thread,
            PARENT_MAIN_CASES_THREAD=parent_main_cases_thread,
            CHILD_DELETE_CASES=child_delete_cases,
            CHILD_INIT_CASES=child_init_cases,
        )
    )
    templatefile.close()


if __name__ == "__main__":
    main(sys.argv)