File indexing completed on 2024-05-12 04:39:29
0001 /* 0002 SPDX-FileCopyrightText: 2006 Matt Rogers <mattr@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "cmakeparsertest.h" 0008 0009 #include <QTemporaryFile> 0010 #include "cmListFileLexer.h" 0011 #include "cmakelistsparser.h" 0012 0013 CMakeParserTest::CMakeParserTest() 0014 {} 0015 0016 CMakeParserTest::~CMakeParserTest() 0017 {} 0018 0019 void CMakeParserTest::testLexerCreation() 0020 { 0021 cmListFileLexer* lexer = cmListFileLexer_New(); 0022 QVERIFY( lexer != nullptr ); 0023 cmListFileLexer_Delete( lexer ); 0024 } 0025 0026 void CMakeParserTest::testLexerWithFile() 0027 { 0028 QTemporaryFile tempFile; 0029 tempFile.setAutoRemove( false ); 0030 tempFile.open(); 0031 if ( !QFile::exists( tempFile.fileName() ) ) 0032 QFAIL( "Unable to open temporary file" ); 0033 0034 QString tempName = tempFile.fileName(); 0035 tempFile.close(); //hacks to the get name of the file 0036 0037 cmListFileLexer* lexer = cmListFileLexer_New(); 0038 if ( !lexer ) 0039 QFAIL( "unable to create lexer" ); 0040 QVERIFY( cmListFileLexer_SetFileName( lexer, qPrintable( tempName ), nullptr ) ); 0041 cmListFileLexer_Delete( lexer ); 0042 tempFile.remove(); 0043 } 0044 0045 void CMakeParserTest::testParserWithGoodData() 0046 { 0047 // QFAIL( "the magic is missing" ); 0048 QFETCH( QString, text ); 0049 QTemporaryFile tempFile; 0050 tempFile.setAutoRemove( false ); 0051 tempFile.open(); 0052 if ( !QFile::exists( tempFile.fileName() ) ) 0053 QFAIL( "Unable to open temporary file" ); 0054 0055 tempFile.write( text.toUtf8() ); 0056 QString tempName = tempFile.fileName(); 0057 tempFile.close(); //hacks to the get name of the file 0058 const bool parseError = CMakeListsParser::readCMakeFile( tempName ).isEmpty(); 0059 QVERIFY( parseError == false ); 0060 tempFile.remove(); 0061 } 0062 0063 void CMakeParserTest::testParserWithGoodData_data() 0064 { 0065 QTest::addColumn<QString>( "text" ); 0066 QTest::newRow( "good data1" ) << "project(foo)\nset(foobar_SRCS foo.h foo.c)"; 0067 QTest::newRow( "good data2" ) << "set(foobar_SRCS foo.h foo.c)\n" 0068 "add_executable( foo ${foobar_SRCS})"; 0069 QTest::newRow( "test data" ) << "add_test(mytest \"mytest\")\n"; 0070 } 0071 0072 void CMakeParserTest::testParserWithBadData() 0073 { 0074 QFETCH( QString, text ); 0075 QTemporaryFile tempFile; 0076 tempFile.setAutoRemove( false ); 0077 tempFile.open(); 0078 if ( !QFile::exists( tempFile.fileName() ) ) 0079 QFAIL( "Unable to open temporary file" ); 0080 0081 tempFile.write( text.toUtf8() ); 0082 tempFile.close(); //hacks to the get name of the file 0083 // CMakeAst* ast = new CMakeAst; 0084 // QString tempName = tempFile.fileName(); 0085 // bool parseError = CMakeListsParser::parseCMakeFile( ast, qPrintable( tempName ) ); 0086 // delete ast; 0087 // QVERIFY( parseError == true ); 0088 tempFile.remove(); 0089 } 0090 0091 void CMakeParserTest::testParserWithBadData_data() 0092 { 0093 QTest::addColumn<QString>( "text" ); 0094 0095 //just plain wrong. :) 0096 QTest::newRow( "bad data 1" ) << "foo bar baz zippedy do dah"; 0097 0098 //missing right parenthesis 0099 QTest::newRow( "bad data 2" ) << "set(mysrcs_SRCS foo.c\n\n\n"; 0100 0101 //extra identifiers between functions. cmake doesn't allow plain identifiers 0102 QTest::newRow( "bad data 3" ) << "the(quick) brown fox jumped(over) the lazy dog"; 0103 0104 //invalid due to no newline before next command 0105 QTest::newRow( "bad data 4" ) << "project(foo) set(mysrcs_SRCS foo.c)"; 0106 } 0107 0108 // void CMakeParserTest::testAstCreation() 0109 // { 0110 0111 // } 0112 0113 QTEST_GUILESS_MAIN( CMakeParserTest ) 0114 0115 #include "moc_cmakeparsertest.cpp"