File: test_problemmodel.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (570 lines) | stat: -rw-r--r-- 17,268 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2015 Laszlo Kis-Adam <laszlo.kis-adam@kdemail.net>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include <QTest>

#include <shell/problemmodel.h>
#include <shell/problem.h>
#include <shell/problemconstants.h>
#include <language/editor/documentrange.h>

#include <tests/testcore.h>
#include <tests/autotestshell.h>

#include <KLocalizedString>

#define MYCOMPARE(actual, expected) \
    if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__)) \
    return false

#define MYVERIFY(statement) \
    if (!QTest::qVerify((statement), #statement, "", __FILE__, __LINE__))\
        return false

using namespace KDevelop;

class TestProblemModel : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();

    void testNoGrouping();
    void testPathGrouping();
    void testSeverityGrouping();
    void testPlaceholderText();

private:
    QString prependPathRoot(QString const& currentPath) const;
    void generateProblems();
    bool checkIsSame(int row, const QModelIndex &parent, const IProblem::Ptr &problem);
    bool checkDiagnostics(int row, const QModelIndex &parent);
    bool checkDisplay(int row, const QModelIndex &parent, const IProblem::Ptr &problem);
    bool checkLabel(int row, const QModelIndex &parent, const QString &label);
    bool checkPathGroup(int row, const IProblem::Ptr &problem);
    bool checkSeverityGroup(int row, const IProblem::Ptr &problem);

    QScopedPointer<ProblemModel> m_model;
    QVector<IProblem::Ptr> m_problems;
    IProblem::Ptr m_diagnosticTestProblem;
};

void TestProblemModel::initTestCase()
{
    AutoTestShell::init();
    TestCore::initialize(Core::NoUi);

    m_model.reset(new ProblemModel(nullptr));
    m_model->setScope(BypassScopeFilter);

    generateProblems();
}

void TestProblemModel::cleanupTestCase()
{
    TestCore::shutdown();
}

void TestProblemModel::testNoGrouping()
{
    m_model->setGrouping(NoGrouping);
    m_model->setSeverity(IProblem::Hint);

    QCOMPARE(m_model->rowCount(), 0);

    // Check if setting the problems works
    m_model->setProblems(m_problems);
    QCOMPARE(m_model->rowCount(), 3);

    for (int i = 0; i < m_model->rowCount(); i++) {
        QVERIFY(checkIsSame(i, QModelIndex(), m_problems[i]));
    }

    // Check if displaying various data parts works
    QVERIFY(checkDisplay(0, QModelIndex(), m_problems[0]));

    // Check if clearing the problems works
    m_model->clearProblems();
    QCOMPARE(m_model->rowCount(), 0);

    // Check if adding the problems works
    int c = 0;
    for (const IProblem::Ptr& p : qAsConst(m_problems)) {
        m_model->addProblem(p);
        c++;

        QCOMPARE(m_model->rowCount(), c);
    }

    for (int i = 0; i < m_model->rowCount(); i++) {
        QVERIFY(checkIsSame(i, QModelIndex(), m_problems[i]));
    }


    // Check if filtering works
    // old-style setSeverity
    // Error filter
    m_model->setSeverity(IProblem::Error);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));

    // Warning filter
    m_model->setSeverity(IProblem::Warning);
    QCOMPARE(m_model->rowCount(), 2);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));
    QVERIFY(checkIsSame(1, QModelIndex(), m_problems[1]));

    // Hint filter
    m_model->setSeverity(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 3);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));
    QVERIFY(checkIsSame(1, QModelIndex(), m_problems[1]));
    QVERIFY(checkIsSame(2, QModelIndex(), m_problems[2]));

    // Check if filtering works
    // new style
    // Error filter
    m_model->setSeverities(IProblem::Error);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));

    // Warning filter
    m_model->setSeverities(IProblem::Warning);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[1]));

    // Hint filter
    m_model->setSeverities(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[2]));

    // Error + Hint filter
    m_model->setSeverities(IProblem::Error | IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 2);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));
    QVERIFY(checkIsSame(1, QModelIndex(), m_problems[2]));

    m_model->setSeverities(IProblem::Error | IProblem::Warning | IProblem::Hint);

    // Check if diagnostics are added properly
    m_model->clearProblems();
    m_model->addProblem(m_diagnosticTestProblem);
    QVERIFY(checkDiagnostics(0, QModelIndex()));

    m_model->clearProblems();
}

void TestProblemModel::testPathGrouping()
{
    m_model->setGrouping(PathGrouping);
    m_model->setSeverity(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 0);

    // Check if setting problems works
    m_model->setProblems(m_problems);
    QCOMPARE(m_model->rowCount(), 3);

    for (int i = 0; i < m_model->rowCount(); i++) {
        QVERIFY(checkLabel(i, QModelIndex(), m_problems[i]->finalLocation().document.str()));

        QModelIndex idx = m_model->index(i, 0);
        QVERIFY(idx.isValid());
        QVERIFY(checkIsSame(0, idx, m_problems[i]));
    }

    // Check if displaying various data parts works
    {
        QModelIndex idx = m_model->index(0, 0);
        QVERIFY(idx.isValid());
        QVERIFY(checkDisplay(0, idx, m_problems[0]));
    }

    // Check if clearing works
    m_model->clearProblems();
    QCOMPARE(m_model->rowCount(), 0);

    // Check if add problems works
    int c = 0;
    for (const IProblem::Ptr& p : qAsConst(m_problems)) {
        m_model->addProblem(p);
        c++;

        QCOMPARE(m_model->rowCount(), c);
    }

    for (int i = 0; i < m_model->rowCount(); i++) {
        QVERIFY(checkLabel(i, QModelIndex(), m_problems[i]->finalLocation().document.str()));

        QModelIndex idx = m_model->index(i, 0);
        QVERIFY(idx.isValid());
        QVERIFY(checkIsSame(0, idx, m_problems[i]));
    }

    // Check if filtering works
    // old-style setSeverity
    // Error filtering
    m_model->setSeverity(IProblem::Error);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkPathGroup(0, m_problems[0]));

    // Warning filtering
    m_model->setSeverity(IProblem::Warning);
    QCOMPARE(m_model->rowCount(), 2);
    QVERIFY(checkPathGroup(0, m_problems[0]));
    QVERIFY(checkPathGroup(1, m_problems[1]));

    // Hint filtering
    m_model->setSeverity(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 3);
    QVERIFY(checkPathGroup(0, m_problems[0]));
    QVERIFY(checkPathGroup(1, m_problems[1]));
    QVERIFY(checkPathGroup(2, m_problems[2]));

    // Check if filtering works
    // new style
    // Error filtering
    m_model->setSeverities(IProblem::Error);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkPathGroup(0, m_problems[0]));

    // Warning filtering
    m_model->setSeverities(IProblem::Warning);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkPathGroup(0, m_problems[1]));

    // Hint filtering
    m_model->setSeverities(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkPathGroup(0, m_problems[2]));

    // Error + Hint filtering
    m_model->setSeverities(IProblem::Error | IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 2);
    QVERIFY(checkPathGroup(0, m_problems[0]));
    QVERIFY(checkPathGroup(1, m_problems[2]));

    m_model->setSeverities(IProblem::Error | IProblem::Warning | IProblem::Hint);

    // Check if diagnostics get to the right place
    m_model->clearProblems();
    m_model->addProblem(m_diagnosticTestProblem);
    {
        QModelIndex parent = m_model->index(0, 0);
        QVERIFY(parent.isValid());
        checkDiagnostics(0, parent);
    }

    m_model->clearProblems();
}

void TestProblemModel::testSeverityGrouping()
{
    m_model->setGrouping(SeverityGrouping);
    m_model->setSeverity(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 3);

    // Check if setting problems works
    m_model->setProblems(m_problems);
    QCOMPARE(m_model->rowCount(), 3);
    for (int i = 0; i < m_model->rowCount(); i++) {
        QVERIFY(checkSeverityGroup(i, m_problems[i]));
    }

    // Check if displaying works
    for (int i = 0; i < m_model->rowCount(); i++) {
        QModelIndex parent = m_model->index(i, 0);
        QVERIFY(parent.isValid());

        QVERIFY(checkDisplay(0, parent, m_problems[i]));
    }

    // Check if clearing works
    m_model->clearProblems();
    QCOMPARE(m_model->rowCount(), 3);

    // Check if adding problems works
    int c = 0;
    for (const IProblem::Ptr& p : qAsConst(m_problems)) {
        m_model->addProblem(p);
        QVERIFY(checkSeverityGroup(c, m_problems[c]));
        c++;
    }

    // Check if filtering works
    // old-style setSeverity
    // Error filtering
    m_model->setSeverity(IProblem::Error);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(0, m_problems[0]);

    // Warning filtering
    m_model->setSeverity(IProblem::Warning);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(0, m_problems[0]);
    checkSeverityGroup(1, m_problems[1]);

    // Hint filtering
    m_model->setSeverity(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(0, m_problems[0]);
    checkSeverityGroup(1, m_problems[1]);
    checkSeverityGroup(2, m_problems[2]);

    // Check if filtering works
    // Error filtering
    m_model->setSeverities(IProblem::Error);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(0, m_problems[0]);

    // Warning filtering
    m_model->setSeverities(IProblem::Warning);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(1, m_problems[1]);

    // Hint filtering
    m_model->setSeverities(IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(2, m_problems[2]);

    // Error + Hint filtering
    m_model->setSeverities(IProblem::Error | IProblem::Hint);
    QCOMPARE(m_model->rowCount(), 3);
    checkSeverityGroup(0, m_problems[0]);
    checkSeverityGroup(2, m_problems[2]);

    m_model->setSeverities(IProblem::Error | IProblem::Warning | IProblem::Hint);

    // Check if diagnostics get to the right place
    m_model->clearProblems();
    m_model->addProblem(m_diagnosticTestProblem);
    {
        QModelIndex parent = m_model->index(0, 0);
        QVERIFY(parent.isValid());

        checkDiagnostics(0, parent);
    }

    m_model->clearProblems();
}

void TestProblemModel::testPlaceholderText()
{
    const QString text1 = QStringLiteral("testPlaceholderText1");
    const QString text2 = QStringLiteral("testPlaceholderText2");
    const QString empty;

    m_model->setGrouping(NoGrouping);

    // Test model with empty placeholder text

    QCOMPARE(m_model->rowCount(), 0);
    m_model->setPlaceholderText(empty);
    QCOMPARE(m_model->rowCount(), 0);
    m_model->setProblems(m_problems);
    QCOMPARE(m_model->rowCount(), 3);
    m_model->clearProblems();
    QCOMPARE(m_model->rowCount(), 0);

    // Test empty model with non-empty placeholder text

    m_model->setPlaceholderText(text1);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkLabel(0, QModelIndex(), text1));

    m_model->setPlaceholderText(text2);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkLabel(0, QModelIndex(), text2));

    // Test non-empty model with non-empty placeholder text

    m_model->setProblems(m_problems);
    QCOMPARE(m_model->rowCount(), 3);

    m_model->clearProblems();
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkLabel(0, QModelIndex(), text2));

    m_model->addProblem(m_problems[0]);
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));

    m_model->addProblem(m_problems[1]);
    QCOMPARE(m_model->rowCount(), 2);
    QVERIFY(checkIsSame(1, QModelIndex(), m_problems[1]));

    m_model->setPlaceholderText(text1);
    QCOMPARE(m_model->rowCount(), 2);
    QVERIFY(checkIsSame(0, QModelIndex(), m_problems[0]));
    QVERIFY(checkIsSame(1, QModelIndex(), m_problems[1]));

    m_model->setProblems({});
    QCOMPARE(m_model->rowCount(), 1);
    QVERIFY(checkLabel(0, QModelIndex(), text1));
}

// Needed because util/Path.cpp asserts e.g. C: on Windows
QString TestProblemModel::prependPathRoot(QString const& currentPath) const
{
#ifdef Q_OS_WIN
    QString pathRoot = QStringLiteral("x:/");
#else
    QString pathRoot = QStringLiteral("/");
#endif
    return pathRoot + currentPath;
}

// Generate 3 problems, all with different paths, different severity
// Also generates a problem with diagnostics
void TestProblemModel::generateProblems()
{
    IProblem::Ptr p1(new DetectedProblem());
    IProblem::Ptr p2(new DetectedProblem());
    IProblem::Ptr p3(new DetectedProblem());

    DocumentRange r1;
    r1.document = IndexedString( prependPathRoot("just/a/random/path") );

    p1->setDescription(QStringLiteral("PROBLEM1"));
    p1->setSeverity(IProblem::Error);
    p1->setFinalLocation(r1);

    DocumentRange r2;
    r2.document = IndexedString( prependPathRoot("just/another/path") );

    p2->setDescription(QStringLiteral("PROBLEM2"));
    p2->setSeverity(IProblem::Warning);
    p2->setFinalLocation(r2);

    DocumentRange r3;
    r3.document = IndexedString( prependPathRoot("yet/another/test/path") );

    p2->setDescription(QStringLiteral("PROBLEM3"));
    p3->setSeverity(IProblem::Hint);
    p3->setFinalLocation(r3);

    m_problems.push_back(p1);
    m_problems.push_back(p2);
    m_problems.push_back(p3);

    // Problem for diagnostic testing
    IProblem::Ptr p(new DetectedProblem());
    DocumentRange r;
    r.document = IndexedString("DIAGTEST");
    p->setFinalLocation(r);
    p->setDescription(QStringLiteral("PROBLEM"));
    p->setSeverity(IProblem::Error);

    IProblem::Ptr d(new DetectedProblem());
    d->setDescription(QStringLiteral("DIAG"));

    IProblem::Ptr dd(new DetectedProblem());
    dd->setDescription(QStringLiteral("DIAGDIAG"));
    d->addDiagnostic(dd);
    p->addDiagnostic(d);
    m_diagnosticTestProblem = p;
}

bool TestProblemModel::checkIsSame(int row, const QModelIndex &parent, const IProblem::Ptr &problem)
{
    QModelIndex idx;

    idx = m_model->index(row, 0, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), problem->description());

    return true;
}

bool TestProblemModel::checkDiagnostics(int row, const QModelIndex &parent)
{
    MYCOMPARE(m_model->rowCount(parent), 1);

    QModelIndex idx;
    idx = m_model->index(row, 0, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), m_diagnosticTestProblem->description());

    QModelIndex diagidx;
    IProblem::Ptr diag = m_diagnosticTestProblem->diagnostics().at(0);
    diagidx = m_model->index(0, 0, idx);
    MYVERIFY(diagidx.isValid());
    MYCOMPARE(m_model->data(diagidx).toString(), diag->description());

    QModelIndex diagdiagidx;
    IProblem::Ptr diagdiag = diag->diagnostics().at(0);
    diagdiagidx = m_model->index(0, 0, diagidx);
    MYVERIFY(diagdiagidx.isValid());
    MYCOMPARE(m_model->data(diagdiagidx).toString(), diagdiag->description());

    return true;
}

bool TestProblemModel::checkDisplay(int row, const QModelIndex &parent, const IProblem::Ptr &problem)
{
    QModelIndex idx;

    idx = m_model->index(row, 0, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), problem->description());

    idx = m_model->index(row, 1, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), problem->sourceString());

    idx = m_model->index(row, 2, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), problem->finalLocation().document.str());

    idx = m_model->index(row, 3, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), QString::number(problem->finalLocation().start().line() + 1));

    idx = m_model->index(row, 4, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), QString::number(problem->finalLocation().start().column() + 1));

    return true;
}

bool TestProblemModel::checkLabel(int row, const QModelIndex &parent, const QString &label)
{
    QModelIndex idx = m_model->index(row, 0, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), label);

    return true;
}

bool TestProblemModel::checkPathGroup(int row, const IProblem::Ptr &problem)
{
    QModelIndex parent = m_model->index(row, 0);
    MYVERIFY(parent.isValid());
    MYCOMPARE(m_model->data(parent).toString(), problem->finalLocation().document.str());

    QModelIndex idx = m_model->index(0, 0, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), problem->description());

    return true;
}

bool TestProblemModel::checkSeverityGroup(int row, const IProblem::Ptr &problem)
{
    QModelIndex parent = m_model->index(row, 0);
    MYVERIFY(parent.isValid());
    MYCOMPARE(m_model->data(parent).toString(), problem->severityString());

    QModelIndex idx = m_model->index(0, 0, parent);
    MYVERIFY(idx.isValid());
    MYCOMPARE(m_model->data(idx).toString(), problem->description());

    return true;
}

QTEST_MAIN(TestProblemModel)

#include "test_problemmodel.moc"