Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gui/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
add_custom_target(gui-tests)

add_subdirectory(codeeditorstyle)
add_subdirectory(cppchecklibrarydata)
add_subdirectory(erroritem)
add_subdirectory(filelist)
add_subdirectory(platforms)
add_subdirectory(projectfile)
add_subdirectory(resultstree)
add_subdirectory(showtypes)
add_subdirectory(translationhandler)
add_subdirectory(xmlreportv2)
23 changes: 23 additions & 0 deletions gui/test/codeeditorstyle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
qt_wrap_cpp(test-codeeditorstyle_SRC testcodeeditorstyle.h)
add_custom_target(build-testcodeeditorstyle-deps SOURCES ${test-codeeditorstyle_SRC})
add_dependencies(gui-build-deps build-testcodeeditorstyle-deps)
add_executable(test-codeeditorstyle
${test-codeeditorstyle_SRC}
testcodeeditorstyle.cpp
${CMAKE_SOURCE_DIR}/gui/codeeditorstyle.cpp
)
target_include_directories(test-codeeditorstyle PRIVATE ${CMAKE_SOURCE_DIR}/gui)
target_link_libraries(test-codeeditorstyle ${QT_CORE_LIB} ${QT_GUI_LIB} ${QT_TEST_LIB})

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(QT_VERSION VERSION_GREATER_EQUAL "6.9.0")
# caused by Qt generated moc code starting with 6.9.0 - see https://bugreports.qt.io/browse/QTBUG-135638
target_compile_options_safe(test-codeeditorstyle -Wno-ctad-maybe-unsupported)
endif()
endif()

if (REGISTER_GUI_TESTS)
add_test(NAME test-codeeditorstyle COMMAND $<TARGET_FILE:test-codeeditorstyle> -platform offscreen)
endif()

add_dependencies(gui-tests test-codeeditorstyle)
103 changes: 103 additions & 0 deletions gui/test/codeeditorstyle/testcodeeditorstyle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "testcodeeditorstyle.h"

#include "codeeditorstyle.h"

#include <QDir>
#include <QSettings>
#include <QtTest>

QTEST_MAIN(TestCodeEditorStyle)

void TestCodeEditorStyle::defaultStyles_notEqual() const
{
QVERIFY(defaultStyleLight != defaultStyleDark);
}

void TestCodeEditorStyle::equality_sameStyle() const
{
QVERIFY(defaultStyleLight == defaultStyleLight);
QVERIFY(defaultStyleDark == defaultStyleDark);
}

void TestCodeEditorStyle::equality_differentColor() const
{
CodeEditorStyle style(defaultStyleLight);
style.widgetFGColor = QColor(1, 2, 3);
QVERIFY(style != defaultStyleLight);
}

void TestCodeEditorStyle::getSystemTheme_isSystemTheme() const
{
const CodeEditorStyle sys = CodeEditorStyle::getSystemTheme();
QVERIFY(sys.isSystemTheme());
}

void TestCodeEditorStyle::getSystemTheme_colorsMatchLight() const
{
const CodeEditorStyle sys = CodeEditorStyle::getSystemTheme();
QCOMPARE(sys.widgetFGColor, defaultStyleLight.widgetFGColor);
QCOMPARE(sys.widgetBGColor, defaultStyleLight.widgetBGColor);
QCOMPARE(sys.keywordColor, defaultStyleLight.keywordColor);
QCOMPARE(sys.keywordWeight, defaultStyleLight.keywordWeight);
QCOMPARE(sys.commentColor, defaultStyleLight.commentColor);
}

void TestCodeEditorStyle::saveLoad_darkRoundtrip() const
{
const QString settingsFile = QDir::tempPath() + "/test_codeeditorstyle_dark.ini";
QFile::remove(settingsFile);

{
QSettings settings(settingsFile, QSettings::IniFormat);
CodeEditorStyle::saveSettings(&settings, defaultStyleDark);
}

QSettings settings(settingsFile, QSettings::IniFormat);
const CodeEditorStyle loaded = CodeEditorStyle::loadSettings(&settings);
QCOMPARE(loaded, defaultStyleDark);

QFile::remove(settingsFile);
}

void TestCodeEditorStyle::saveLoad_customColors() const
{
// Create a style that differs from both defaults so it is saved as "Custom"
CodeEditorStyle custom(defaultStyleLight);
custom.widgetFGColor = QColor(11, 22, 33);
custom.keywordColor = QColor(44, 55, 66);
custom.commentColor = QColor(77, 88, 99);

const QString settingsFile = QDir::tempPath() + "/test_codeeditorstyle_custom.ini";
QFile::remove(settingsFile);

{
QSettings settings(settingsFile, QSettings::IniFormat);
CodeEditorStyle::saveSettings(&settings, custom);
}

QSettings settings(settingsFile, QSettings::IniFormat);
const CodeEditorStyle loaded = CodeEditorStyle::loadSettings(&settings);
QCOMPARE(loaded.widgetFGColor, custom.widgetFGColor);
QCOMPARE(loaded.keywordColor, custom.keywordColor);
QCOMPARE(loaded.commentColor, custom.commentColor);

QFile::remove(settingsFile);
}
37 changes: 37 additions & 0 deletions gui/test/codeeditorstyle/testcodeeditorstyle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* -*- C++ -*-
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TESTCODEEDITORSTYLE_H
#define TESTCODEEDITORSTYLE_H

#include <QObject>

class TestCodeEditorStyle : public QObject {
Q_OBJECT

private slots:
void defaultStyles_notEqual() const;
void equality_sameStyle() const;
void equality_differentColor() const;
void getSystemTheme_isSystemTheme() const;
void getSystemTheme_colorsMatchLight() const;
void saveLoad_darkRoundtrip() const;
void saveLoad_customColors() const;
};

#endif // TESTCODEEDITORSTYLE_H
23 changes: 23 additions & 0 deletions gui/test/erroritem/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
qt_wrap_cpp(test-erroritem_SRC testerroritem.h)
add_custom_target(build-testerroritem-deps SOURCES ${test-erroritem_SRC})
add_dependencies(gui-build-deps build-testerroritem-deps)
add_executable(test-erroritem
${test-erroritem_SRC}
testerroritem.cpp
${CMAKE_SOURCE_DIR}/gui/erroritem.cpp
)
target_include_directories(test-erroritem PRIVATE ${CMAKE_SOURCE_DIR}/gui)
target_link_libraries(test-erroritem ${QT_CORE_LIB} ${QT_TEST_LIB} cppcheck-core)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if(QT_VERSION VERSION_GREATER_EQUAL "6.9.0")
# caused by Qt generated moc code starting with 6.9.0 - see https://bugreports.qt.io/browse/QTBUG-135638
target_compile_options_safe(test-erroritem -Wno-ctad-maybe-unsupported)
endif()
endif()

if (REGISTER_GUI_TESTS)
add_test(NAME test-erroritem COMMAND $<TARGET_FILE:test-erroritem>)
endif()

add_dependencies(gui-tests test-erroritem)
162 changes: 162 additions & 0 deletions gui/test/erroritem/testerroritem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2026 Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "testerroritem.h"

#include "erroritem.h"
#include "errortypes.h"

#include <QtTest>

QTEST_MAIN(TestErrorItem)

void TestErrorItem::guiSeverity_toString() const
{
QCOMPARE(GuiSeverity::toString(Severity::error), QString("error"));
QCOMPARE(GuiSeverity::toString(Severity::warning), QString("warning"));
QCOMPARE(GuiSeverity::toString(Severity::style), QString("style"));
QCOMPARE(GuiSeverity::toString(Severity::performance), QString("performance"));
QCOMPARE(GuiSeverity::toString(Severity::portability), QString("portability"));
QCOMPARE(GuiSeverity::toString(Severity::information), QString("information"));
QCOMPARE(GuiSeverity::toString(Severity::none), QString(""));
}

void TestErrorItem::guiSeverity_fromString() const
{
QCOMPARE(GuiSeverity::fromString("error"), Severity::error);
QCOMPARE(GuiSeverity::fromString("warning"), Severity::warning);
QCOMPARE(GuiSeverity::fromString("style"), Severity::style);
QCOMPARE(GuiSeverity::fromString("performance"), Severity::performance);
QCOMPARE(GuiSeverity::fromString("portability"), Severity::portability);
QCOMPARE(GuiSeverity::fromString("information"), Severity::information);
QCOMPARE(GuiSeverity::fromString("none"), Severity::none);
QCOMPARE(GuiSeverity::fromString("unknown"), Severity::none);
}

void TestErrorItem::errorItem_tool() const
{
ErrorItem item;
item.errorId = "nullPointer";
QCOMPARE(item.tool(), QString("cppcheck"));

item.errorId = "clang-analyzer";
QCOMPARE(item.tool(), QString("clang-analyzer"));

item.errorId = "clang-tidy-readability-braces";
QCOMPARE(item.tool(), QString("clang-tidy"));

item.errorId = "clang-diagnostic-unused";
QCOMPARE(item.tool(), QString("clang"));
}

void TestErrorItem::errorItem_toString() const
{
ErrorItem item;
item.errorId = "nullPointer";
item.severity = Severity::error;
item.summary = "Null pointer dereference";

QErrorPathItem path;
path.file = "test.cpp";
path.line = 42;
path.column = 5;
item.errorPath << path;

const QString result = item.toString();
QVERIFY(result.startsWith("test.cpp:42:5:"));
QVERIFY(result.contains("[nullPointer]"));
QVERIFY(result.contains("Null pointer dereference"));
QVERIFY(result.contains("error"));
}

void TestErrorItem::errorItem_same_byHash() const
{
ErrorItem item1;
item1.hash = 12345;
ErrorItem item2;
item2.hash = 12345;
QVERIFY(ErrorItem::same(item1, item2));

item2.hash = 99999;
QVERIFY(!ErrorItem::same(item1, item2));
}

void TestErrorItem::errorItem_same_byFields() const
{
ErrorItem item1;
item1.errorId = "nullPointer";
item1.severity = Severity::error;
item1.message = "The pointer is null";
item1.inconclusive = false;
QErrorPathItem p;
p.file = "test.cpp";
p.line = 10;
p.column = 3;
item1.errorPath << p;

ErrorItem item2 = item1;
QVERIFY(ErrorItem::same(item1, item2));
}

void TestErrorItem::errorItem_same_different() const
{
ErrorItem item1;
item1.errorId = "nullPointer";
item1.severity = Severity::error;

ErrorItem item2;
item2.errorId = "uninitVar";
item2.severity = Severity::error;

QVERIFY(!ErrorItem::same(item1, item2));
}

void TestErrorItem::errorItem_filterMatch() const
{
ErrorItem item;
item.errorId = "nullPointer";
item.summary = "Null pointer dereference";
item.message = "The pointer is null at this location";
QErrorPathItem p;
p.file = "main.cpp";
p.info = "check here";
item.errorPath << p;

// empty filter matches everything
QVERIFY(item.filterMatch(""));

// matches in errorId (case insensitive)
QVERIFY(item.filterMatch("nullPointer"));
QVERIFY(item.filterMatch("NULLpointer"));

// matches in summary
QVERIFY(item.filterMatch("Null pointer"));
QVERIFY(item.filterMatch("DEREFERENCE"));

// matches in message
QVERIFY(item.filterMatch("location"));

// matches in errorPath file
QVERIFY(item.filterMatch("main.cpp"));

// matches in errorPath info
QVERIFY(item.filterMatch("check here"));

// no match
QVERIFY(!item.filterMatch("xyz123abc"));
}
Loading
Loading