1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/ipdl/test/cxx/genIPDLUnitTests.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 1.4 +# This Source Code Form is subject to the terms of the Mozilla Public 1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + 1.8 +import string, sys 1.9 + 1.10 +def usage(): 1.11 + print >>sys.stderr, """ 1.12 +%s template_file -t unit_tests... -e extra_protocols... 1.13 + 1.14 + TEMPLATE_FILE is used to generate to generate the unit-tester .cpp 1.15 + UNIT_TESTS are the top-level protocols defining unit tests 1.16 + EXTRA_PROTOCOLS are top-level protocols for subprocesses that can be 1.17 + spawned in tests but are not unit tests in and of 1.18 + themselves 1.19 +"""% (sys.argv[0]) 1.20 + sys.exit(1) 1.21 + 1.22 +def main(argv): 1.23 + template = argv[1] 1.24 + 1.25 + if argv[2] != '-t': usage() 1.26 + i = 3 1.27 + unittests = [] 1.28 + while argv[i] != '-e': 1.29 + unittests.append(argv[i]) 1.30 + i += 1 1.31 + 1.32 + extras = argv[(i+1):] 1.33 + 1.34 + includes = '\n'.join([ 1.35 + '#include "%s.h"'% (t) for t in unittests ]) 1.36 + 1.37 + 1.38 + enum_values = '\n'.join([ 1.39 + ' %s,'% (t) for t in unittests+extras ]) 1.40 + last_enum = unittests[-1] 1.41 + 1.42 + 1.43 + string_to_enums = '\n'.join([ 1.44 + ''' else if (!strcmp(aString, "%s")) 1.45 + return %s;'''% (t, t) for t in unittests+extras ]) 1.46 + 1.47 + enum_to_strings = '\n'.join([ 1.48 + ''' case %s: 1.49 + return "%s";'''%(t, t) for t in unittests+extras ]) 1.50 + 1.51 + parent_delete_cases = '\n'.join([ 1.52 +''' case %s: { 1.53 + delete reinterpret_cast<%sParent*>(gParentActor); 1.54 + return; 1.55 + } 1.56 +'''% (t, t) for t in unittests ]) 1.57 + 1.58 + parent_enabled_cases_proc = '\n'.join([ 1.59 +''' case %s: { 1.60 + if (!%sParent::RunTestInProcesses()) { 1.61 + passed("N/A to proc"); 1.62 + DeferredParentShutdown(); 1.63 + return; 1.64 + } 1.65 + break; 1.66 + } 1.67 +''' % (t, t) for t in unittests ]) 1.68 + 1.69 + parent_main_cases_proc = '\n'.join([ 1.70 +''' case %s: { 1.71 + %sParent** parent = 1.72 + reinterpret_cast<%sParent**>(&gParentActor); 1.73 + *parent = new %sParent(); 1.74 + (*parent)->Open(transport, child); 1.75 + return (*parent)->Main(); 1.76 + } 1.77 +'''% (t, t, t, t) for t in unittests ]) 1.78 + 1.79 + parent_enabled_cases_thread = '\n'.join([ 1.80 +''' case %s: { 1.81 + if (!%sParent::RunTestInThreads()) { 1.82 + passed("N/A to threads"); 1.83 + DeferredParentShutdown(); 1.84 + return; 1.85 + } 1.86 + break; 1.87 + } 1.88 +''' % (t, t) for t in unittests ]) 1.89 + 1.90 + parent_main_cases_thread = '\n'.join([ 1.91 +''' case %s: { 1.92 + %sParent** parent = 1.93 + reinterpret_cast<%sParent**>(&gParentActor); 1.94 + *parent = new %sParent(); 1.95 + 1.96 + %sChild** child = 1.97 + reinterpret_cast<%sChild**>(&gChildActor); 1.98 + *child = new %sChild(); 1.99 + 1.100 + ::mozilla::ipc::MessageChannel *childChannel = (*child)->GetIPCChannel(); 1.101 + ::mozilla::ipc::Side parentSide = 1.102 + ::mozilla::ipc::ParentSide; 1.103 + 1.104 + (*parent)->Open(childChannel, childMessageLoop, parentSide); 1.105 + return (*parent)->Main(); 1.106 + } 1.107 +'''% (t, t, t, t, t, t, t) for t in unittests ]) 1.108 + 1.109 + child_delete_cases = '\n'.join([ 1.110 +''' case %s: { 1.111 + delete reinterpret_cast<%sChild*>(gChildActor); 1.112 + return; 1.113 + } 1.114 +'''% (t, t) for t in unittests+extras ]) 1.115 + 1.116 + 1.117 + child_init_cases = '\n'.join([ 1.118 +''' case %s: { 1.119 + %sChild** child = 1.120 + reinterpret_cast<%sChild**>(&gChildActor); 1.121 + *child = new %sChild(); 1.122 + (*child)->Open(transport, parent, worker); 1.123 + return; 1.124 + } 1.125 +'''% (t, t, t, t) for t in unittests+extras ]) 1.126 + 1.127 + templatefile = open(template, 'r') 1.128 + sys.stdout.write( 1.129 + string.Template(templatefile.read()).substitute( 1.130 + INCLUDES=includes, 1.131 + ENUM_VALUES=enum_values, LAST_ENUM=last_enum, 1.132 + STRING_TO_ENUMS=string_to_enums, 1.133 + ENUM_TO_STRINGS=enum_to_strings, 1.134 + PARENT_DELETE_CASES=parent_delete_cases, 1.135 + PARENT_ENABLED_CASES_PROC=parent_enabled_cases_proc, 1.136 + PARENT_MAIN_CASES_PROC=parent_main_cases_proc, 1.137 + PARENT_ENABLED_CASES_THREAD=parent_enabled_cases_thread, 1.138 + PARENT_MAIN_CASES_THREAD=parent_main_cases_thread, 1.139 + CHILD_DELETE_CASES=child_delete_cases, 1.140 + CHILD_INIT_CASES=child_init_cases)) 1.141 + templatefile.close() 1.142 + 1.143 +if __name__ == '__main__': 1.144 + main(sys.argv)