Skip to content

Commit e38b8cb

Browse files
authored
Merge pull request #10 from Acik-Kaynak-Gelistirme-Toplulugu/fix/tests-and-format
test: add unit tests and .clang-format config
2 parents dc95451 + 588627c commit e38b8cb

5 files changed

Lines changed: 246 additions & 2 deletions

File tree

.clang-format

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
# ro-Control clang-format configuration
3+
# CI bu dosyayı kullanır — değişiklik yapılırsa tüm kaynak dosyalar yeniden formatlanmalı.
4+
# Kullanım: find src/ -name "*.cpp" -o -name "*.h" | xargs clang-format -i
5+
6+
BasedOnStyle: LLVM
7+
Language: Cpp
8+
ColumnLimit: 80
9+
IndentWidth: 2
10+
TabWidth: 2
11+
UseTab: Never
12+
BreakBeforeBraces: Attach
13+
AllowShortFunctionsOnASingleLine: All
14+
AllowShortIfStatementsOnASingleLine: false
15+
AllowShortLoopsOnASingleLine: false
16+
SortIncludes: true
17+
IncludeBlocks: Preserve
18+
...

.github/workflows/ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,22 @@ jobs:
2929
qt6-qtwayland-devel \
3030
kf6-qqc2-desktop-style \
3131
polkit-devel \
32-
clang-tools-extra
32+
clang-tools-extra \
33+
qt6-qtbase-private-devel
3334
3435
- name: Configure (CMake)
3536
run: |
3637
cmake -B build \
3738
-GNinja \
3839
-DCMAKE_BUILD_TYPE=Release \
39-
-DBUILD_TESTS=OFF
40+
-DBUILD_TESTS=ON
4041
4142
- name: Build
4243
run: cmake --build build --parallel
4344

45+
- name: Run tests
46+
run: cd build && ctest --output-on-failure
47+
4448
- name: Check formatting (clang-format)
4549
run: |
4650
find src/ -name "*.cpp" -o -name "*.h" | \

tests/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
find_package(Qt6 REQUIRED COMPONENTS Test)
2+
3+
# ─── Detector Tests ──────────────────────────────────────────────────────────
4+
qt_add_executable(test_detector
5+
test_detector.cpp
6+
${CMAKE_SOURCE_DIR}/src/backend/nvidia/detector.cpp
7+
${CMAKE_SOURCE_DIR}/src/backend/system/commandrunner.cpp
8+
)
9+
10+
target_include_directories(test_detector PRIVATE
11+
${CMAKE_SOURCE_DIR}/src
12+
${CMAKE_SOURCE_DIR}/src/backend
13+
)
14+
15+
target_link_libraries(test_detector PRIVATE
16+
Qt6::Core
17+
Qt6::Test
18+
)
19+
20+
add_test(NAME test_detector COMMAND test_detector)
21+
22+
# ─── Monitor Tests ───────────────────────────────────────────────────────────
23+
qt_add_executable(test_monitor
24+
test_monitor.cpp
25+
${CMAKE_SOURCE_DIR}/src/backend/monitor/cpumonitor.cpp
26+
${CMAKE_SOURCE_DIR}/src/backend/monitor/gpumonitor.cpp
27+
${CMAKE_SOURCE_DIR}/src/backend/monitor/rammonitor.cpp
28+
${CMAKE_SOURCE_DIR}/src/backend/system/commandrunner.cpp
29+
)
30+
31+
target_include_directories(test_monitor PRIVATE
32+
${CMAKE_SOURCE_DIR}/src
33+
${CMAKE_SOURCE_DIR}/src/backend
34+
)
35+
36+
target_link_libraries(test_monitor PRIVATE
37+
Qt6::Core
38+
Qt6::Test
39+
)
40+
41+
add_test(NAME test_monitor COMMAND test_monitor)

tests/test_detector.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <QTest>
2+
3+
#include "nvidia/detector.h"
4+
5+
class TestDetector : public QObject {
6+
Q_OBJECT
7+
8+
private slots:
9+
// Sınıf oluşturulabiliyor mu?
10+
void testConstruction() {
11+
NvidiaDetector detector;
12+
// Crash olmadan oluşturulabiliyorsa geçer
13+
QVERIFY(true);
14+
}
15+
16+
// GpuInfo struct varsayılan değerleri
17+
void testGpuInfoDefaults() {
18+
NvidiaDetector::GpuInfo info{};
19+
QCOMPARE(info.found, false);
20+
QVERIFY(info.name.isEmpty());
21+
QVERIFY(info.driverVersion.isEmpty());
22+
QCOMPARE(info.driverLoaded, false);
23+
QCOMPARE(info.nouveauActive, false);
24+
}
25+
26+
// detect() çağrılabiliyor mu? (donanım olmadan bile çökmemeli)
27+
void testDetectDoesNotCrash() {
28+
NvidiaDetector detector;
29+
auto info = detector.detect();
30+
// NVIDIA yoksa found=false olmalı, crash olmamalı
31+
Q_UNUSED(info);
32+
QVERIFY(true);
33+
}
34+
35+
// hasNvidiaGpu() — dönüş tipi bool
36+
void testHasNvidiaGpu() {
37+
NvidiaDetector detector;
38+
bool result = detector.hasNvidiaGpu();
39+
// Sonuç true veya false olabilir — önemli olan crash olmaması
40+
Q_UNUSED(result);
41+
QVERIFY(true);
42+
}
43+
44+
// isDriverInstalled() — dönüş tipi bool
45+
void testIsDriverInstalled() {
46+
NvidiaDetector detector;
47+
bool result = detector.isDriverInstalled();
48+
Q_UNUSED(result);
49+
QVERIFY(true);
50+
}
51+
52+
// installedDriverVersion() — boş string veya versiyon döner
53+
void testInstalledDriverVersion() {
54+
NvidiaDetector detector;
55+
QString version = detector.installedDriverVersion();
56+
// Versiyon varsa "xxx.xx.xx" gibi olmalı, yoksa boş string
57+
if (!version.isEmpty()) {
58+
QVERIFY(version.contains(QChar('.')));
59+
}
60+
}
61+
62+
// detect() tutarlılık: found=false ise driverVersion boş olmalı
63+
void testDetectConsistency() {
64+
NvidiaDetector detector;
65+
auto info = detector.detect();
66+
if (!info.found) {
67+
QVERIFY(info.name.isEmpty());
68+
}
69+
}
70+
};
71+
72+
QTEST_MAIN(TestDetector)
73+
#include "test_detector.moc"

tests/test_monitor.cpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include <QSignalSpy>
2+
#include <QTest>
3+
4+
#include "monitor/cpumonitor.h"
5+
#include "monitor/gpumonitor.h"
6+
#include "monitor/rammonitor.h"
7+
8+
class TestMonitor : public QObject {
9+
Q_OBJECT
10+
11+
private slots:
12+
// ── CpuMonitor ──────────────────────────────────────────────────────────
13+
14+
void testCpuConstruction() {
15+
CpuMonitor cpu;
16+
QCOMPARE(cpu.load(), 0);
17+
QCOMPARE(cpu.temperature(), 0);
18+
}
19+
20+
void testCpuStartStop() {
21+
CpuMonitor cpu;
22+
cpu.start(500);
23+
// Timer çalışıyor olmalı
24+
QTest::qWait(100);
25+
cpu.stop();
26+
QVERIFY(true); // crash olmadı
27+
}
28+
29+
void testCpuSignals() {
30+
CpuMonitor cpu;
31+
QSignalSpy loadSpy(&cpu, &CpuMonitor::loadChanged);
32+
QSignalSpy tempSpy(&cpu, &CpuMonitor::temperatureChanged);
33+
QVERIFY(loadSpy.isValid());
34+
QVERIFY(tempSpy.isValid());
35+
}
36+
37+
void testCpuLoadRange() {
38+
CpuMonitor cpu;
39+
cpu.start(100);
40+
QTest::qWait(300); // birkaç poll döngüsü bekle
41+
cpu.stop();
42+
// Yük 0-100 arasında olmalı
43+
QVERIFY(cpu.load() >= 0 && cpu.load() <= 100);
44+
}
45+
46+
// ── GpuMonitor ──────────────────────────────────────────────────────────
47+
48+
void testGpuConstruction() {
49+
GpuMonitor gpu;
50+
QCOMPARE(gpu.temperature(), 0);
51+
QCOMPARE(gpu.load(), 0);
52+
QCOMPARE(gpu.vramUsed(), 0);
53+
QCOMPARE(gpu.vramTotal(), 0);
54+
QCOMPARE(gpu.available(), false);
55+
}
56+
57+
void testGpuStartStop() {
58+
GpuMonitor gpu;
59+
gpu.start(500);
60+
QTest::qWait(100);
61+
gpu.stop();
62+
QVERIFY(true);
63+
}
64+
65+
void testGpuSignals() {
66+
GpuMonitor gpu;
67+
QSignalSpy tempSpy(&gpu, &GpuMonitor::temperatureChanged);
68+
QSignalSpy loadSpy(&gpu, &GpuMonitor::loadChanged);
69+
QSignalSpy vramUsedSpy(&gpu, &GpuMonitor::vramUsedChanged);
70+
QSignalSpy vramTotalSpy(&gpu, &GpuMonitor::vramTotalChanged);
71+
QSignalSpy availSpy(&gpu, &GpuMonitor::availableChanged);
72+
QVERIFY(tempSpy.isValid());
73+
QVERIFY(loadSpy.isValid());
74+
QVERIFY(vramUsedSpy.isValid());
75+
QVERIFY(vramTotalSpy.isValid());
76+
QVERIFY(availSpy.isValid());
77+
}
78+
79+
// ── RamMonitor ──────────────────────────────────────────────────────────
80+
81+
void testRamConstruction() {
82+
RamMonitor ram;
83+
QCOMPARE(ram.totalMb(), 0);
84+
QCOMPARE(ram.usedMb(), 0);
85+
QCOMPARE(ram.usedPercent(), 0);
86+
}
87+
88+
void testRamStartStop() {
89+
RamMonitor ram;
90+
ram.start(500);
91+
QTest::qWait(100);
92+
ram.stop();
93+
QVERIFY(true);
94+
}
95+
96+
void testRamSignals() {
97+
RamMonitor ram;
98+
QSignalSpy totalSpy(&ram, &RamMonitor::totalMbChanged);
99+
QSignalSpy usedSpy(&ram, &RamMonitor::usedMbChanged);
100+
QSignalSpy pctSpy(&ram, &RamMonitor::usedPercentChanged);
101+
QVERIFY(totalSpy.isValid());
102+
QVERIFY(usedSpy.isValid());
103+
QVERIFY(pctSpy.isValid());
104+
}
105+
};
106+
107+
QTEST_MAIN(TestMonitor)
108+
#include "test_monitor.moc"

0 commit comments

Comments
 (0)