forked from firebase/firebase-cpp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_android_test.py
executable file
·103 lines (87 loc) · 3.4 KB
/
generate_android_test.py
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
#!/usr/grte/v4/bin/python2.7
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Generate JUnit4 tests from gtest files.
This script reads a template and fills in test-specific information such as .so
library name and Java class name. This script also goes over the gtest files and
finds all test methods of the pattern TEST_F(..., ...) and converts each into a
@Test-annotated test method.
"""
# We will be open-source this. So please do not introduce google3 dependency
# unless absolutely necessary.
import argparse
import re
GTEST_METHOD_RE = (r'TEST(?:_F)?[(]\s*(?P<test_class>[A-Za-z]+)\s*,\s*'
r'(?P<test_method>[A-Za-z]+)\s*[)]')
JAVA_TEST_METHOD = r"""
@Test
public void {test_class}{test_method}() {{
run("{test_class}.{test_method}");
}}
"""
def generate_fragment(gtests):
"""Generate @Test-annotated test method code from the provided gtest files."""
fragments = []
gtest_method_pattern = re.compile(GTEST_METHOD_RE)
for gtest in gtests:
with open(gtest, 'r') as gtest_file:
gtest_code = gtest_file.read()
for matched in re.finditer(gtest_method_pattern, gtest_code):
fragments.append(
JAVA_TEST_METHOD.format(
test_class=matched.group('test_class'),
test_method=matched.group('test_method')))
return ''.join(fragments)
def generate_file(template, out, **kwargs):
"""Generate a Java file from the provided template and parameters."""
with open(template, 'r') as template_file:
template_string = template_file.read()
java_code = template_string.format(**kwargs)
with open(out, 'w') as out_file:
out_file.write(java_code)
def main():
parser = argparse.ArgumentParser(
description='Generates JUnit4 tests from gtest files.')
parser.add_argument(
'--template',
help='the filename of the template to use in the generation',
required=True)
parser.add_argument(
'--java_package',
help='which package test Java class belongs to',
required=True)
parser.add_argument(
'--java_class',
help='specifies the name of the class to generate',
required=True)
parser.add_argument(
'--so_lib',
help=('specifies the name of the native library without prefix lib and '
'suffix .so. You must compile the C++ test code together with the '
'firestore_android_test_main.cc as a shared library, say libfoo.so '
'and pass the name foo here.'),
required=True)
parser.add_argument('--out', help='the output file path', required=True)
parser.add_argument('srcs', nargs='+', help='the input gtest file paths')
args = parser.parse_args()
fragment = generate_fragment(args.srcs)
generate_file(
args.template,
args.out,
package_name=args.java_package,
java_class_name=args.java_class,
so_lib_name=args.so_lib,
tests=fragment)
if __name__ == '__main__':
main()