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::TestKdevForamtSource) | ||||
27 | | ||||
28 | using namespace KDevelop; | ||||
29 | | ||||
30 | bool writeLinesToFile(const QString& path, const QStringList& lines); | ||||
31 | | ||||
32 | void TestKdevForamtSource::doTest(const QStringList& formatFileData, bool hasCommands, bool hasMatch) | ||||
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 | if (!formatFileData.isEmpty()) | ||||
35 | QCOMPARE(writeLinesToFile(QStringLiteral("format_sources"), formatFileData), true); | ||||
36 | | ||||
37 | for (Source& source : m_sources) { | ||||
38 | KDevFormatFile formatFile(source.path, source.path); | ||||
39 | | ||||
40 | if (formatFileData.isEmpty()) | ||||
41 | QCOMPARE(formatFile.find(), false); | ||||
42 | else { | ||||
43 | QCOMPARE(formatFile.find(), true); | ||||
44 | | ||||
45 | if (!hasCommands) | ||||
46 | QCOMPARE(formatFile.read(), false); | ||||
47 | else { | ||||
48 | QCOMPARE(formatFile.read(), true); | ||||
49 | | ||||
50 | if (hasMatch) | ||||
51 | QCOMPARE(formatFile.apply(), true); | ||||
52 | else | ||||
53 | QCOMPARE(formatFile.apply(), false); | ||||
54 | } | ||||
55 | } | ||||
56 | | ||||
57 | QCOMPARE(source.read(), true); | ||||
58 | QCOMPARE(source.processedData, source.expectedData); | ||||
59 | } | ||||
60 | } | ||||
61 | | ||||
62 | void TestKdevForamtSource::testNotFound() | ||||
63 | { | ||||
64 | QCOMPARE(initTest(), true); | ||||
65 | | ||||
66 | doTest({}); | ||||
67 | } | ||||
68 | | ||||
69 | void KDevelop::TestKdevForamtSource::testNoCommands() | ||||
70 | { | ||||
71 | QCOMPARE(initTest(), true); | ||||
72 | | ||||
73 | doTest({QStringLiteral("# some comment")}, false); | ||||
74 | } | ||||
75 | | ||||
76 | void KDevelop::TestKdevForamtSource::testNotMatch() | ||||
77 | { | ||||
78 | QCOMPARE(initTest(), true); | ||||
79 | | ||||
80 | doTest({QStringLiteral("notmatched.cpp : unused_command")}, true, false); | ||||
81 | } | ||||
82 | | ||||
83 | void KDevelop::TestKdevForamtSource::testMatch1() | ||||
84 | { | ||||
85 | static const QStringList formatFileData({ | ||||
86 | QStringLiteral("src1/source_1.cpp : cat $ORIGFILE | sed 's/foo/FOO/' > tmp && mv tmp $ORIGFILE"), | ||||
87 | QStringLiteral("src2/source_2.cpp : cat $ORIGFILE | sed 's/sqrt/std::sqrt/' > tmp && mv tmp $ORIGFILE"), | ||||
88 | QStringLiteral("*.cpp : cat $ORIGFILE | sed 's/z/Z/' > tmp && mv tmp $ORIGFILE"), | ||||
89 | QStringLiteral("notmatched.cpp : unused_command"), | ||||
90 | }); | ||||
91 | | ||||
92 | QCOMPARE(initTest(), true); | ||||
93 | | ||||
94 | m_sources[0].expectedData.replaceInStrings("foo", "FOO"); | ||||
95 | m_sources[1].expectedData.replaceInStrings("sqrt", "std::sqrt"); | ||||
96 | m_sources[2].expectedData.replaceInStrings("z", "Z"); | ||||
97 | | ||||
98 | doTest(formatFileData, true, true); | ||||
99 | } | ||||
100 | | ||||
101 | void KDevelop::TestKdevForamtSource::testMatch2() | ||||
102 | { | ||||
103 | static const QStringList formatFileData({QStringLiteral("cat $ORIGFILE | sed 's/;/;;/' > tmp && mv tmp $ORIGFILE")}); | ||||
104 | | ||||
105 | QCOMPARE(initTest(), true); | ||||
106 | | ||||
107 | for (Source& source : m_sources) { | ||||
108 | source.expectedData.replaceInStrings(";", ";;"); | ||||
109 | } | ||||
110 | | ||||
111 | doTest(formatFileData, true, true); | ||||
112 | } | ||||
113 | | ||||
114 | bool TestKdevForamtSource::initTest() | ||||
115 | { | ||||
116 | QString workPath = QStandardPaths::standardLocations(QStandardPaths::TempLocation).first(); | ||||
117 | workPath += "/test_kdevformatsource/"; | ||||
118 | | ||||
119 | if (QDir(workPath).exists() && !QDir(workPath).removeRecursively()) { | ||||
120 | qDebug() << "unable to remove existing directory" << workPath; | ||||
121 | return false; | ||||
122 | } | ||||
123 | | ||||
124 | if (!mkPath(workPath)) | ||||
125 | return false; | ||||
126 | | ||||
127 | if (!mkPath(workPath + "src1")) | ||||
128 | return false; | ||||
129 | | ||||
130 | if (!mkPath(workPath + "src2")) | ||||
131 | return false; | ||||
132 | | ||||
133 | m_sources.resize(3); | ||||
134 | | ||||
135 | m_sources[0].path = workPath + "src1/source_1.cpp"; | ||||
136 | m_sources[0].origData = QStringList({ | ||||
137 | QStringLiteral("void foo(int x) {"), | ||||
138 | QStringLiteral(" printf(\"squared x = %d\\n\", x * x);"), | ||||
139 | QStringLiteral("}") | ||||
140 | }); | ||||
141 | | ||||
142 | m_sources[1].path = workPath + "src2/source_2.cpp"; | ||||
143 | m_sources[1].origData = QStringList({ | ||||
144 | QStringLiteral("void bar(double x) {"), | ||||
145 | QStringLiteral(" x = sqrt(x);"), | ||||
146 | QStringLiteral(" printf(\"sqrt(x) = %e\\n\", x);"), | ||||
147 | QStringLiteral("}") | ||||
148 | }); | ||||
149 | | ||||
150 | m_sources[2].path = workPath + "source_3.cpp"; | ||||
151 | m_sources[2].origData = QStringList({ | ||||
152 | QStringLiteral("void baz(double x, double y) {"), | ||||
153 | QStringLiteral(" double z = pow(x, y);"), | ||||
154 | QStringLiteral(" printf(\"x^y = %e\\n\", z);"), | ||||
155 | QStringLiteral("}") | ||||
156 | }); | ||||
157 | | ||||
158 | for (Source& source : m_sources) { | ||||
159 | source.expectedData = source.origData; | ||||
160 | | ||||
161 | if (!source.write()) | ||||
162 | return false; | ||||
163 | } | ||||
164 | | ||||
165 | QDir::setCurrent(workPath); | ||||
166 | | ||||
167 | return true; | ||||
168 | } | ||||
169 | | ||||
170 | bool KDevelop::TestKdevForamtSource::mkPath(const QString& path) | ||||
171 | { | ||||
172 | if (!QDir().exists(path) && !QDir().mkpath(path)) { | ||||
173 | qDebug() << "unable to create directory" << path; | ||||
174 | return false; | ||||
175 | } | ||||
176 | | ||||
177 | return true; | ||||
178 | } | ||||
179 | | ||||
180 | bool Source::write() | ||||
181 | { | ||||
182 | return ::writeLinesToFile(path, origData); | ||||
183 | } | ||||
184 | | ||||
185 | bool Source::read() | ||||
186 | { | ||||
187 | QFile inFile(path); | ||||
188 | if (!inFile.open(QIODevice::ReadOnly)) { | ||||
189 | qDebug() << "unable to open file" << path << "for reading"; | ||||
190 | return false; | ||||
191 | } | ||||
192 | | ||||
193 | processedData.clear(); | ||||
194 | | ||||
195 | QTextStream inStream(&inFile); | ||||
196 | while(!inStream.atEnd()) { | ||||
197 | processedData += inStream.readLine(); | ||||
198 | } | ||||
199 | inFile.close(); | ||||
200 | | ||||
201 | return true; | ||||
202 | } | ||||
203 | | ||||
204 | bool writeLinesToFile(const QString& path, const QStringList& lines) | ||||
205 | { | ||||
206 | QFile outFile(path); | ||||
207 | if (!outFile.open(QIODevice::WriteOnly)) { | ||||
208 | qDebug() << "unable to open file" << path << "for writing"; | ||||
209 | return false; | ||||
210 | } | ||||
211 | | ||||
212 | QTextStream outStream(&outFile); | ||||
213 | foreach (const QString& line, lines) { | ||||
214 | outStream << line << "\n"; | ||||
215 | } | ||||
216 | outFile.close(); | ||||
217 | | ||||
218 | return true; | ||||
219 | } | ||||
220 | | ||||
221 | #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.