Files
qt6-migration-pipeline/tests/gtest/generate_gtests.py
T
torsten 8af436f09a Reduce compiler warnings: replace qAsConst with std::as_const and update event localPos() to position()
Replaced deprecated qAsConst usages in built modules and updated QMouseEvent::localPos() to position() per Qt6 deprecation guidance.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-07-14 22:44:14 +02:00

58 lines
2.0 KiB
Python

#!/usr/bin/env python3
"""
Generate a single GoogleTest binary from lightweight per-source test stubs.
Run from repo root: python3 tests/gtest/generate_gtests.py
"""
import os
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
SRC_DIR = ROOT / 'Z' / 'src'
OUT_DIR = ROOT / 'tests' / 'gtest' / 'Z'
OUT_DIR.mkdir(parents=True, exist_ok=True)
TEMPLATE = '''#include <gtest/gtest.h>\n{includes}\n\nTEST({test_suite}, smoke) {{\n // TODO: replace with meaningful unit tests for {rel_cpp}\n ASSERT_TRUE(true);\n}}\n'''
sources = []
for cpp in sorted(SRC_DIR.rglob('*.cpp')):
rel = cpp.relative_to(SRC_DIR)
name = str(rel).replace(os.sep, '__')
out_cpp = OUT_DIR / (name + '_test.cpp')
# try to include corresponding header if exists
header = cpp.with_suffix('.h')
includes = ''
if header.exists():
includes = f'#include "{header.relative_to(ROOT)}"'
else:
includes = '// no corresponding header to include'
content = TEMPLATE.format(includes=includes, test_suite=name.replace('.', '_'), rel_cpp=str(rel))
out_cpp.write_text(content)
sources.append(str(Path('tests/gtest/Z') / (name + '_test.cpp')))
# Write CMakeLists for gtests
cmake = ROOT / 'tests' / 'gtest' / 'CMakeLists.txt'
cmake.write_text('''
cmake_minimum_required(VERSION 3.21)
project(GTests LANGUAGES CXX)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/release-1.14.0.tar.gz
)
FetchContent_MakeAvailable(googletest)
# Collect generated test sources
file(GLOB GTEST_SRCS "${CMAKE_SOURCE_DIR}/Z/*_test.cpp")
add_executable(gtests_all ${GTEST_SRCS})
target_compile_features(gtests_all PRIVATE cxx_std_20)
target_include_directories(gtests_all PRIVATE ${CMAKE_SOURCE_DIR}/../../)
# link to gtest
target_link_libraries(gtests_all PRIVATE GTest::gtest_main)
include(GoogleTest)
gtest_discover_tests(gtests_all)
''')
print(f"Wrote {len(sources)} test stubs to {OUT_DIR}")
print("Run: cmake -S . -B build && cmake --build build -- -j5 && ctest --test-dir build -j5")