Skip to content

Commit e6979ab

Browse files
committed
Add unit tests for gui/codeeditorstyle, gui/erroritem, gui/platforms, gui/showtypes
1 parent 45e0eb0 commit e6979ab

13 files changed

Lines changed: 681 additions & 0 deletions

gui/test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
add_custom_target(gui-tests)
22

3+
add_subdirectory(codeeditorstyle)
34
add_subdirectory(cppchecklibrarydata)
5+
add_subdirectory(erroritem)
46
add_subdirectory(filelist)
7+
add_subdirectory(platforms)
58
add_subdirectory(projectfile)
69
add_subdirectory(resultstree)
10+
add_subdirectory(showtypes)
711
add_subdirectory(translationhandler)
812
add_subdirectory(xmlreportv2)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
qt_wrap_cpp(test-codeeditorstyle_SRC testcodeeditorstyle.h)
2+
add_custom_target(build-testcodeeditorstyle-deps SOURCES ${test-codeeditorstyle_SRC})
3+
add_dependencies(gui-build-deps build-testcodeeditorstyle-deps)
4+
add_executable(test-codeeditorstyle
5+
${test-codeeditorstyle_SRC}
6+
testcodeeditorstyle.cpp
7+
${CMAKE_SOURCE_DIR}/gui/codeeditorstyle.cpp
8+
)
9+
target_include_directories(test-codeeditorstyle PRIVATE ${CMAKE_SOURCE_DIR}/gui)
10+
target_link_libraries(test-codeeditorstyle ${QT_CORE_LIB} ${QT_GUI_LIB} ${QT_TEST_LIB})
11+
12+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
13+
if(QT_VERSION VERSION_GREATER_EQUAL "6.9.0")
14+
# caused by Qt generated moc code starting with 6.9.0 - see https://bugreports.qt.io/browse/QTBUG-135638
15+
target_compile_options_safe(test-codeeditorstyle -Wno-ctad-maybe-unsupported)
16+
endif()
17+
endif()
18+
19+
if (REGISTER_GUI_TESTS)
20+
add_test(NAME test-codeeditorstyle COMMAND $<TARGET_FILE:test-codeeditorstyle> -platform offscreen)
21+
endif()
22+
23+
add_dependencies(gui-tests test-codeeditorstyle)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2026 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "testcodeeditorstyle.h"
20+
21+
#include "codeeditorstyle.h"
22+
23+
#include <QDir>
24+
#include <QSettings>
25+
#include <QtTest>
26+
27+
QTEST_MAIN(TestCodeEditorStyle)
28+
29+
void TestCodeEditorStyle::defaultStyles_notEqual() const
30+
{
31+
QVERIFY(defaultStyleLight != defaultStyleDark);
32+
}
33+
34+
void TestCodeEditorStyle::equality_sameStyle() const
35+
{
36+
QVERIFY(defaultStyleLight == defaultStyleLight);
37+
QVERIFY(defaultStyleDark == defaultStyleDark);
38+
}
39+
40+
void TestCodeEditorStyle::equality_differentColor() const
41+
{
42+
CodeEditorStyle style(defaultStyleLight);
43+
style.widgetFGColor = QColor(1, 2, 3);
44+
QVERIFY(style != defaultStyleLight);
45+
}
46+
47+
void TestCodeEditorStyle::getSystemTheme_isSystemTheme() const
48+
{
49+
const CodeEditorStyle sys = CodeEditorStyle::getSystemTheme();
50+
QVERIFY(sys.isSystemTheme());
51+
}
52+
53+
void TestCodeEditorStyle::getSystemTheme_colorsMatchLight() const
54+
{
55+
const CodeEditorStyle sys = CodeEditorStyle::getSystemTheme();
56+
QCOMPARE(sys.widgetFGColor, defaultStyleLight.widgetFGColor);
57+
QCOMPARE(sys.widgetBGColor, defaultStyleLight.widgetBGColor);
58+
QCOMPARE(sys.keywordColor, defaultStyleLight.keywordColor);
59+
QCOMPARE(sys.keywordWeight, defaultStyleLight.keywordWeight);
60+
QCOMPARE(sys.commentColor, defaultStyleLight.commentColor);
61+
}
62+
63+
void TestCodeEditorStyle::saveLoad_darkRoundtrip() const
64+
{
65+
const QString settingsFile = QDir::tempPath() + "/test_codeeditorstyle_dark.ini";
66+
QFile::remove(settingsFile);
67+
68+
{
69+
QSettings settings(settingsFile, QSettings::IniFormat);
70+
CodeEditorStyle::saveSettings(&settings, defaultStyleDark);
71+
}
72+
73+
QSettings settings(settingsFile, QSettings::IniFormat);
74+
const CodeEditorStyle loaded = CodeEditorStyle::loadSettings(&settings);
75+
QCOMPARE(loaded, defaultStyleDark);
76+
77+
QFile::remove(settingsFile);
78+
}
79+
80+
void TestCodeEditorStyle::saveLoad_customColors() const
81+
{
82+
// Create a style that differs from both defaults so it is saved as "Custom"
83+
CodeEditorStyle custom(defaultStyleLight);
84+
custom.widgetFGColor = QColor(11, 22, 33);
85+
custom.keywordColor = QColor(44, 55, 66);
86+
custom.commentColor = QColor(77, 88, 99);
87+
88+
const QString settingsFile = QDir::tempPath() + "/test_codeeditorstyle_custom.ini";
89+
QFile::remove(settingsFile);
90+
91+
{
92+
QSettings settings(settingsFile, QSettings::IniFormat);
93+
CodeEditorStyle::saveSettings(&settings, custom);
94+
}
95+
96+
QSettings settings(settingsFile, QSettings::IniFormat);
97+
const CodeEditorStyle loaded = CodeEditorStyle::loadSettings(&settings);
98+
QCOMPARE(loaded.widgetFGColor, custom.widgetFGColor);
99+
QCOMPARE(loaded.keywordColor, custom.keywordColor);
100+
QCOMPARE(loaded.commentColor, custom.commentColor);
101+
102+
QFile::remove(settingsFile);
103+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* -*- C++ -*-
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2026 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef TESTCODEEDITORSTYLE_H
20+
#define TESTCODEEDITORSTYLE_H
21+
22+
#include <QObject>
23+
24+
class TestCodeEditorStyle : public QObject {
25+
Q_OBJECT
26+
27+
private slots:
28+
void defaultStyles_notEqual() const;
29+
void equality_sameStyle() const;
30+
void equality_differentColor() const;
31+
void getSystemTheme_isSystemTheme() const;
32+
void getSystemTheme_colorsMatchLight() const;
33+
void saveLoad_darkRoundtrip() const;
34+
void saveLoad_customColors() const;
35+
};
36+
37+
#endif // TESTCODEEDITORSTYLE_H

gui/test/erroritem/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
qt_wrap_cpp(test-erroritem_SRC testerroritem.h)
2+
add_custom_target(build-testerroritem-deps SOURCES ${test-erroritem_SRC})
3+
add_dependencies(gui-build-deps build-testerroritem-deps)
4+
add_executable(test-erroritem
5+
${test-erroritem_SRC}
6+
testerroritem.cpp
7+
${CMAKE_SOURCE_DIR}/gui/erroritem.cpp
8+
)
9+
target_include_directories(test-erroritem PRIVATE ${CMAKE_SOURCE_DIR}/gui)
10+
target_link_libraries(test-erroritem ${QT_CORE_LIB} ${QT_TEST_LIB} cppcheck-core)
11+
12+
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
13+
if(QT_VERSION VERSION_GREATER_EQUAL "6.9.0")
14+
# caused by Qt generated moc code starting with 6.9.0 - see https://bugreports.qt.io/browse/QTBUG-135638
15+
target_compile_options_safe(test-erroritem -Wno-ctad-maybe-unsupported)
16+
endif()
17+
endif()
18+
19+
if (REGISTER_GUI_TESTS)
20+
add_test(NAME test-erroritem COMMAND $<TARGET_FILE:test-erroritem>)
21+
endif()
22+
23+
add_dependencies(gui-tests test-erroritem)
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Cppcheck - A tool for static C/C++ code analysis
3+
* Copyright (C) 2007-2026 Cppcheck team.
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include "testerroritem.h"
20+
21+
#include "erroritem.h"
22+
#include "errortypes.h"
23+
24+
#include <QtTest>
25+
26+
QTEST_MAIN(TestErrorItem)
27+
28+
void TestErrorItem::guiSeverity_toString() const
29+
{
30+
QCOMPARE(GuiSeverity::toString(Severity::error), QString("error"));
31+
QCOMPARE(GuiSeverity::toString(Severity::warning), QString("warning"));
32+
QCOMPARE(GuiSeverity::toString(Severity::style), QString("style"));
33+
QCOMPARE(GuiSeverity::toString(Severity::performance), QString("performance"));
34+
QCOMPARE(GuiSeverity::toString(Severity::portability), QString("portability"));
35+
QCOMPARE(GuiSeverity::toString(Severity::information), QString("information"));
36+
QCOMPARE(GuiSeverity::toString(Severity::none), QString(""));
37+
}
38+
39+
void TestErrorItem::guiSeverity_fromString() const
40+
{
41+
QCOMPARE(GuiSeverity::fromString("error"), Severity::error);
42+
QCOMPARE(GuiSeverity::fromString("warning"), Severity::warning);
43+
QCOMPARE(GuiSeverity::fromString("style"), Severity::style);
44+
QCOMPARE(GuiSeverity::fromString("performance"), Severity::performance);
45+
QCOMPARE(GuiSeverity::fromString("portability"), Severity::portability);
46+
QCOMPARE(GuiSeverity::fromString("information"), Severity::information);
47+
QCOMPARE(GuiSeverity::fromString("none"), Severity::none);
48+
QCOMPARE(GuiSeverity::fromString("unknown"), Severity::none);
49+
}
50+
51+
void TestErrorItem::errorItem_tool() const
52+
{
53+
ErrorItem item;
54+
item.errorId = "nullPointer";
55+
QCOMPARE(item.tool(), QString("cppcheck"));
56+
57+
item.errorId = "clang-analyzer";
58+
QCOMPARE(item.tool(), QString("clang-analyzer"));
59+
60+
item.errorId = "clang-tidy-readability-braces";
61+
QCOMPARE(item.tool(), QString("clang-tidy"));
62+
63+
item.errorId = "clang-diagnostic-unused";
64+
QCOMPARE(item.tool(), QString("clang"));
65+
}
66+
67+
void TestErrorItem::errorItem_toString() const
68+
{
69+
ErrorItem item;
70+
item.errorId = "nullPointer";
71+
item.severity = Severity::error;
72+
item.summary = "Null pointer dereference";
73+
74+
QErrorPathItem path;
75+
path.file = "test.cpp";
76+
path.line = 42;
77+
path.column = 5;
78+
item.errorPath << path;
79+
80+
const QString result = item.toString();
81+
QVERIFY(result.startsWith("test.cpp:42:5:"));
82+
QVERIFY(result.contains("[nullPointer]"));
83+
QVERIFY(result.contains("Null pointer dereference"));
84+
QVERIFY(result.contains("error"));
85+
}
86+
87+
void TestErrorItem::errorItem_same_byHash() const
88+
{
89+
ErrorItem item1;
90+
item1.hash = 12345;
91+
ErrorItem item2;
92+
item2.hash = 12345;
93+
QVERIFY(ErrorItem::same(item1, item2));
94+
95+
item2.hash = 99999;
96+
QVERIFY(!ErrorItem::same(item1, item2));
97+
}
98+
99+
void TestErrorItem::errorItem_same_byFields() const
100+
{
101+
ErrorItem item1;
102+
item1.errorId = "nullPointer";
103+
item1.severity = Severity::error;
104+
item1.message = "The pointer is null";
105+
item1.inconclusive = false;
106+
QErrorPathItem p;
107+
p.file = "test.cpp";
108+
p.line = 10;
109+
p.column = 3;
110+
item1.errorPath << p;
111+
112+
ErrorItem item2 = item1;
113+
QVERIFY(ErrorItem::same(item1, item2));
114+
}
115+
116+
void TestErrorItem::errorItem_same_different() const
117+
{
118+
ErrorItem item1;
119+
item1.errorId = "nullPointer";
120+
item1.severity = Severity::error;
121+
122+
ErrorItem item2;
123+
item2.errorId = "uninitVar";
124+
item2.severity = Severity::error;
125+
126+
QVERIFY(!ErrorItem::same(item1, item2));
127+
}
128+
129+
void TestErrorItem::errorItem_filterMatch() const
130+
{
131+
ErrorItem item;
132+
item.errorId = "nullPointer";
133+
item.summary = "Null pointer dereference";
134+
item.message = "The pointer is null at this location";
135+
QErrorPathItem p;
136+
p.file = "main.cpp";
137+
p.info = "check here";
138+
item.errorPath << p;
139+
140+
// empty filter matches everything
141+
QVERIFY(item.filterMatch(""));
142+
143+
// matches in errorId (case insensitive)
144+
QVERIFY(item.filterMatch("nullPointer"));
145+
QVERIFY(item.filterMatch("NULLpointer"));
146+
147+
// matches in summary
148+
QVERIFY(item.filterMatch("Null pointer"));
149+
QVERIFY(item.filterMatch("DEREFERENCE"));
150+
151+
// matches in message
152+
QVERIFY(item.filterMatch("location"));
153+
154+
// matches in errorPath file
155+
QVERIFY(item.filterMatch("main.cpp"));
156+
157+
// matches in errorPath info
158+
QVERIFY(item.filterMatch("check here"));
159+
160+
// no match
161+
QVERIFY(!item.filterMatch("xyz123abc"));
162+
}

0 commit comments

Comments
 (0)