Changeset View
Changeset View
Standalone View
Standalone View
util/tests/test_kdevformatsource.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2016 Anton Anikin <anton.anikin@htower.ru> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or modify | ||||
5 | it under the terms of the GNU General Public License as published by | ||||
6 | the Free Software Foundation; either version 2 of the License, or | ||||
7 | (at your option) any later version. | ||||
8 | | ||||
9 | This program is distributed in the hope that it will be useful, | ||||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
12 | GNU General Public License for more details. | ||||
13 | | ||||
14 | You should have received a copy of the GNU General Public License along | ||||
15 | with this program; if not, write to the Free Software Foundation, Inc., | ||||
16 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "test_kdevformatsource.h" | ||||
20 | #include "../kdevformatfile.h" | ||||
21 | | ||||
22 | #include <QtTest/QtTest> | ||||
23 | | ||||
24 | #include <QDir> | ||||
25 | | ||||
26 | QTEST_MAIN(KDevelop::TestKdevFormatSource) | ||||
27 | | ||||
28 | using namespace KDevelop; | ||||
29 | | ||||
30 | void TestKdevFormatSource::testNotFound_data() | ||||
31 | { | ||||
32 | static const QStringList formatFileData = {}; | ||||
33 | | ||||
kfunk: Hmm, test looks useful, but it would be more easy to understand if you'd have used data-driven… | |||||
I actually meant the way Qt implements data-driven testing, see here: http://doc.qt.io/qt-5/qttestlib-tutorial2-example.html But feel free to leave it like that, I'll refactor it if noone else steps up. kfunk: I actually meant the way Qt implements data-driven testing, see here: http://doc.qt.io/qt… | |||||
antonanikin: Ok. I'll try to use this way in next revision. | |||||
kfunk: Typo: TestKdevForamtSource | |||||
34 | QCOMPARE(initTest(formatFileData), true); | ||||
35 | | ||||
36 | for (const Source& source : m_sources) { | ||||
37 | QTest::newRow(source.path.toUtf8()) << source.path << false << false << false << source.lines; | ||||
38 | } | ||||
39 | } | ||||
40 | | ||||
41 | void TestKdevFormatSource::testNotFound() | ||||
42 | { | ||||
43 | runTest(); | ||||
44 | } | ||||
45 | | ||||
46 | void TestKdevFormatSource::testNoCommands_data() | ||||
47 | { | ||||
48 | static const QStringList formatFileData = {QStringLiteral("# some comment")}; | ||||
49 | | ||||
50 | QCOMPARE(initTest(formatFileData), true); | ||||
51 | | ||||
52 | for (const Source& source : m_sources) { | ||||
53 | QTest::newRow(source.path.toUtf8()) << source.path << true << false << false << source.lines; | ||||
54 | } | ||||
55 | } | ||||
56 | | ||||
57 | void TestKdevFormatSource::testNoCommands() | ||||
58 | { | ||||
59 | runTest(); | ||||
60 | } | ||||
61 | | ||||
62 | void TestKdevFormatSource::testNotMatch_data() | ||||
63 | { | ||||
64 | static const QStringList formatFileData = {QStringLiteral("notmatched.cpp : unused_command")}; | ||||
65 | | ||||
66 | QCOMPARE(initTest(formatFileData), true); | ||||
67 | | ||||
68 | for (const Source& source : m_sources) { | ||||
69 | QTest::newRow(source.path.toUtf8()) << source.path << true << true << false << source.lines; | ||||
70 | } | ||||
71 | } | ||||
72 | | ||||
73 | void TestKdevFormatSource::testNotMatch() | ||||
74 | { | ||||
75 | runTest(); | ||||
76 | } | ||||
77 | | ||||
78 | void TestKdevFormatSource::testMatch1_data() | ||||
79 | { | ||||
80 | static const QStringList formatFileData({ | ||||
81 | QStringLiteral("src1/source_1.cpp : cat $ORIGFILE | sed 's/foo/FOO/' > tmp && mv tmp $ORIGFILE"), | ||||
82 | QStringLiteral("src2/source_2.cpp : cat $ORIGFILE | sed 's/sqrt/std::sqrt/' > tmp && mv tmp $ORIGFILE"), | ||||
83 | QStringLiteral("*.cpp : cat $ORIGFILE | sed 's/z/Z/' > tmp && mv tmp $ORIGFILE"), | ||||
84 | QStringLiteral("notmatched.cpp : unused_command"), | ||||
85 | }); | ||||
86 | | ||||
87 | QCOMPARE(initTest(formatFileData), true); | ||||
88 | | ||||
89 | m_sources[0].lines.replaceInStrings("foo", "FOO"); | ||||
90 | m_sources[1].lines.replaceInStrings("sqrt", "std::sqrt"); | ||||
91 | m_sources[2].lines.replaceInStrings("z", "Z"); | ||||
92 | | ||||
93 | for (const Source& source : m_sources) { | ||||
94 | QTest::newRow(source.path.toUtf8()) << source.path << true << true << true << source.lines; | ||||
95 | } | ||||
96 | } | ||||
97 | | ||||
98 | void TestKdevFormatSource::testMatch1() | ||||
99 | { | ||||
100 | runTest(); | ||||
101 | } | ||||
102 | | ||||
103 | void TestKdevFormatSource::testMatch2_data() | ||||
104 | { | ||||
105 | static const QStringList formatFileData({QStringLiteral("cat $ORIGFILE | sed 's/;/;;/' > tmp && mv tmp $ORIGFILE")}); | ||||
106 | | ||||
107 | QCOMPARE(initTest(formatFileData), true); | ||||
108 | | ||||
109 | for (Source& source : m_sources) { | ||||
110 | source.lines.replaceInStrings(";", ";;"); | ||||
111 | QTest::newRow(source.path.toUtf8()) << source.path << true << true << true << source.lines; | ||||
112 | } | ||||
113 | | ||||
114 | } | ||||
115 | | ||||
116 | void TestKdevFormatSource::testMatch2() | ||||
117 | { | ||||
118 | runTest(); | ||||
119 | } | ||||
120 | | ||||
121 | bool TestKdevFormatSource::initTest(const QStringList& formatFileData) | ||||
122 | { | ||||
123 | QTest::addColumn<QString>("path"); | ||||
124 | QTest::addColumn<bool>("isFound"); | ||||
125 | QTest::addColumn<bool>("isRead"); | ||||
126 | QTest::addColumn<bool>("isApplied"); | ||||
127 | QTest::addColumn<QStringList>("lines"); | ||||
128 | | ||||
129 | QString workPath = QStandardPaths::standardLocations(QStandardPaths::TempLocation).first(); | ||||
130 | workPath += "/test_kdevformatsource/"; | ||||
131 | | ||||
132 | if (QDir(workPath).exists() && !QDir(workPath).removeRecursively()) { | ||||
133 | qDebug() << "unable to remove existing directory" << workPath; | ||||
134 | return false; | ||||
135 | } | ||||
136 | | ||||
137 | if (!mkPath(workPath)) | ||||
138 | return false; | ||||
139 | | ||||
140 | if (!mkPath(workPath + "src1")) | ||||
141 | return false; | ||||
142 | | ||||
143 | if (!mkPath(workPath + "src2")) | ||||
144 | return false; | ||||
145 | | ||||
146 | if (!QDir::setCurrent(workPath)) { | ||||
147 | qDebug() << "unable to set current directory to" << workPath; | ||||
148 | return false; | ||||
149 | } | ||||
150 | | ||||
151 | m_sources.resize(3); | ||||
152 | | ||||
153 | m_sources[0].path = workPath + "src1/source_1.cpp"; | ||||
154 | m_sources[0].lines = QStringList({ | ||||
155 | QStringLiteral("void foo(int x) {"), | ||||
156 | QStringLiteral(" printf(\"squared x = %d\\n\", x * x);"), | ||||
157 | QStringLiteral("}") | ||||
158 | }); | ||||
159 | | ||||
160 | m_sources[1].path = workPath + "src2/source_2.cpp"; | ||||
161 | m_sources[1].lines = QStringList({ | ||||
162 | QStringLiteral("void bar(double x) {"), | ||||
163 | QStringLiteral(" x = sqrt(x);"), | ||||
164 | QStringLiteral(" printf(\"sqrt(x) = %e\\n\", x);"), | ||||
165 | QStringLiteral("}") | ||||
166 | }); | ||||
167 | | ||||
168 | m_sources[2].path = workPath + "source_3.cpp"; | ||||
169 | m_sources[2].lines = QStringList({ | ||||
170 | QStringLiteral("void baz(double x, double y) {"), | ||||
171 | QStringLiteral(" double z = pow(x, y);"), | ||||
172 | QStringLiteral(" printf(\"x^y = %e\\n\", z);"), | ||||
173 | QStringLiteral("}") | ||||
174 | }); | ||||
175 | | ||||
176 | for (const Source& source : m_sources) { | ||||
177 | if (!writeLines(source.path, source.lines)) | ||||
178 | return false; | ||||
179 | } | ||||
180 | | ||||
181 | if (!formatFileData.isEmpty() && !writeLines(QStringLiteral("format_sources"), formatFileData)) | ||||
182 | return false; | ||||
183 | | ||||
184 | return true; | ||||
185 | } | ||||
186 | | ||||
187 | void TestKdevFormatSource::runTest() const | ||||
188 | { | ||||
189 | QFETCH(QString, path); | ||||
190 | QFETCH(bool, isFound); | ||||
191 | QFETCH(bool, isRead); | ||||
192 | QFETCH(bool, isApplied); | ||||
193 | QFETCH(QStringList, lines); | ||||
194 | | ||||
195 | KDevFormatFile formatFile(path, path); | ||||
196 | | ||||
197 | QCOMPARE(formatFile.find(), isFound); | ||||
198 | | ||||
199 | if (isFound) | ||||
200 | QCOMPARE(formatFile.read(), isRead); | ||||
201 | | ||||
202 | if (isRead) | ||||
203 | QCOMPARE(formatFile.apply(), isApplied); | ||||
204 | | ||||
205 | QStringList processedLines; | ||||
206 | QCOMPARE(readLines(path, processedLines), true); | ||||
207 | | ||||
208 | QCOMPARE(processedLines, lines); | ||||
209 | } | ||||
210 | | ||||
211 | bool TestKdevFormatSource::mkPath(const QString& path) const | ||||
212 | { | ||||
213 | if (!QDir().exists(path) && !QDir().mkpath(path)) { | ||||
214 | qDebug() << "unable to create directory" << path; | ||||
215 | return false; | ||||
216 | } | ||||
217 | | ||||
218 | return true; | ||||
219 | } | ||||
220 | | ||||
221 | bool TestKdevFormatSource::writeLines(const QString& path, const QStringList& lines) const | ||||
222 | { | ||||
223 | QFile outFile(path); | ||||
224 | if (!outFile.open(QIODevice::WriteOnly)) { | ||||
225 | qDebug() << "unable to open file" << path << "for writing"; | ||||
226 | return false; | ||||
227 | } | ||||
228 | | ||||
229 | QTextStream outStream(&outFile); | ||||
230 | for (const QString& line : lines) { | ||||
231 | outStream << line << "\n"; | ||||
232 | } | ||||
233 | outFile.close(); | ||||
234 | | ||||
235 | return true; | ||||
236 | } | ||||
237 | | ||||
238 | bool TestKdevFormatSource::readLines(const QString& path, QStringList& lines) const | ||||
239 | { | ||||
240 | QFile inFile(path); | ||||
241 | if (!inFile.open(QIODevice::ReadOnly)) { | ||||
242 | qDebug() << "unable to open file" << path << "for reading"; | ||||
243 | return false; | ||||
244 | } | ||||
245 | | ||||
246 | lines.clear(); | ||||
247 | | ||||
248 | QTextStream inStream(&inFile); | ||||
249 | while (!inStream.atEnd()) { | ||||
250 | lines += inStream.readLine(); | ||||
251 | } | ||||
252 | inFile.close(); | ||||
253 | | ||||
254 | return true; | ||||
255 | } | ||||
256 | | ||||
257 | #include "test_kdevformatsource.moc" |
Hmm, test looks useful, but it would be more easy to understand if you'd have used data-driven tests here. In the data section, you could both specify the format file contents + expected contents for the source files.
Right know we're duplicating a lot of code in the test functions.