GCC Code Coverage Report


Directory: ./
File: autotests/testlog.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 59 59 100.0%
Branches: 33 60 55.0%

Line Branch Exec Source
1 /*************************************************************************************
2 * Copyright 2016 by Sebastian Kügler <sebas@kde.org> *
3 * *
4 * This library is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * This library 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 GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this library; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
17 *************************************************************************************/
18 #include <QLoggingCategory>
19 #include <QObject>
20 #include <QtTest>
21
22 #include "log.h"
23
24 Q_DECLARE_LOGGING_CATEGORY(DISMAN_TESTLOG)
25
26
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
2 Q_LOGGING_CATEGORY(DISMAN_TESTLOG, "disman.testlog")
27
28 using namespace Disman;
29
30 auto DISMAN_LOGGING = "DISMAN_LOGGING";
31
32 class TestLog : public QObject
33 {
34 Q_OBJECT
35
36 private Q_SLOTS:
37 void init();
38 void initTestCase();
39 void cleanupTestCase();
40 void testContext();
41 void testEnabled();
42 void testLog();
43
44 private:
45 QString m_defaultLogFile;
46 };
47
48 3 void TestLog::init()
49 {
50 3 QStandardPaths::setTestModeEnabled(true);
51 6 m_defaultLogFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)
52 9 + QLatin1String("/disman/disman.log");
53 3 }
54
55 1 void TestLog::initTestCase()
56 {
57
58 1 qputenv(DISMAN_LOGGING, QByteArray("true"));
59 1 }
60
61 1 void TestLog::cleanupTestCase()
62 {
63 1 qunsetenv(DISMAN_LOGGING);
64 1 }
65
66 1 void TestLog::testContext()
67 {
68 1 auto log = Log::instance();
69 1 QString ctx = QStringLiteral("context text");
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 QVERIFY(log != nullptr);
71 1 log->set_context(ctx);
72
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(log->context(), ctx);
73
74
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete log;
75
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 }
76
77 1 void TestLog::testEnabled()
78 {
79 1 qputenv(DISMAN_LOGGING, QByteArray("faLSe"));
80
81 1 auto log = Log::instance();
82
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QCOMPARE(log->enabled(), false);
83
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(log->file(), QString());
84
85
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete log;
86 1 qunsetenv(DISMAN_LOGGING);
87
88 1 log = Log::instance();
89
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QCOMPARE(log->enabled(), false);
90
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
1 QCOMPARE(log->file(), QString());
91
92
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete log;
93 1 qputenv(DISMAN_LOGGING, QByteArray("truE"));
94
95 1 log = Log::instance();
96
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QCOMPARE(log->enabled(), true);
97
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(log->file(), m_defaultLogFile);
98
99
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete log;
100 }
101
102 1 void TestLog::testLog()
103 {
104 1 auto log = Log::instance();
105 Q_UNUSED(log);
106
107 1 QFile lf(m_defaultLogFile);
108 1 lf.remove();
109
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!lf.exists());
110
111 1 QString logmsg = QStringLiteral("This is a log message. ♥");
112 1 Log::log(logmsg);
113
114
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(lf.exists());
115
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(lf.remove());
116
117
2/2
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
2 qCDebug(DISMAN_TESTLOG) << "qCDebug message from testlog";
118
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(lf.exists());
119
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(lf.remove());
120
121
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 delete Log::instance();
122
123 // Make sure on log file gets written when disabled
124 1 qputenv(DISMAN_LOGGING, "false");
125
126
2/2
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
2 qCDebug(DISMAN_TESTLOG) << logmsg;
127
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 QCOMPARE(Log::instance()->enabled(), false);
128
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!lf.exists());
129
130 1 Log::log(logmsg);
131
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 QVERIFY(!lf.exists());
132
133 // Make sure we don't crash on cleanup
134
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 delete Log::instance();
135
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 delete Log::instance();
136
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 }
137
138 1 QTEST_MAIN(TestLog)
139
140 #include "testlog.moc"
141