Changeset View
Changeset View
Standalone View
Standalone View
autotests/xmlextractortest.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2018 Stefan Brüns <stefan.bruens@rwth-aachen.de> | ||||
3 | | ||||
4 | This library is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU Lesser General Public | ||||
6 | License as published by the Free Software Foundation; either | ||||
7 | version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | later version accepted by the membership of KDE e.V. (or its | ||||
9 | successor approved by the membership of KDE e.V.), which shall | ||||
10 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | | ||||
12 | This library is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | Lesser General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU Lesser General Public | ||||
18 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "xmlextractortest.h" | ||||
22 | | ||||
23 | #include <QtTest> | ||||
24 | | ||||
25 | #include "simpleextractionresult.h" | ||||
26 | #include "indexerextractortestsconfig.h" | ||||
27 | #include "extractors/xmlextractor.h" | ||||
28 | | ||||
29 | using namespace KFileMetaData; | ||||
30 | | ||||
31 | XmlExtractorTests::XmlExtractorTests(QObject* parent) : | ||||
32 | QObject(parent) | ||||
33 | { | ||||
34 | } | ||||
35 | | ||||
36 | QString XmlExtractorTests::testFilePath(const QString& fileName) const | ||||
37 | { | ||||
38 | return QLatin1String(INDEXER_TESTS_SAMPLE_FILES_PATH) + QLatin1Char('/') + fileName; | ||||
39 | } | ||||
lbeltrame: IIRC you shouldn't use `QDir::separator()`. See http://agateau.com/2015/qdir-separator… | |||||
Thx, fixed here and elsewhere (D16505) bruns: Thx, fixed here and elsewhere (D16505) | |||||
40 | | ||||
41 | void XmlExtractorTests::benchMarkXmlExtractor() | ||||
42 | { | ||||
43 | XmlExtractor plugin(this); | ||||
44 | | ||||
45 | // generate a test file with varying number of words per line | ||||
46 | QTemporaryFile file(QStringLiteral("XXXXXX.xml")); | ||||
47 | QVERIFY(file.open()); | ||||
48 | | ||||
49 | int count = 0; | ||||
50 | file.write("<xml>\n"); | ||||
51 | QByteArray chunk("<text>foo bar </text>"); | ||||
52 | for (int line = 0; line < 10000; ++line) { | ||||
53 | // staircase pattern, 0, 1, 2, ... 98, 0, 0, 1 ... chunks per line | ||||
54 | for (int i = 0; i < line % 100; ++i) { | ||||
55 | count++; | ||||
56 | file.write(chunk); | ||||
57 | } | ||||
58 | file.write("\n"); | ||||
59 | } | ||||
60 | file.write("</xml>\n"); | ||||
61 | file.close(); | ||||
62 | | ||||
63 | SimpleExtractionResult result(file.fileName(), QStringLiteral("application/xml")); | ||||
64 | | ||||
65 | plugin.extract(&result); | ||||
66 | | ||||
67 | QString content = QStringLiteral("foo bar\n"); | ||||
68 | content.replace(QLatin1Char('\n'), QLatin1Char(' ')); | ||||
69 | QCOMPARE(result.text().leftRef(8), content.leftRef(8)); | ||||
70 | QCOMPARE(result.text().size(), 1 + 8 * count); | ||||
71 | | ||||
72 | QBENCHMARK { | ||||
73 | plugin.extract(&result); | ||||
74 | } | ||||
75 | } | ||||
76 | | ||||
77 | void XmlExtractorTests::testXmlExtractor() | ||||
78 | { | ||||
79 | XmlExtractor plugin{this}; | ||||
80 | | ||||
81 | SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_metadata.svg")), | ||||
82 | QStringLiteral("image/svg"), | ||||
83 | ExtractionResult::ExtractEverything); | ||||
84 | plugin.extract(&result); | ||||
85 | | ||||
86 | QString content = QStringLiteral("Some text\n"); | ||||
87 | | ||||
88 | QCOMPARE(result.types().size(), 1); | ||||
89 | QCOMPARE(result.types().at(0), Type::Image); | ||||
90 | | ||||
91 | QCOMPARE(result.properties().size(), 1); | ||||
92 | QCOMPARE(result.properties().value(Property::Title), QStringLiteral("Document Title")); | ||||
93 | | ||||
94 | content.replace(QLatin1Char('\n'), QLatin1Char(' ')); | ||||
95 | QCOMPARE(result.text(), content); | ||||
96 | } | ||||
97 | | ||||
98 | void XmlExtractorTests::testXmlExtractorNoContent() | ||||
99 | { | ||||
100 | XmlExtractor plugin{this}; | ||||
101 | | ||||
102 | SimpleExtractionResult result(testFilePath(QStringLiteral("test_with_metadata.svg")), | ||||
103 | QStringLiteral("image/svg"), | ||||
104 | ExtractionResult::ExtractMetaData); | ||||
105 | plugin.extract(&result); | ||||
106 | | ||||
107 | QCOMPARE(result.types().size(), 1); | ||||
108 | QCOMPARE(result.types().at(0), Type::Image); | ||||
109 | | ||||
110 | QCOMPARE(result.properties().size(), 1); | ||||
111 | QCOMPARE(result.properties().value(Property::Title), QStringLiteral("Document Title")); | ||||
112 | | ||||
113 | QVERIFY(result.text().isEmpty()); | ||||
114 | } | ||||
115 | | ||||
116 | QTEST_GUILESS_MAIN(XmlExtractorTests) | ||||
117 | |
IIRC you shouldn't use QDir::separator(). See http://agateau.com/2015/qdir-separator-considered-harmful/