-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtestloadfile.cpp
652 lines (605 loc) · 20.4 KB
/
testloadfile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/**************************************************************************
* This file is part of QXmlEdit *
* Copyright (C) 2015-2022 by Luca Bellonda and individual contributors *
* as indicated in the AUTHORS file *
* lbellonda _at_ gmail.com *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; if not, write to the *
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
**************************************************************************/
#include "testloadfile.h"
#include "app.h"
#include "testhelpers/testmainwindow.h"
#include "modules/xml/xmlerrormanagerdialog.h"
#include "modules/xml/xmlloadcontext.h"
#define TEST_DATA TEST_BASE_DATA "/xml/loading/"
#define FILE_OK TEST_DATA "ok.xml"
#define FILE_KO TEST_DATA "ko.xml"
TestLoadFile::TestLoadFile()
{
_xmlLoadHandlerAnswer = false;
}
TestLoadFile::~TestLoadFile()
{
}
bool TestLoadFile::testFast()
{
_testName = "testFastt" ;
return testErrorsManagement();
}
bool TestLoadFile::testUnit()
{
_testName = "testUnit" ;
if(!loadFileOK()) {
return false;
}
if(!loadFileKO()) {
return false;
}
if(!testLoadWithModifications()) {
return false;
}
#if QT_VERSION >= QT_VERSION_CHECK(5,7,1)
if(!testVerifyLoad1()) {
return false;
}
if(!testVerifyLoad2()) {
return false;
}
#endif
return true;
}
bool TestLoadFile::loadFileOK()
{
_testName = "loadFileOK";
App app;
if(!app.init() ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, true);
app.getUiDelegate()->resetErrorCount();
if(!app.mainWindow()->loadFile(QString(FILE_OK))) {
return error("load file");
}
if(app.getUiDelegate()->errorCount()>0) {
return error(QString("Expected no errors, but found:%1").arg(app.getUiDelegate()->errorCount()));
}
return true ;
}
bool TestLoadFile::loadFileKO()
{
_testName = "loadFileKO";
App app;
if(!app.init() ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, true);
app.getUiDelegate()->resetErrorCount();
if(app.mainWindow()->loadFile(QString(FILE_KO))) {
return error("Loading file was unxpected successfully.");
}
if(app.getUiDelegate()->errorCount()==0) {
return error(QString("Expected errors, but found:%1").arg(app.getUiDelegate()->errorCount()));
}
return true ;
}
bool TestLoadFile::testVerifyLoad1()
{
_testName = "testVerifyLoad1";
App app;
if(!app.init() ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, true);
app.getUiDelegate()->resetErrorCount();
const char *sz = "<?xml version='1.0' standalone='yes'?><root a='aA;'/>";
QString strTest = sz ;
QBuffer ioDevice;
ioDevice.setData(strTest.toUtf8());
if(ioDevice.open(QIODevice::ReadOnly)) {
if( app.mainWindow()->loadFileInnerStream(&ioDevice, "a", false, true)) {
return error("Invalid xml loaded.");
}
if(app.getUiDelegate()->errorCount()==0) {
return error(QString("Expected errors, but found:%1").arg(app.getUiDelegate()->errorCount()));
}
} else {
return error("Unable to load test data.");
}
return true ;
}
bool TestLoadFile::testVerifyLoad2()
{
_testName = "testVerifyLoad2";
App app;
if(!app.init() ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, true);
app.getUiDelegate()->resetErrorCount();
const char *sz = "<?xml version='1.0' standalone='yes'?><root a='a\x1A'/>";
QString strTest = sz ;
QBuffer ioDevice;
ioDevice.setData(strTest.toUtf8());
if(ioDevice.open(QIODevice::ReadOnly)) {
if( app.mainWindow()->loadFileInnerStream(&ioDevice, "a", false, true)) {
return error("Invalid xml loaded.");
}
if(app.getUiDelegate()->errorCount()==0) {
return error(QString("Expected errors, but found:%1").arg(app.getUiDelegate()->errorCount()));
}
} else {
return error("Unable to load test data.");
}
return true ;
}
/* Logic for ask for modifications (to be replicated in tests)
* Operation: before: 1! n after: 1! n
* Reload a a - -
* Drop a - - s
* Session a - - s
* Recent a - - s
* Preferred a - - s
* New Clip a - - s
* New Spec a - - s
* Load a - - s
*
* a: ask unconditionally.
* s: ask if the file is in one of the editor windows.
*
* Ask:
* -if single + mod -> ask
* -if force + mod -> ask
*/
bool TestLoadFile::testLoadWithModifications()
{
_testName = "testLoadWithModifications" ;
if(!loadDrop()) {
return false;
}
if(!loadReload()) {
return false;
}
if(!loadSession()) {
return false;
}
if(!loadRecent()) {
return false;
}
if(!loadPreferredDir()) {
return false;
}
/*if(!loadOpen()) {
return false;
}*/
if(!newSpec()) {
return false;
}
/*if(!newFromClp()) {
return false;
}
if(!newNew()) {
return false;
}*/
return true ;
}
bool TestLoadFile::loadFileForMod(const bool openOtherWindow,
const QString &filePath, const bool isModification,
const int expectingAskingFirst, const int expectingAskingLater,
const int expectedWindows,
bool (TestLoadFile::*method)(App *, const QString &) )
{
App app;
app.setUseTestWindow();
if(!app.init(true) ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, openOtherWindow);
app.getUiDelegate()->resetErrorCount();
if(!app.mainWindow()->loadFile(QString(filePath))) {
return error(QString("Loading file:'%1'").arg(filePath));
}
if( isModification ) {
app.mainWindow()->getRegola()->setModified(true);
}
app.getCurrentUIDelegate()->resetErrorCount();
if( !(this->*method)(&app, filePath) ) {
return error("Method returned false");
}
if( isModification ) {
if(app.getCurrentUIDelegate()->askCountBeforeLoad()!=expectingAskingFirst) {
return error(QString("Expected %1 questions before, but found %2")
.arg(expectingAskingFirst).arg(app.getCurrentUIDelegate()->askCountBeforeLoad()));
}
if(app.getCurrentUIDelegate()->askCountAfterLoad()!=expectingAskingLater) {
return error(QString("Expected %1 questions after, but found %2")
.arg(expectingAskingLater).arg(app.getCurrentUIDelegate()->askCountAfterLoad()));
}
} else {
if(app.getCurrentUIDelegate()->askTotalCount()!=0) {
return error(QString("Expected no ask message, but found:%1").arg(app.getUiDelegate()->askTotalCount()));
}
}
if( app.data()->windowsCount() != expectedWindows ) {
return error(QString("Windows count error: expected %1, but found:%2").arg(app.data()->windowsCount()).arg(expectedWindows));
}
return true ;
}
bool TestLoadFile::loadFileMod(
const bool openOtherWindow,
const int expectingAskingFirst, const int expectingAskingLater,
const int expectedWindows,
bool (TestLoadFile::*method)(App *, const QString &) )
{
return loadFileForMod(openOtherWindow, FILE_OK, true,
expectingAskingFirst, expectingAskingLater,
expectedWindows,
method );
}
bool TestLoadFile::loadFileNoMod(
const bool openOtherWindow,
const int expectingAskingFirst, const int expectingAskingLater,
const int expectedWindows,
bool (TestLoadFile::*method)(App *, const QString &) )
{
return loadFileForMod(openOtherWindow, FILE_OK, false,
expectingAskingFirst, expectingAskingLater,
expectedWindows,
method );
}
bool TestLoadFile::actionDrop(App *app, const QString & filePath)
{
return app->mainWindow()->openDroppedFile(filePath);
}
bool TestLoadFile::loadDrop()
{
_testName = "loadDrop" ;
// no mod
if( !loadFileNoMod(true, 0, 0, 1, &TestLoadFile::actionDrop ) ) {
return false;
}
// mod
if( !loadFileMod(true, 0, 1, 1, &TestLoadFile::actionDrop ) ) {
return false;
}
if( !loadFileNoMod(false, 0, 0, 1, &TestLoadFile::actionDrop ) ) {
return false;
}
if( !loadFileMod(false, 1, 0, 1, &TestLoadFile::actionDrop ) ) {
return false;
}
return true;
}
bool TestLoadFile::actionReload(App *app, const QString & /*filePath*/)
{
return app->mainWindow()->reload();
}
bool TestLoadFile::loadReload()
{
_testName = "loadReload" ;
if( !loadFileNoMod(true, 0, 0, 1, &TestLoadFile::actionReload ) ) {
return false;
}
if( !loadFileMod(true, 1, 0, 1, &TestLoadFile::actionReload ) ) {
return false;
}
if( !loadFileNoMod(false, 0, 0, 1, &TestLoadFile::actionReload ) ) {
return false;
}
if( !loadFileMod(false, 1, 0, 1, &TestLoadFile::actionReload ) ) {
return false;
}
return true;
}
bool TestLoadFile::actionLoadSession(App *app, const QString & filePath)
{
return app->mainWindow()->onSessionfileLoadRequest(filePath);
}
bool TestLoadFile::loadSession()
{
_testName = "loadSession" ;
if( !loadFileNoMod(true, 0, 0, 1, &TestLoadFile::actionLoadSession ) ) {
return false;
}
if( !loadFileMod(true, 0, 1, 1, &TestLoadFile::actionLoadSession ) ) {
return false;
}
if( !loadFileNoMod(false, 0, 0, 1, &TestLoadFile::actionLoadSession ) ) {
return false;
}
if( !loadFileMod(false, 1, 0, 1, &TestLoadFile::actionLoadSession ) ) {
return false;
}
return true;
}
bool TestLoadFile::actionLoadRecent(App *app, const QString & filePath)
{
return app->mainWindow()->recentFile(filePath);
}
bool TestLoadFile::loadRecent()
{
_testName = "loadRecent" ;
if( !loadFileNoMod(true, 0, 0, 1, &TestLoadFile::actionLoadRecent ) ) {
return false;
}
if( !loadFileMod(true, 0, 1, 1, &TestLoadFile::actionLoadRecent ) ) {
return false;
}
if( !loadFileNoMod(false, 0, 0, 1, &TestLoadFile::actionLoadRecent ) ) {
return false;
}
if( !loadFileMod(false, 1, 0, 1, &TestLoadFile::actionLoadRecent ) ) {
return false;
}
return true;
}
bool TestLoadFile::actionLoadPreferredDir(App *app, const QString & filePath)
{
return app->mainWindow()->preferredDirLoadFile(filePath);
}
bool TestLoadFile::loadPreferredDir()
{
_testName = "loadPreferredDir" ;
if( !loadFileNoMod(true, 0, 0, 1, &TestLoadFile::actionLoadPreferredDir ) ) {
return false;
}
if( !loadFileMod(true, 0, 1, 1, &TestLoadFile::actionLoadPreferredDir ) ) {
return false;
}
if( !loadFileNoMod(false, 0, 0, 1, &TestLoadFile::actionLoadPreferredDir ) ) {
return false;
}
if( !loadFileMod(false, 0, 0, 1, &TestLoadFile::actionLoadPreferredDir ) ) {
return false;
}
return true;
}
bool TestLoadFile::actionNewSpecialized(App *app, const QString & /*filePath*/)
{
app->mainWindow()->on_actionNewMavenPOM_triggered();
return true;
}
bool TestLoadFile::newSpec()
{
_testName = "newSpec" ;
if( !loadFileNoMod(true, 0, 0, 2, &TestLoadFile::actionNewSpecialized ) ) {
return false;
}
if( !loadFileMod(true, 0, 0, 2, &TestLoadFile::actionNewSpecialized ) ) {
return false;
}
if( !loadFileNoMod(false, 0, 0, 1, &TestLoadFile::actionNewSpecialized ) ) {
return false;
}
if( !loadFileMod(false, 1, 0, 1, &TestLoadFile::actionNewSpecialized ) ) {
return false;
}
return true;
}
//----------------------------
bool TestLoadFile::testErrorsManagement()
{
_testName = "testErrorsManagement" ;
if( !testErrorLoadUnit() ) {
return false;
}
if( !testError0Pos() ) {
return false;
}
if( !testErrorLoadAnyway() ) {
return false;
}
if( !testErrorNOK() ) {
return false;
}
return true;
}
bool TestLoadFile::testErrorLoadUnit()
{
_testName = "testErrorLoadUnit";
int res = 0;
int expected = -1 ;
res = XMLErrorManagerDialog::charsBeforeError( 0, XMLErrorManagerDialog::CharsBefore );
if( res != expected ) {
return error(QString("Expected %1 returned '%2'").arg(expected).arg(res));
}
expected = 0 ;
res = XMLErrorManagerDialog::charsBeforeError( 1, XMLErrorManagerDialog::CharsBefore );
if( res != expected ) {
return error(QString("Expected %1 returned '%2'").arg(expected).arg(res));
}
expected = XMLErrorManagerDialog::CharsBefore-2 ;
res = XMLErrorManagerDialog::charsBeforeError( XMLErrorManagerDialog::CharsBefore-1, XMLErrorManagerDialog::CharsBefore );
if( res != expected ) {
return error(QString("Expected %1 returned '%2'").arg(expected).arg(res));
}
expected = XMLErrorManagerDialog::CharsBefore ;
res = XMLErrorManagerDialog::charsBeforeError( XMLErrorManagerDialog::CharsBefore, XMLErrorManagerDialog::CharsBefore );
if( res != expected ) {
return error(QString("Expected %1 returned '%2'").arg(expected).arg(res));
}
expected = XMLErrorManagerDialog::CharsBefore ;
res = XMLErrorManagerDialog::charsBeforeError( XMLErrorManagerDialog::CharsBefore+1, XMLErrorManagerDialog::CharsBefore );
if( res != expected ) {
return error(QString("Expected %1 returned '%2'").arg(expected).arg(res));
}
expected = XMLErrorManagerDialog::CharsBefore/2 -1 ;
res = XMLErrorManagerDialog::charsBeforeError( XMLErrorManagerDialog::CharsBefore/2, XMLErrorManagerDialog::CharsBefore );
if( res != expected ) {
return error(QString("Expected %1 returned '%2'").arg(expected).arg(res));
}
return true;
}
static QString makeString(const int val )
{
QString str;
str.fill('1', val);
return str;
}
bool TestLoadFile::testError0Pos()
{
_testName = "testError00Pos" ;
{
QString dataIn = "" ;
if(!testAnErrorPos( dataIn, 0, 0, 0, "", "", "")) {
return false;
}
}
_testName = "testError01Pos" ;
{
QString dataIn = "a" ;
if(!testAnErrorPos( dataIn, 0, 1, 1, "", "a", "")) {
return false;
}
}
_testName = "testError02Pos" ;
{
QString dataIn = "azzz" ;
if(!testAnErrorPos( dataIn, 0, 1, 1, "", "a", "zzz")) {
return false;
}
}
_testName = "testError03Pos" ;
{
QString dataIn = "12345a" ;
if(!testAnErrorPos( dataIn, 0, 6, 6, "12345", "a", "")) {
return false;
}
}
_testName = "testError04Pos" ;
{
QString dataIn = "12345avv" ;
if(!testAnErrorPos( dataIn, 0, 6, 6, "12345", "a", "vv")) {
return false;
}
}
_testName = "testError05aPos" ;
{
const int size = XMLErrorManagerDialog::BlockSize;
QString dataIn = makeString(size);
dataIn += "avv" ;
if(!testAnErrorPos( dataIn, 0, size, size, "11111111111111111111", "a", "vv")) {
return false;
}
}
_testName = "testError05Pos" ;
{
const int size = XMLErrorManagerDialog::BlockSize;
QString dataIn = makeString(size-2);
dataIn += "22avv" ;
if(!testAnErrorPos( dataIn, 0, size, size, "11111111111111111122", "a", "vv")) {
return false;
}
}
_testName = "testError06Pos" ;
{
const int size = 2*XMLErrorManagerDialog::BlockSize;
QString dataIn = makeString(size-1);
dataIn += "2avv" ;
if(!testAnErrorPos( dataIn, 0, size, size, "11111111111111111112", "a", "vv")) {
return false;
}
}
_testName = "testError07Pos" ;
{
const int size = 2*XMLErrorManagerDialog::BlockSize;
QString dataIn = makeString(size);
dataIn += "avv" ;
if(!testAnErrorPos( dataIn, 0, size, size, "11111111111111111111", "a", "vv")) {
return false;
}
}
_testName = "testError07Pos" ;
{
const int size = (6*XMLErrorManagerDialog::BlockSize);
QString dataIn = makeString(size);
dataIn += "222avv" ;
if(!testAnErrorPos( dataIn, 0, size+3, size+3, "11111111111111111222", "a", "vv")) {
return false;
}
}
_testName = "testError08Pos" ;
{
const int size = (6*XMLErrorManagerDialog::BlockSize);
QString dataIn = makeString(size);
dataIn += "222a" ;
if(!testAnErrorPos( dataIn, 0, size+3, size+3, "11111111111111111222", "a", "")) {
return false;
}
}
return true ;
}
bool TestLoadFile::testAnErrorPos( const QString &dataIn,
const qint64 line, const qint64 column, const qint64 offset,
const QString &expectedBefore,
const QString &expectedIn,
const QString &expectedAfter)
{
QByteArray array = dataIn.toUtf8();
QBuffer buffer(&array);
buffer.open(QBuffer::ReadOnly);
QXmlStreamReader reader(&buffer);
XMLLoadContext context;
context.setEncoding("UTF-8");
context.setErrorPosition(line, column, offset);
XMLErrorManagerDialog dlg( NULL, &context, &reader);
if(expectedBefore != dlg._beforeString ) {
return error(QString("Before: expected '%1', found: '%2'").arg(expectedBefore).arg(dlg._beforeString));
}
if(expectedIn != dlg._errorPosString ) {
return error(QString("Pos: expected '%1', found: '%2'").arg(expectedIn).arg(dlg._errorPosString));
}
if(expectedAfter != dlg._afterString ) {
return error(QString("After: expected '%1', found: '%2'").arg(expectedAfter).arg(dlg._afterString));
}
return true;
}
#define FILE_KO_PARTIAL TEST_DATA "partialko.xml"
#define FILE_KO_NOPE TEST_DATA "partialko.xml"
bool TestLoadFile::testErrorLoadAnyway()
{
_testName = "testErrorLoadAnyway" ;
_xmlLoadHandlerAnswer = true ;
// next: readonly
App app;
if(!app.init() ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, false);
app.mainWindow()->setLoadErrorHandler(this);
if(!app.mainWindow()->loadFile(QString(FILE_KO_PARTIAL))) {
return error("load partial error file should not be error");
}
return true ;
}
bool TestLoadFile::testErrorNOK()
{
_testName = "testErrorLoadNOK" ;
_xmlLoadHandlerAnswer = false ;
App app;
if(!app.init() ) {
return error("init");
}
Config::saveBool(Config::KEY_GENERAL_OPEN_NEWWINDOW, false);
app.mainWindow()->setLoadErrorHandler(this);
if(app.mainWindow()->loadFile(QString(FILE_KO_NOPE))) {
return error("load partial error file should be error");
}
return true ;
}
bool TestLoadFile::showErrorAndAskUserIfContinue(QWidget * /*parent*/, XMLLoadContext * /*context*/, QXmlStreamReader * /*xmlReader*/)
{
return _xmlLoadHandlerAnswer;
}