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
build / android / gyp / gcc_preprocess_tests.py [blame]
#!/usr/bin/env python3
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for gcc_preprocess.py
This test suite contains various tests for the 'java_cpp_template' build rule,
which uses the gcc preprocessor to turn a template into Java code.
"""
import unittest
import tempfile
import gcc_preprocess
class TestPreprocess(unittest.TestCase):
def testParsePackageName(self):
with tempfile.NamedTemporaryFile(mode='w') as f:
template = f.name
f.file.write("""
package org.chromium.fake;
public class Empty {
}
""")
f.file.flush()
package_name, data = gcc_preprocess.ProcessJavaFile(template, [], [])
self.assertEqual('org.chromium.fake', package_name)
self.assertEqual(
"""
package org.chromium.fake;
public class Empty {
}
""".strip(), data.strip())
def testMissingPackageName(self):
with tempfile.NamedTemporaryFile(mode='w') as f:
template = f.name
f.file.write("""
public class Empty {
}
""")
f.file.flush()
with self.assertRaisesRegex(Exception,
r'Could not find java package of.*'):
gcc_preprocess.ProcessJavaFile(template, [], [])
def testSinglePreprocessorEvaluation(self):
with tempfile.NamedTemporaryFile(mode='w') as f:
template = f.name
f.file.write("""
package org.chromium.fake;
public class Sample {
#if defined(_ENABLE_ASSERTS)
public boolean ENABLE_ASSERTS = true;
#else
public boolean ENABLE_ASSERTS = false;
#endif
}
""")
f.file.flush()
defines = [
'_ENABLE_ASSERTS',
]
package_name, data = gcc_preprocess.ProcessJavaFile(template, defines, [])
self.assertEqual('org.chromium.fake', package_name)
self.assertEqual(
"""
package org.chromium.fake;
public class Sample {
public boolean ENABLE_ASSERTS = true;
}
""".strip(), data.strip())
def testNestedPreprocessorEvaluation(self):
with tempfile.NamedTemporaryFile(mode='w') as f:
template = f.name
f.file.write("""
package org.chromium.fake;
#if defined(USE_FINAL)
#define MAYBE_FINAL final
#else
#define MAYBE_FINAL
#endif
public class Sample {
#if defined(_ENABLE_ASSERTS)
public MAYBE_FINAL boolean ENABLE_ASSERTS = true;
#else
public MAYBE_FINAL boolean ENABLE_ASSERTS = false;
#endif
}
""")
f.file.flush()
defines = [
'_ENABLE_ASSERTS',
'USE_FINAL',
]
package_name, data = gcc_preprocess.ProcessJavaFile(template, defines, [])
self.assertEqual('org.chromium.fake', package_name)
self.assertEqual(
"""
package org.chromium.fake;
public class Sample {
public final boolean ENABLE_ASSERTS = true;
}
""".strip(), data.strip())
def testPreserveComments(self):
with tempfile.NamedTemporaryFile(mode='w') as f:
template = f.name
f.file.write("""
// Copyright header ...
package org.chromium.fake;
/**
* Some javadoc.
*/
public class Sample {
// This is a comment outside the #if block.
#if defined(_ENABLE_ASSERTS)
// Inside the #if block.
public boolean ENABLE_ASSERTS = true;
#else
// Inside the #else block.
public boolean ENABLE_ASSERTS = false;
#endif
}
""")
f.file.flush()
defines = [
'_ENABLE_ASSERTS',
]
package_name, data = gcc_preprocess.ProcessJavaFile(template, defines, [])
self.assertEqual('org.chromium.fake', package_name)
self.assertEqual(
"""
// Copyright header ...
package org.chromium.fake;
/**
* Some javadoc.
*/
public class Sample {
// This is a comment outside the #if block.
// Inside the #if block.
public boolean ENABLE_ASSERTS = true;
}
""".strip(), data.strip())
if __name__ == '__main__':
unittest.main()