From d31f6496c975497823daa2214d4906703e9a5789 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 7 Aug 2024 21:39:21 -0700 Subject: [PATCH] moc: Add a test for QtMocHelpers Change-Id: I8a96935cf6c742259c9dfffd17e9a702717a18bf Reviewed-by: Ahmad Samir --- tests/auto/tools/CMakeLists.txt | 2 + tests/auto/tools/mochelpers/CMakeLists.txt | 17 +++++++ .../auto/tools/mochelpers/tst_mochelpers.cpp | 50 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 tests/auto/tools/mochelpers/CMakeLists.txt create mode 100644 tests/auto/tools/mochelpers/tst_mochelpers.cpp diff --git a/tests/auto/tools/CMakeLists.txt b/tests/auto/tools/CMakeLists.txt index ffeb228cf34..3fa004f4fc5 100644 --- a/tests/auto/tools/CMakeLists.txt +++ b/tests/auto/tools/CMakeLists.txt @@ -1,6 +1,8 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause +add_subdirectory(mochelpers) + # QTBUG-88538 if(NOT ANDROID AND NOT IOS) add_subdirectory(qmakelib) diff --git a/tests/auto/tools/mochelpers/CMakeLists.txt b/tests/auto/tools/mochelpers/CMakeLists.txt new file mode 100644 index 00000000000..2de51e4bdcc --- /dev/null +++ b/tests/auto/tools/mochelpers/CMakeLists.txt @@ -0,0 +1,17 @@ +# Copyright (C) 2024 Intel Corporation. +# SPDX-License-Identifier: BSD-3-Clause + +##################################################################### +## tst_qmetaenum Test: +##################################################################### + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_mochelpers LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + +qt_internal_add_test(tst_mochelpers + SOURCES + tst_mochelpers.cpp +) diff --git a/tests/auto/tools/mochelpers/tst_mochelpers.cpp b/tests/auto/tools/mochelpers/tst_mochelpers.cpp new file mode 100644 index 00000000000..6ceade78c14 --- /dev/null +++ b/tests/auto/tools/mochelpers/tst_mochelpers.cpp @@ -0,0 +1,50 @@ +// Copyright (C) 2024 Intel Corporation. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +// Testing qtmochelpers.h is probably pointless... if there's a problem with it +// then you most likely can't compile this test in the first place. +#include + +#include + +#include + +#include + +class tst_MocHelpers : public QObject +{ + Q_OBJECT +private slots: + void stringData(); +}; + +template +void verifyStringData(const QtMocHelpers::StringData &data, + std::initializer_list strings) +{ + QCOMPARE(std::size(strings), size_t(Count) / 2); + ptrdiff_t i = 0; + for (const char *str : strings) { + uint offset = data.offsetsAndSizes[i++] - sizeof(data.offsetsAndSizes); + uint len = data.offsetsAndSizes[i++]; + QByteArrayView result(data.stringdata0 + offset, len); + + QCOMPARE(len, strlen(str)); + QCOMPARE(result, str); + } +} + +void tst_MocHelpers::stringData() +{ +#define CHECK(...) \ + verifyStringData(QtMocHelpers::stringData(__VA_ARGS__), { __VA_ARGS__ }) + + QTest::setThrowOnFail(true); + CHECK("Hello"); + CHECK("Hello", "World"); + CHECK("Hello", "", "World"); +#undef CHECK +} + +QTEST_MAIN(tst_MocHelpers) +#include "tst_mochelpers.moc"