GCC Code Coverage Report


Directory: ./
File: autotests/config.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 41 66 62.1%
Branches: 20 68 29.4%

Line Branch Exec Source
1 /*
2 SPDX-FileCopyrightText: 2020 Roman Gilg <subdiff@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only
5 */
6 #include <QCoreApplication>
7 #include <QObject>
8 #include <QtTest>
9 #include <memory>
10
11 #include "config.h"
12 #include "getconfigoperation.h"
13 #include "output.h"
14
15 using namespace Disman;
16
17 class TestConfig : public QObject
18 {
19 Q_OBJECT
20
21 private Q_SLOTS:
22 void initTestCase();
23
24 void test_load();
25 void test_compare_simple_data();
26 void test_compare_outputs();
27
28 private:
29 ConfigPtr load_config(std::string file_name);
30 };
31
32 1 void TestConfig::initTestCase()
33 {
34 1 qputenv("DISMAN_LOGGING", "false");
35 1 qputenv("DISMAN_IN_PROCESS", "1");
36 1 qputenv("DISMAN_BACKEND", "fake");
37 1 }
38
39 2 ConfigPtr TestConfig::load_config(std::string file_name)
40 {
41 2 auto test_data = std::string("TEST_DATA=") + std::string(TEST_DATA) + file_name;
42 2 qputenv("DISMAN_BACKEND_ARGS", test_data.c_str());
43
44 2 auto op = new GetConfigOperation();
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!op->exec()) {
46 qDebug() << "GetConfigOperation::exec failed.";
47 return nullptr;
48 }
49
50 2 auto config = op->config();
51
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!config) {
52 qDebug() << "GetConfigOperation returned null config.";
53 return nullptr;
54 }
55
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!config->valid()) {
56 qDebug() << "GetConfigOperation returned invalid config.";
57 return nullptr;
58 }
59
60 2 return config;
61 2 }
62
63 1 void TestConfig::test_load()
64 {
65 1 auto config = load_config("singleoutput.json");
66
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
67
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QVERIFY(config->valid());
68
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
69
70 void TestConfig::test_compare_simple_data()
71 {
72 auto config = load_config("singleoutput.json");
73 QVERIFY(config);
74
75 QVERIFY(!config->compare(nullptr));
76 QVERIFY(config->compare(config));
77
78 auto config2 = config->clone();
79 QVERIFY(config2);
80
81 QVERIFY(config->compare(config2));
82 QVERIFY(config2->compare(config));
83
84 config2->set_valid(false);
85 QVERIFY(!config->compare(config2));
86 QVERIFY(!config2->compare(config));
87
88 config2->set_valid(true);
89 QVERIFY(config->compare(config2));
90
91 QCOMPARE(config2->cause(), Config::Cause::file);
92 config2->set_cause(Config::Cause::generated);
93 QVERIFY(!config->compare(config2));
94
95 config2->set_cause(Config::Cause::file);
96 QVERIFY(config->compare(config2));
97 }
98
99 1 void TestConfig::test_compare_outputs()
100 {
101 1 auto config = load_config("multipleoutput.json");
102
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config);
103
104 1 auto config2 = config->clone();
105
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(config2);
106
107
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(config->compare(config2));
108
109
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QVERIFY(config->outputs().at(1));
110
1/2
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 QVERIFY(config->outputs().at(2));
111
112 1 auto output3 = config2->outputs().at(2)->clone();
113
114
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(output3);
115 1 output3->set_id(3);
116
117 1 config2->add_output(output3);
118
119
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->compare(config2));
120
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config2->compare(config));
121
122 1 config2->remove_output(3);
123
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(config->compare(config2));
124
125 1 config2->remove_output(2);
126
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config->compare(config2));
127
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QVERIFY(!config2->compare(config));
128
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 }
129
130 1 QTEST_GUILESS_MAIN(TestConfig)
131
132 #include "config.moc"
133