2022-08-19 15:59:36 +02:00
// Copyright (C) 2016 The Qt Company Ltd.
2022-12-21 10:12:09 +01:00
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
2010-11-01 17:39:28 +01:00
2013-03-27 22:36:45 +01:00
# include <utils/filesearch.h>
2010-11-01 17:39:28 +01:00
2020-10-30 12:11:32 +01:00
# include <QTextCodec>
2012-02-15 10:42:41 +01:00
# include <QtTest>
2010-11-01 17:39:28 +01:00
2022-09-05 19:06:37 +02:00
using namespace Utils ;
2010-11-01 17:39:28 +01:00
class tst_FileSearch : public QObject
{
Q_OBJECT
2013-06-13 16:37:14 +02:00
public :
enum RegExpFlag {
NoRegExp ,
RegExp
} ;
2010-11-01 17:39:28 +01:00
private slots :
void multipleResults ( ) ;
void caseSensitive ( ) ;
void caseInSensitive ( ) ;
2012-11-30 16:15:07 +01:00
void matchCaseReplacement ( ) ;
2023-05-05 14:05:10 +02:00
private :
const FilePath m_filePath = FilePath : : fromString ( " :/tst_filesearch/testfile.txt " ) ;
2010-11-01 17:39:28 +01:00
} ;
2023-05-05 14:05:10 +02:00
SearchResultItem searchResult ( const FilePath & fileName , const QString & matchingLine ,
int lineNumber , int matchStart , int matchLength ,
const QStringList & regexpCapturedTexts = { } )
{
SearchResultItem result ;
result . setFilePath ( fileName ) ;
result . setLineText ( matchingLine ) ;
result . setMainRange ( lineNumber , matchStart , matchLength ) ;
result . setUserData ( regexpCapturedTexts ) ;
result . setUseTextEditorFont ( true ) ;
return result ;
}
void test_helper ( const FilePath & filePath , const SearchResultItems & expectedResults ,
const QString & term , QTextDocument : : FindFlags flags = { } ,
tst_FileSearch : : RegExpFlag regexp = tst_FileSearch : : NoRegExp )
{
FileIterator * it = new FileListIterator ( { filePath } , { QTextCodec : : codecForLocale ( ) } ) ;
QFutureWatcher < SearchResultItems > watcher ;
QSignalSpy ready ( & watcher , & QFutureWatcherBase : : resultsReadyAt ) ;
if ( regexp = = tst_FileSearch : : NoRegExp )
watcher . setFuture ( Utils : : findInFiles ( term , it , flags ) ) ;
else
watcher . setFuture ( Utils : : findInFilesRegExp ( term , it , flags ) ) ;
watcher . future ( ) . waitForFinished ( ) ;
QTest : : qWait ( 100 ) ; // process events
QCOMPARE ( ready . count ( ) , 1 ) ;
SearchResultItems results = watcher . resultAt ( 0 ) ;
QCOMPARE ( results . count ( ) , expectedResults . count ( ) ) ;
for ( int i = 0 ; i < expectedResults . size ( ) ; + + i )
QCOMPARE ( results . at ( i ) , expectedResults . at ( i ) ) ;
2010-11-01 17:39:28 +01:00
}
void tst_FileSearch : : multipleResults ( )
{
2023-05-05 14:05:10 +02:00
SearchResultItems expectedResults ;
expectedResults < < searchResult ( m_filePath , " search to find multiple find results " , 2 , 10 , 4 ) ;
expectedResults < < searchResult ( m_filePath , " search to find multiple find results " , 2 , 24 , 4 ) ;
expectedResults < < searchResult ( m_filePath , " here you find another result " , 4 , 9 , 4 ) ;
test_helper ( m_filePath , expectedResults , " find " ) ;
2013-06-13 16:37:14 +02:00
expectedResults . clear ( ) ;
2023-05-05 14:05:10 +02:00
expectedResults < < searchResult ( m_filePath ,
" aaaaaaaa this line has 2 results for four a in a row " ,
5 , 0 , 4 ) ;
expectedResults < < searchResult ( m_filePath ,
" aaaaaaaa this line has 2 results for four a in a row " ,
5 , 4 , 4 ) ;
test_helper ( m_filePath , expectedResults , " aaaa " ) ;
2013-06-13 16:37:14 +02:00
expectedResults . clear ( ) ;
2023-05-05 14:05:10 +02:00
expectedResults < < searchResult ( m_filePath ,
" aaaaaaaa this line has 2 results for four a in a row " ,
5 , 0 , 4 , { " aaaa " } ) ;
expectedResults < < searchResult ( m_filePath ,
" aaaaaaaa this line has 2 results for four a in a row " ,
5 , 4 , 4 , { " aaaa " } ) ;
test_helper ( m_filePath , expectedResults , " aaaa " , { } , RegExp ) ;
2010-11-01 17:39:28 +01:00
}
void tst_FileSearch : : caseSensitive ( )
{
2023-05-05 14:05:10 +02:00
SearchResultItems expectedResults ;
expectedResults < < searchResult ( m_filePath , " search CaseSensitively for casesensitive " ,
3 , 7 , 13 ) ;
test_helper ( m_filePath , expectedResults , " CaseSensitive " , QTextDocument : : FindCaseSensitively ) ;
2010-11-01 17:39:28 +01:00
}
void tst_FileSearch : : caseInSensitive ( )
{
2023-05-05 14:05:10 +02:00
SearchResultItems expectedResults ;
expectedResults < < searchResult ( m_filePath , " search CaseSensitively for casesensitive " , 3 , 7 , 13 ) ;
expectedResults < < searchResult ( m_filePath , " search CaseSensitively for casesensitive " , 3 , 27 , 13 ) ;
test_helper ( m_filePath , expectedResults , " CaseSensitive " ) ;
2010-11-01 17:39:28 +01:00
}
2012-11-30 16:15:07 +01:00
void tst_FileSearch : : matchCaseReplacement ( )
{
QCOMPARE ( Utils : : matchCaseReplacement ( " " , " foobar " ) , QString ( " foobar " ) ) ; //empty string
QCOMPARE ( Utils : : matchCaseReplacement ( " testpad " , " foobar " ) , QString ( " foobar " ) ) ; //lower case
QCOMPARE ( Utils : : matchCaseReplacement ( " TESTPAD " , " foobar " ) , QString ( " FOOBAR " ) ) ; //upper case
QCOMPARE ( Utils : : matchCaseReplacement ( " Testpad " , " foobar " ) , QString ( " Foobar " ) ) ; //capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " tESTPAD " , " foobar " ) , QString ( " fOOBAR " ) ) ; //un-capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " tEsTpAd " , " foobar " ) , QString ( " foobar " ) ) ; //mixed case, use replacement as specified
QCOMPARE ( Utils : : matchCaseReplacement ( " TeStPaD " , " foobar " ) , QString ( " foobar " ) ) ; //mixed case, use replacement as specified
QCOMPARE ( Utils : : matchCaseReplacement ( " testpad " , " fooBar " ) , QString ( " foobar " ) ) ; //lower case
QCOMPARE ( Utils : : matchCaseReplacement ( " TESTPAD " , " fooBar " ) , QString ( " FOOBAR " ) ) ; //upper case
QCOMPARE ( Utils : : matchCaseReplacement ( " Testpad " , " fooBar " ) , QString ( " Foobar " ) ) ; //capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " tESTPAD " , " fooBar " ) , QString ( " fOOBAR " ) ) ; //un-capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " tEsTpAd " , " fooBar " ) , QString ( " fooBar " ) ) ; //mixed case, use replacement as specified
QCOMPARE ( Utils : : matchCaseReplacement ( " TeStPaD " , " fooBar " ) , QString ( " fooBar " ) ) ; //mixed case, use replacement as specified
//with common prefix
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXtestpad " , " prefixfoobar " ) , QString ( " pReFiXfoobar " ) ) ; //lower case
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXTESTPAD " , " prefixfoobar " ) , QString ( " pReFiXFOOBAR " ) ) ; //upper case
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXTestpad " , " prefixfoobar " ) , QString ( " pReFiXFoobar " ) ) ; //capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXtESTPAD " , " prefixfoobar " ) , QString ( " pReFiXfOOBAR " ) ) ; //un-capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXtEsTpAd " , " prefixfoobar " ) , QString ( " pReFiXfoobar " ) ) ; //mixed case, use replacement as specified
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXTeStPaD " , " prefixfoobar " ) , QString ( " pReFiXfoobar " ) ) ; //mixed case, use replacement as specified
//with common suffix
QCOMPARE ( Utils : : matchCaseReplacement ( " testpadSuFfIx " , " foobarsuffix " ) , QString ( " foobarSuFfIx " ) ) ; //lower case
QCOMPARE ( Utils : : matchCaseReplacement ( " TESTPADSuFfIx " , " foobarsuffix " ) , QString ( " FOOBARSuFfIx " ) ) ; //upper case
QCOMPARE ( Utils : : matchCaseReplacement ( " TestpadSuFfIx " , " foobarsuffix " ) , QString ( " FoobarSuFfIx " ) ) ; //capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " tESTPADSuFfIx " , " foobarsuffix " ) , QString ( " fOOBARSuFfIx " ) ) ; //un-capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " tEsTpAdSuFfIx " , " foobarsuffix " ) , QString ( " foobarSuFfIx " ) ) ; //mixed case, use replacement as specified
QCOMPARE ( Utils : : matchCaseReplacement ( " TeStPaDSuFfIx " , " foobarsuffix " ) , QString ( " foobarSuFfIx " ) ) ; //mixed case, use replacement as specified
//with common prefix and suffix
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXtestpadSuFfIx " , " prefixfoobarsuffix " ) , QString ( " pReFiXfoobarSuFfIx " ) ) ; //lower case
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXTESTPADSuFfIx " , " prefixfoobarsuffix " ) , QString ( " pReFiXFOOBARSuFfIx " ) ) ; //upper case
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXTestpadSuFfIx " , " prefixfoobarsuffix " ) , QString ( " pReFiXFoobarSuFfIx " ) ) ; //capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXtESTPADSuFfIx " , " prefixfoobarsuffix " ) , QString ( " pReFiXfOOBARSuFfIx " ) ) ; //un-capitalized
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXtEsTpAdSuFfIx " , " prefixfoobarsuffix " ) , QString ( " pReFiXfoobarSuFfIx " ) ) ; //mixed case, use replacement as specified
QCOMPARE ( Utils : : matchCaseReplacement ( " pReFiXTeStPaDSuFfIx " , " prefixfoobarsuffix " ) , QString ( " pReFiXfoobarSuFfIx " ) ) ; //mixed case, use replacement as specified
}
2022-06-10 12:21:30 +02:00
QTEST_GUILESS_MAIN ( tst_FileSearch )
2010-11-01 17:39:28 +01:00
# include "tst_filesearch.moc"