File indexing completed on 2024-09-08 04:19:11
0001 #!env python3 0002 # -*- coding: utf-8 -*- 0003 # Copyright 2014 Simon Edwards <simon@simonzone.com> 0004 # 0005 # This program is free software; you can redistribute it and/or modify 0006 # it under the terms of the GNU General Public License as published by 0007 # the Free Software Foundation; either version 2 of the License, or 0008 # (at your option) any later version. 0009 # 0010 # This program is distributed in the hope that it will be useful, 0011 # but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 # GNU General Public License for more details. 0014 # 0015 # You should have received a copy of the GNU General Public License 0016 # along with this program; if not, write to the 0017 # Free Software Foundation, Inc., 0018 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 0019 0020 import unittest 0021 import cmakeparser 0022 0023 class TestCMakeParser(unittest.TestCase): 0024 0025 def setUp(self): 0026 self.parser = cmakeparser.CMakeParser() 0027 0028 def testComment(self): 0029 self.assertTrue(len(self.parser.parse("""# Just a comment 0030 """))==0, "Comment failed.") 0031 0032 def testCommentSpace(self): 0033 self.assertTrue(len(self.parser.parse(""" 0034 # Just a comment 0035 0036 """))==0, "Comment space failed.") 0037 0038 def testEmptyStatement(self): 0039 result = self.parser.parse("""endif()""") 0040 self.assertEqual(len(result), 1) 0041 self.assertEqual(result[0].command(), "endif") 0042 self.assertEqual(len(result[0].arguments()), 0) 0043 0044 def testStatement(self): 0045 result = self.parser.parse("""project(pykde5) 0046 """) 0047 self.assertEqual(len(result), 1) 0048 self.assertEqual(result[0].command(), "project") 0049 0050 def testStatement2(self): 0051 result = self.parser.parse("""project(pykde5) # just a comment 0052 """) 0053 self.assertEqual(len(result), 1) 0054 self.assertEqual(result[0].command(), "project") 0055 0056 def testStatement3(self): 0057 result = self.parser.parse("""cmake_policy(SET CMP0002 OLD)""") 0058 self.assertEqual(len(result), 1) 0059 self.assertEqual(result[0].command(), "cmake_policy") 0060 self.assertEqual(len(result[0].arguments()), 3) 0061 0062 def testStatement4(self): 0063 result = self.parser.parse("""cmake_policy ( SET CMP0002 OLD ) """) 0064 self.assertEqual(len(result), 1) 0065 self.assertEqual(result[0].command(), "cmake_policy") 0066 self.assertEqual(len(result[0].arguments()), 3) 0067 0068 def testStatement5(self): 0069 result = self.parser.parse("""include_directories( 0070 ${SIP_INCLUDE_DIR} 0071 ${qt5core_include} 0072 ${qt5gui_include} 0073 ${qt5widgets_include} 0074 ) 0075 """) 0076 self.assertEqual(len(result), 1) 0077 self.assertEqual(result[0].command(), "include_directories") 0078 self.assertEqual(len(result[0].arguments()), 4) 0079 0080 def testStatement6(self): 0081 result = self.parser.parse("""include_directories( 0082 ${SIP_INCLUDE_DIR} 0083 #${qt5core_include} 0084 ${qt5gui_include} 0085 ${qt5widgets_include} 0086 ) 0087 """) 0088 self.assertEqual(len(result), 1) 0089 self.assertEqual(result[0].command(), "include_directories") 0090 self.assertEqual(len(result[0].arguments()), 3) 0091 0092 0093 def testString(self): 0094 result = self.parser.parse(""" 0095 set_package_properties(PythonLibrary PROPERTIES DESCRIPTION 0096 "The Python Library" URL "http://www.python.org" 0097 TYPE REQUIRED PURPOSE "Required to build PyKDE4") 0098 """) 0099 # print(repr(result)) 0100 0101 def testString2(self): 0102 result = self.parser.parse(""" 0103 set_package_properties(PythonLibrary PROPERTIES DESCRIPTION 0104 "The Python Library" URL "http://www.python.org" 0105 TYPE REQUIRED PURPOSE "Required to build PyKDE4. 0106 And some muliline stuff.") 0107 """) 0108 # print(repr(result)) 0109 0110 def testString3(self): 0111 result = self.parser.parse(""" 0112 check_cxx_source_compiles(" #ifdef __SUNPRO_CC 0113 #define __asm__ asm 0114 #endif 0115 int main() { __asm__(\\"pxor %mm0, %mm0\\") ; }" HAVE_X86_MMX) 0116 """) 0117 # print(repr(result)) 0118 self.assertEqual(len(result[0].arguments()), 2) 0119 0120 def testString4(self): 0121 result = self.parser.parse(r""" 0122 target_compile_definitions(${BACKEND_TEST_TARGET} PUBLIC -DKDIRWATCH_TEST_METHOD=\"${_backendName}\") 0123 """, debug=0) 0124 #print(repr(result)) 0125 #self.assertEqual(len(result[0].arguments()), 2) 0126 0127 0128 def testNesting(self): 0129 result = self.parser.parse(""" 0130 if(MSVC OR (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) 0131 """) 0132 self.assertEqual(len(result[0].arguments()), 9) 0133 0134 def testSolidCmake(self): 0135 result = self.parser.parse( 0136 """ 0137 include (CheckCXXSourceCompiles) 0138 0139 if(MSVC OR (WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) 0140 check_cxx_source_compiles("int main() { __asm { pxor mm0, mm0 }; }" HAVE_X86_MMX) 0141 else() 0142 check_cxx_source_compiles(" #ifdef __SUNPRO_CC 0143 #define __asm__ asm 0144 #endif 0145 int main() { __asm__(\\"pxor %mm0, %mm0\\") ; }" HAVE_X86_MMX) 0146 endif() 0147 """) 0148 0149 if __name__ == '__main__': 0150 unittest.main()