Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/tests/kis_parse_spin_boxes_test.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2016 Laurent Valentin Jospin <laurent.valentin@famillejospin.ch> | ||||
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 | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "kis_parse_spin_boxes_test.h" | ||||
20 | | ||||
21 | #include "kis_num_parser.h" | ||||
22 | #include "kis_double_parse_spin_box.h" | ||||
23 | #include "kis_int_parse_spin_box.h" | ||||
24 | | ||||
25 | #include <QString> | ||||
26 | #include <QtTest> | ||||
27 | | ||||
28 | const QStringList KisParseSpinBoxesTest::doubleExprs = {"1", | ||||
29 | "-12", | ||||
30 | "7.9 - 12", | ||||
31 | "cos(90)*2", | ||||
32 | "cos(acos(-1)+1*3^2.0)*2 + sin(3)/2"}; | ||||
33 | | ||||
34 | const QStringList KisParseSpinBoxesTest::doubleWrongExprs = {"abc", | ||||
35 | "1/", | ||||
36 | "7.9 + 12*", | ||||
37 | "cos(90)*2 + ", | ||||
38 | "23.0/0", | ||||
39 | "0.0/0.0"}; | ||||
40 | | ||||
41 | const QStringList KisParseSpinBoxesTest::intExprs = {"12", | ||||
42 | "-12", | ||||
43 | "-12.3", | ||||
44 | "12.7 - 25", | ||||
45 | "12.7", | ||||
46 | "12*1.5", | ||||
47 | "12/2.5", | ||||
48 | "518*2/3"}; | ||||
49 | | ||||
50 | const QStringList KisParseSpinBoxesTest::intWrongExprs = {"abc", | ||||
51 | "12.5/2 +", | ||||
52 | "12*", | ||||
53 | "12/0"}; | ||||
54 | | ||||
55 | KisParseSpinBoxesTest::KisParseSpinBoxesTest() : QObject() | ||||
56 | { | ||||
57 | | ||||
58 | } | ||||
59 | | ||||
60 | void KisParseSpinBoxesTest::testDoubleParseNormal() | ||||
61 | { | ||||
62 | | ||||
63 | KisDoubleParseSpinBox spinBox; | ||||
64 | spinBox.setDecimals(3); | ||||
65 | spinBox.setMaximum(9999.0); | ||||
66 | spinBox.setMinimum(-9999.0); | ||||
67 | | ||||
68 | for (int i = 0; i < doubleExprs.size(); i++) { | ||||
69 | | ||||
70 | spinBox.clearFocus(); | ||||
71 | spinBox.clear(); //clear all | ||||
72 | QTest::keyClicks(&spinBox, doubleExprs[i]); | ||||
73 | spinBox.clearFocus(); | ||||
74 | | ||||
75 | double resultParser = KisNumericParser::parseSimpleMathExpr(doubleExprs[i]); | ||||
76 | double valueSpinBox = spinBox.value(); | ||||
77 | | ||||
78 | bool test = resultParser == valueSpinBox || qAbs(resultParser - valueSpinBox) < 1e-2; | ||||
79 | | ||||
80 | QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3") | ||||
81 | .arg(doubleExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str()); | ||||
82 | | ||||
83 | spinBox.setValue(0); | ||||
84 | | ||||
85 | } | ||||
86 | | ||||
87 | | ||||
88 | } | ||||
89 | | ||||
90 | void KisParseSpinBoxesTest::testDoubleParseProblem() | ||||
91 | { | ||||
92 | | ||||
93 | //error can happen with incomplete or incorrect expressions, inf or nan values. | ||||
94 | | ||||
95 | KisDoubleParseSpinBox spinBox; | ||||
96 | spinBox.setMaximum(9999.0); | ||||
97 | spinBox.setMinimum(-9999.0); | ||||
98 | | ||||
99 | for (int i = 0; i < doubleWrongExprs.size(); i++) { | ||||
100 | | ||||
101 | spinBox.clearFocus(); | ||||
102 | spinBox.clear(); //clear all | ||||
103 | QTest::keyClicks(&spinBox, doubleWrongExprs[i]); | ||||
104 | spinBox.clearFocus(); | ||||
105 | | ||||
106 | QVERIFY2(!spinBox.isLastValid(), QString("SpinBox is in a valid state with expression %1, but shouldn't.") | ||||
107 | .arg(doubleWrongExprs[i]).toStdString().c_str()); | ||||
108 | | ||||
109 | spinBox.setValue(0.0); | ||||
110 | | ||||
111 | QVERIFY2(spinBox.isLastValid(), QString("SpinBox unsable to recover error free state after a value reset.") | ||||
112 | .toStdString().c_str()); | ||||
113 | | ||||
114 | spinBox.setValue(0); | ||||
115 | | ||||
116 | } | ||||
117 | | ||||
118 | } | ||||
119 | void KisParseSpinBoxesTest::testDoubleParseWithSuffix(){ | ||||
120 | | ||||
121 | QString suff = "px"; | ||||
122 | | ||||
123 | KisDoubleParseSpinBox spinBox; | ||||
124 | spinBox.setDecimals(3); | ||||
125 | spinBox.setMaximum(9999.0); | ||||
126 | spinBox.setMinimum(-9999.0); | ||||
127 | spinBox.setSuffix(suff); | ||||
128 | | ||||
129 | for (int i = 0; i < doubleExprs.size(); i++) { | ||||
130 | | ||||
131 | spinBox.clearFocus(); | ||||
132 | spinBox.clear(); //clear all | ||||
133 | QTest::keyClicks(&spinBox, doubleExprs[i]); | ||||
134 | spinBox.clearFocus(); | ||||
135 | | ||||
136 | double resultParser = KisNumericParser::parseSimpleMathExpr(doubleExprs[i]); | ||||
137 | double valueSpinBox = spinBox.value(); | ||||
138 | | ||||
139 | bool test = resultParser == valueSpinBox || qAbs(resultParser - valueSpinBox) < 1e-2; | ||||
140 | | ||||
141 | QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3") | ||||
142 | .arg(doubleExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str()); | ||||
143 | | ||||
144 | spinBox.setValue(0); | ||||
145 | | ||||
146 | } | ||||
147 | | ||||
148 | //verify that the suffix don't appear in the clean text in case of error. | ||||
149 | for (int i = 0; i < doubleWrongExprs.size(); i++) { | ||||
150 | | ||||
151 | if (doubleWrongExprs[i].endsWith(suff)) { | ||||
152 | continue; | ||||
153 | } | ||||
154 | | ||||
155 | spinBox.clearFocus(); | ||||
156 | spinBox.clear(); //clear all | ||||
157 | QTest::keyClicks(&spinBox, doubleWrongExprs[i]); | ||||
158 | spinBox.clearFocus(); | ||||
159 | | ||||
160 | QVERIFY2(!spinBox.cleanText().endsWith(suff), "SpinBox failed to remove suffix from clean text in error state."); | ||||
161 | | ||||
162 | spinBox.setValue(0.0); | ||||
163 | | ||||
164 | } | ||||
165 | } | ||||
166 | void KisParseSpinBoxesTest::testDoubleParseWithPrefix() | ||||
167 | { | ||||
168 | | ||||
169 | QString preff = "px"; | ||||
170 | | ||||
171 | KisDoubleParseSpinBox spinBox; | ||||
172 | spinBox.setDecimals(3); | ||||
173 | spinBox.setMaximum(9999.0); | ||||
174 | spinBox.setMinimum(-9999.0); | ||||
175 | spinBox.setPrefix(preff); | ||||
176 | | ||||
177 | for (int i = 0; i < doubleExprs.size(); i++) { | ||||
178 | | ||||
179 | spinBox.clearFocus(); | ||||
180 | spinBox.clear(); //clear all | ||||
181 | QTest::keyClicks(&spinBox, doubleExprs[i]); | ||||
182 | spinBox.clearFocus(); | ||||
183 | | ||||
184 | double resultParser = KisNumericParser::parseSimpleMathExpr(doubleExprs[i]); | ||||
185 | double valueSpinBox = spinBox.value(); | ||||
186 | | ||||
187 | bool test = resultParser == valueSpinBox || qAbs(resultParser - valueSpinBox) < 1e-2; | ||||
188 | | ||||
189 | QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3") | ||||
190 | .arg(doubleExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str()); | ||||
191 | | ||||
192 | spinBox.setValue(0); | ||||
193 | | ||||
194 | } | ||||
195 | | ||||
196 | //verify that the prefix don't appear in the clean text in case of error. | ||||
197 | for (int i = 0; i < doubleWrongExprs.size(); i++) { | ||||
198 | | ||||
199 | if (doubleWrongExprs[i].endsWith(preff)) { | ||||
200 | continue; | ||||
201 | } | ||||
202 | | ||||
203 | spinBox.clearFocus(); | ||||
204 | spinBox.clear(); //clear all | ||||
205 | QTest::keyClicks(&spinBox, doubleWrongExprs[i]); | ||||
206 | spinBox.clearFocus(); | ||||
207 | | ||||
208 | QVERIFY2(!spinBox.cleanText().startsWith(preff), "SpinBox failed to remove prefix from clean text in error state."); | ||||
209 | | ||||
210 | spinBox.setValue(0.0); | ||||
211 | | ||||
212 | } | ||||
213 | } | ||||
214 | | ||||
215 | void KisParseSpinBoxesTest::testIntParseNormal() | ||||
216 | { | ||||
217 | | ||||
218 | KisIntParseSpinBox spinBox; | ||||
219 | spinBox.setMaximum(999); | ||||
220 | spinBox.setMinimum(-999); | ||||
221 | | ||||
222 | for (int i = 0; i < intExprs.size(); i++) { | ||||
223 | | ||||
224 | spinBox.clearFocus(); | ||||
225 | spinBox.clear(); //clear all | ||||
226 | QTest::keyClicks(&spinBox, intExprs[i]); | ||||
227 | spinBox.clearFocus(); | ||||
228 | | ||||
229 | int resultParser = KisNumericParser::parseIntegerMathExpr(intExprs[i]); | ||||
230 | int valueSpinBox = spinBox.value(); | ||||
231 | | ||||
232 | bool test = resultParser == valueSpinBox; | ||||
233 | | ||||
234 | QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3") | ||||
235 | .arg(intExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str()); | ||||
236 | | ||||
237 | spinBox.setValue(0); | ||||
238 | } | ||||
239 | | ||||
240 | } | ||||
241 | | ||||
242 | void KisParseSpinBoxesTest::testIntParseProblem() | ||||
243 | { | ||||
244 | | ||||
245 | //errors can happen with incorrect or incomplete expressions, or division by 0. | ||||
246 | | ||||
247 | KisIntParseSpinBox spinBox; | ||||
248 | spinBox.setMaximum(999); | ||||
249 | spinBox.setMinimum(-999); | ||||
250 | | ||||
251 | for (int i = 0; i < intWrongExprs.size(); i++) { | ||||
252 | | ||||
253 | spinBox.clearFocus(); | ||||
254 | spinBox.clear(); //clear all | ||||
255 | QTest::keyClicks(&spinBox, intWrongExprs[i]); | ||||
256 | spinBox.clearFocus(); | ||||
257 | | ||||
258 | QVERIFY2(!spinBox.isLastValid(), QString("SpinBox is in a valid state with expression %1, but shouldn't.") | ||||
259 | .arg(intWrongExprs[i]).toStdString().c_str()); | ||||
260 | | ||||
261 | spinBox.setValue(0); | ||||
262 | | ||||
263 | QVERIFY2(spinBox.isLastValid(), QString("SpinBox unsable to recover error free state after a value reset.") | ||||
264 | .toStdString().c_str()); | ||||
265 | | ||||
266 | spinBox.setValue(0); | ||||
267 | | ||||
268 | } | ||||
269 | | ||||
270 | } | ||||
271 | | ||||
272 | void KisParseSpinBoxesTest::testIntParseWithSuffix() | ||||
273 | { | ||||
274 | QString suff = "px"; | ||||
275 | | ||||
276 | KisIntParseSpinBox spinBox; | ||||
277 | spinBox.setMaximum(999); | ||||
278 | spinBox.setMinimum(-999); | ||||
279 | spinBox.setSuffix(suff); | ||||
280 | | ||||
281 | for (int i = 0; i < intExprs.size(); i++) { | ||||
282 | | ||||
283 | spinBox.clearFocus(); | ||||
284 | spinBox.clear(); //clear all | ||||
285 | QTest::keyClicks(&spinBox, intExprs[i]); | ||||
286 | spinBox.clearFocus(); | ||||
287 | | ||||
288 | int resultParser = KisNumericParser::parseIntegerMathExpr(intExprs[i]); | ||||
289 | int valueSpinBox = spinBox.value(); | ||||
290 | | ||||
291 | bool test = resultParser == valueSpinBox; | ||||
292 | | ||||
293 | QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3") | ||||
294 | .arg(intExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str()); | ||||
295 | | ||||
296 | spinBox.setValue(0); | ||||
297 | } | ||||
298 | | ||||
299 | //verify that the suffix don't appear in the clean text in case of error. | ||||
300 | for (int i = 0; i < intWrongExprs.size(); i++) { | ||||
301 | | ||||
302 | if (intWrongExprs[i].endsWith(suff)) { | ||||
303 | continue; | ||||
304 | } | ||||
305 | | ||||
306 | spinBox.clearFocus(); | ||||
307 | spinBox.clear(); //clear all | ||||
308 | QTest::keyClicks(&spinBox, intWrongExprs[i]); | ||||
309 | spinBox.clearFocus(); | ||||
310 | | ||||
311 | QVERIFY2(!spinBox.cleanText().endsWith(suff), "SpinBox failed to remove suffix from clean text in error state."); | ||||
312 | | ||||
313 | spinBox.setValue(0.0); | ||||
314 | | ||||
315 | } | ||||
316 | | ||||
317 | } | ||||
318 | void KisParseSpinBoxesTest::testIntParseWithPrefix() | ||||
319 | { | ||||
320 | QString preff = "px"; | ||||
321 | | ||||
322 | KisIntParseSpinBox spinBox; | ||||
323 | spinBox.setMaximum(999); | ||||
324 | spinBox.setMinimum(-999); | ||||
325 | spinBox.setPrefix(preff); | ||||
326 | | ||||
327 | for (int i = 0; i < intExprs.size(); i++) { | ||||
328 | | ||||
329 | spinBox.clearFocus(); | ||||
330 | spinBox.clear(); //clear all | ||||
331 | QTest::keyClicks(&spinBox, intExprs[i]); | ||||
332 | spinBox.clearFocus(); | ||||
333 | | ||||
334 | int resultParser = KisNumericParser::parseIntegerMathExpr(intExprs[i]); | ||||
335 | int valueSpinBox = spinBox.value(); | ||||
336 | | ||||
337 | bool test = resultParser == valueSpinBox; | ||||
338 | | ||||
339 | QVERIFY2(test, QString("Failed with expression %1, result is %2, value is %3") | ||||
340 | .arg(intExprs[i]).arg(resultParser).arg(valueSpinBox).toStdString().c_str()); | ||||
341 | | ||||
342 | spinBox.setValue(0); | ||||
343 | } | ||||
344 | | ||||
345 | //verify that the prefix don't appear in the clean text in case of error. | ||||
346 | for (int i = 0; i < intWrongExprs.size(); i++) { | ||||
347 | | ||||
348 | if (intWrongExprs[i].startsWith(preff)) { | ||||
349 | continue; | ||||
350 | } | ||||
351 | | ||||
352 | spinBox.clearFocus(); | ||||
353 | spinBox.clear(); //clear all | ||||
354 | QTest::keyClicks(&spinBox, intWrongExprs[i]); | ||||
355 | spinBox.clearFocus(); | ||||
356 | | ||||
357 | QVERIFY2(!spinBox.cleanText().startsWith(preff), "SpinBox failed to remove prefix from clean text in error state."); | ||||
358 | | ||||
359 | spinBox.setValue(0.0); | ||||
360 | | ||||
361 | } | ||||
362 | | ||||
363 | } | ||||
364 | | ||||
365 | QTEST_MAIN(KisParseSpinBoxesTest) |