|
1 # This Source Code Form is subject to the terms of the Mozilla Public |
|
2 # License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
|
4 |
|
5 import string, sys |
|
6 |
|
7 def usage(): |
|
8 print >>sys.stderr, """ |
|
9 %s template_file -t unit_tests... -e extra_protocols... |
|
10 |
|
11 TEMPLATE_FILE is used to generate to generate the unit-tester .cpp |
|
12 UNIT_TESTS are the top-level protocols defining unit tests |
|
13 EXTRA_PROTOCOLS are top-level protocols for subprocesses that can be |
|
14 spawned in tests but are not unit tests in and of |
|
15 themselves |
|
16 """% (sys.argv[0]) |
|
17 sys.exit(1) |
|
18 |
|
19 def main(argv): |
|
20 template = argv[1] |
|
21 |
|
22 if argv[2] != '-t': usage() |
|
23 i = 3 |
|
24 unittests = [] |
|
25 while argv[i] != '-e': |
|
26 unittests.append(argv[i]) |
|
27 i += 1 |
|
28 |
|
29 extras = argv[(i+1):] |
|
30 |
|
31 includes = '\n'.join([ |
|
32 '#include "%s.h"'% (t) for t in unittests ]) |
|
33 |
|
34 |
|
35 enum_values = '\n'.join([ |
|
36 ' %s,'% (t) for t in unittests+extras ]) |
|
37 last_enum = unittests[-1] |
|
38 |
|
39 |
|
40 string_to_enums = '\n'.join([ |
|
41 ''' else if (!strcmp(aString, "%s")) |
|
42 return %s;'''% (t, t) for t in unittests+extras ]) |
|
43 |
|
44 enum_to_strings = '\n'.join([ |
|
45 ''' case %s: |
|
46 return "%s";'''%(t, t) for t in unittests+extras ]) |
|
47 |
|
48 parent_delete_cases = '\n'.join([ |
|
49 ''' case %s: { |
|
50 delete reinterpret_cast<%sParent*>(gParentActor); |
|
51 return; |
|
52 } |
|
53 '''% (t, t) for t in unittests ]) |
|
54 |
|
55 parent_enabled_cases_proc = '\n'.join([ |
|
56 ''' case %s: { |
|
57 if (!%sParent::RunTestInProcesses()) { |
|
58 passed("N/A to proc"); |
|
59 DeferredParentShutdown(); |
|
60 return; |
|
61 } |
|
62 break; |
|
63 } |
|
64 ''' % (t, t) for t in unittests ]) |
|
65 |
|
66 parent_main_cases_proc = '\n'.join([ |
|
67 ''' case %s: { |
|
68 %sParent** parent = |
|
69 reinterpret_cast<%sParent**>(&gParentActor); |
|
70 *parent = new %sParent(); |
|
71 (*parent)->Open(transport, child); |
|
72 return (*parent)->Main(); |
|
73 } |
|
74 '''% (t, t, t, t) for t in unittests ]) |
|
75 |
|
76 parent_enabled_cases_thread = '\n'.join([ |
|
77 ''' case %s: { |
|
78 if (!%sParent::RunTestInThreads()) { |
|
79 passed("N/A to threads"); |
|
80 DeferredParentShutdown(); |
|
81 return; |
|
82 } |
|
83 break; |
|
84 } |
|
85 ''' % (t, t) for t in unittests ]) |
|
86 |
|
87 parent_main_cases_thread = '\n'.join([ |
|
88 ''' case %s: { |
|
89 %sParent** parent = |
|
90 reinterpret_cast<%sParent**>(&gParentActor); |
|
91 *parent = new %sParent(); |
|
92 |
|
93 %sChild** child = |
|
94 reinterpret_cast<%sChild**>(&gChildActor); |
|
95 *child = new %sChild(); |
|
96 |
|
97 ::mozilla::ipc::MessageChannel *childChannel = (*child)->GetIPCChannel(); |
|
98 ::mozilla::ipc::Side parentSide = |
|
99 ::mozilla::ipc::ParentSide; |
|
100 |
|
101 (*parent)->Open(childChannel, childMessageLoop, parentSide); |
|
102 return (*parent)->Main(); |
|
103 } |
|
104 '''% (t, t, t, t, t, t, t) for t in unittests ]) |
|
105 |
|
106 child_delete_cases = '\n'.join([ |
|
107 ''' case %s: { |
|
108 delete reinterpret_cast<%sChild*>(gChildActor); |
|
109 return; |
|
110 } |
|
111 '''% (t, t) for t in unittests+extras ]) |
|
112 |
|
113 |
|
114 child_init_cases = '\n'.join([ |
|
115 ''' case %s: { |
|
116 %sChild** child = |
|
117 reinterpret_cast<%sChild**>(&gChildActor); |
|
118 *child = new %sChild(); |
|
119 (*child)->Open(transport, parent, worker); |
|
120 return; |
|
121 } |
|
122 '''% (t, t, t, t) for t in unittests+extras ]) |
|
123 |
|
124 templatefile = open(template, 'r') |
|
125 sys.stdout.write( |
|
126 string.Template(templatefile.read()).substitute( |
|
127 INCLUDES=includes, |
|
128 ENUM_VALUES=enum_values, LAST_ENUM=last_enum, |
|
129 STRING_TO_ENUMS=string_to_enums, |
|
130 ENUM_TO_STRINGS=enum_to_strings, |
|
131 PARENT_DELETE_CASES=parent_delete_cases, |
|
132 PARENT_ENABLED_CASES_PROC=parent_enabled_cases_proc, |
|
133 PARENT_MAIN_CASES_PROC=parent_main_cases_proc, |
|
134 PARENT_ENABLED_CASES_THREAD=parent_enabled_cases_thread, |
|
135 PARENT_MAIN_CASES_THREAD=parent_main_cases_thread, |
|
136 CHILD_DELETE_CASES=child_delete_cases, |
|
137 CHILD_INIT_CASES=child_init_cases)) |
|
138 templatefile.close() |
|
139 |
|
140 if __name__ == '__main__': |
|
141 main(sys.argv) |