File indexing completed on 2024-04-28 15:22:15

0001 /*
0002     This file is part of the Nepomuk KDE project.
0003     SPDX-FileCopyrightText: 2013 David Edmundson <davidedmundson@kde.org>
0004     SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "indexerextractortests.h"
0010 
0011 #include <QTest>
0012 #include <QTemporaryFile>
0013 
0014 #include "simpleextractionresult.h"
0015 #include "indexerextractortestsconfig.h"
0016 #include "extractors/plaintextextractor.h"
0017 
0018 using namespace KFileMetaData;
0019 
0020 IndexerExtractorTests::IndexerExtractorTests(QObject* parent) :
0021     QObject(parent)
0022 {
0023 }
0024 
0025 QString IndexerExtractorTests::testFilePath(const QString& fileName) const
0026 {
0027     return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName;
0028 }
0029 
0030 void IndexerExtractorTests::benchMarkPlainTextExtractor()
0031 {
0032     PlainTextExtractor plugin(this);
0033 
0034     // generate a test file with varying number of words per line
0035     QTemporaryFile file(QStringLiteral("XXXXXX.txt"));
0036     QVERIFY(file.open());
0037     QByteArray chunk("foo bar ");
0038     for (int line = 0; line < 10000; ++line) {
0039         for (int i = 0; i < line % 100; ++i) {
0040             file.write(chunk);
0041         }
0042         file.write("\n");
0043     }
0044 
0045     SimpleExtractionResult result(file.fileName(), QStringLiteral("text/plain"));
0046 
0047     QBENCHMARK {
0048         plugin.extract(&result);
0049     }
0050 }
0051 
0052 void IndexerExtractorTests::testNoExtraction()
0053 {
0054     PlainTextExtractor plugin{this};
0055 
0056     SimpleExtractionResult result(testFilePath(QStringLiteral("plain_text_file.txt")), QStringLiteral("text/plain"), ExtractionResult::ExtractNothing);
0057     plugin.extract(&result);
0058 
0059 
0060     QCOMPARE(result.types().size(), 1);
0061     QCOMPARE(result.types().at(0), Type::Text);
0062 
0063     QCOMPARE(result.properties().size(), 0);
0064 }
0065 
0066 void IndexerExtractorTests::testPlainTextExtractor()
0067 {
0068     PlainTextExtractor plugin{this};
0069 
0070     SimpleExtractionResult result(testFilePath(QStringLiteral("plain_text_file.txt")), QStringLiteral("text/plain"));
0071     plugin.extract(&result);
0072 
0073     QString content;
0074     QTextStream(&content) << "This is a text file\n"
0075                           << "it is four lines long\n"
0076                           << "it has 77 characters\n"
0077                           << "and 17 words.\n";
0078 
0079     QCOMPARE(result.types().size(), 1);
0080     QCOMPARE(result.types().at(0), Type::Text);
0081 
0082     QCOMPARE(result.properties().size(), 1);
0083     QCOMPARE(result.properties().value(Property::LineCount), QVariant(4));
0084 
0085     content.replace(QLatin1Char('\n'), QLatin1Char(' '));
0086     QCOMPARE(result.text(), content);
0087 }
0088 
0089 void IndexerExtractorTests::testPlainTextExtractorNoPlainText()
0090 {
0091     PlainTextExtractor plugin{this};
0092 
0093     SimpleExtractionResult result(testFilePath(QStringLiteral("plain_text_file.txt")), QStringLiteral("text/plain"), ExtractionResult::ExtractMetaData);
0094     plugin.extract(&result);
0095 
0096     QString content;
0097     QTextStream(&content) << "This is a text file\n"
0098                           << "it is four lines long\n"
0099                           << "it has 77 characters\n"
0100                           << "and 17 words.\n";
0101 
0102     QCOMPARE(result.types().size(), 1);
0103     QCOMPARE(result.types().at(0), Type::Text);
0104 
0105     QCOMPARE(result.properties().size(), 0);
0106 }
0107 
0108 QTEST_GUILESS_MAIN(IndexerExtractorTests)
0109 
0110 #include "moc_indexerextractortests.cpp"