1#!/usr/bin/env python3 2# Copyright 2024 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Tests for gcc_preprocess.py 6 7This test suite contains various tests for the 'java_cpp_template' build rule, 8which uses the gcc preprocessor to turn a template into Java code. 9""" 10 11import unittest 12import tempfile 13 14import gcc_preprocess 15 16 17class TestPreprocess(unittest.TestCase): 18 19 def testParsePackageName(self): 20 with tempfile.NamedTemporaryFile(mode='w') as f: 21 template = f.name 22 f.file.write(""" 23package org.chromium.fake; 24public class Empty { 25} 26""") 27 f.file.flush() 28 package_name, data = gcc_preprocess.ProcessJavaFile(template, [], []) 29 self.assertEqual('org.chromium.fake', package_name) 30 self.assertEqual( 31 """ 32package org.chromium.fake; 33public class Empty { 34} 35""".strip(), data.strip()) 36 37 def testMissingPackageName(self): 38 with tempfile.NamedTemporaryFile(mode='w') as f: 39 template = f.name 40 f.file.write(""" 41public class Empty { 42} 43""") 44 f.file.flush() 45 with self.assertRaisesRegex(Exception, 46 r'Could not find java package of.*'): 47 gcc_preprocess.ProcessJavaFile(template, [], []) 48 49 def testSinglePreprocessorEvaluation(self): 50 with tempfile.NamedTemporaryFile(mode='w') as f: 51 template = f.name 52 f.file.write(""" 53package org.chromium.fake; 54public class Sample { 55#if defined(_ENABLE_ASSERTS) 56 public boolean ENABLE_ASSERTS = true; 57#else 58 public boolean ENABLE_ASSERTS = false; 59#endif 60} 61""") 62 f.file.flush() 63 defines = [ 64 '_ENABLE_ASSERTS', 65 ] 66 package_name, data = gcc_preprocess.ProcessJavaFile(template, defines, []) 67 self.assertEqual('org.chromium.fake', package_name) 68 self.assertEqual( 69 """ 70package org.chromium.fake; 71public class Sample { 72 public boolean ENABLE_ASSERTS = true; 73} 74""".strip(), data.strip()) 75 76 def testNestedPreprocessorEvaluation(self): 77 with tempfile.NamedTemporaryFile(mode='w') as f: 78 template = f.name 79 f.file.write(""" 80package org.chromium.fake; 81#if defined(USE_FINAL) 82#define MAYBE_FINAL final 83#else 84#define MAYBE_FINAL 85#endif 86public class Sample { 87#if defined(_ENABLE_ASSERTS) 88 public MAYBE_FINAL boolean ENABLE_ASSERTS = true; 89#else 90 public MAYBE_FINAL boolean ENABLE_ASSERTS = false; 91#endif 92} 93""") 94 f.file.flush() 95 defines = [ 96 '_ENABLE_ASSERTS', 97 'USE_FINAL', 98 ] 99 package_name, data = gcc_preprocess.ProcessJavaFile(template, defines, []) 100 self.assertEqual('org.chromium.fake', package_name) 101 self.assertEqual( 102 """ 103package org.chromium.fake; 104public class Sample { 105 public final boolean ENABLE_ASSERTS = true; 106} 107""".strip(), data.strip()) 108 109 def testPreserveComments(self): 110 with tempfile.NamedTemporaryFile(mode='w') as f: 111 template = f.name 112 f.file.write(""" 113// Copyright header ... 114package org.chromium.fake; 115/** 116 * Some javadoc. 117 */ 118public class Sample { 119 // This is a comment outside the #if block. 120#if defined(_ENABLE_ASSERTS) 121 // Inside the #if block. 122 public boolean ENABLE_ASSERTS = true; 123#else 124 // Inside the #else block. 125 public boolean ENABLE_ASSERTS = false; 126#endif 127} 128""") 129 f.file.flush() 130 defines = [ 131 '_ENABLE_ASSERTS', 132 ] 133 package_name, data = gcc_preprocess.ProcessJavaFile(template, defines, []) 134 self.assertEqual('org.chromium.fake', package_name) 135 self.assertEqual( 136 """ 137// Copyright header ... 138package org.chromium.fake; 139/** 140 * Some javadoc. 141 */ 142public class Sample { 143 // This is a comment outside the #if block. 144 // Inside the #if block. 145 public boolean ENABLE_ASSERTS = true; 146} 147""".strip(), data.strip()) 148 149 150if __name__ == '__main__': 151 unittest.main() 152