diff options
Diffstat (limited to 'sources/shiboken2/tests/test_generator')
11 files changed, 470 insertions, 0 deletions
diff --git a/sources/shiboken2/tests/test_generator/CMakeLists.txt b/sources/shiboken2/tests/test_generator/CMakeLists.txt new file mode 100644 index 000000000..5f5099897 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/CMakeLists.txt @@ -0,0 +1,68 @@ +project(test_generator) + +set(dummy_generator_SRC dummygenerator.cpp) +add_library(dummy_generator SHARED ${dummy_generator_SRC}) +target_link_libraries(dummy_generator ${APIEXTRACTOR_LIBRARY} ${QT_QTCORE_LIBRARY} genrunner) +set_property(TARGET dummy_generator PROPERTY PREFIX "") + +add_executable(dummygenerator main.cpp) +set(DUMMYGENERATOR_EXECUTABLE dummygenerator${generator_SUFFIX}) +set_target_properties(dummygenerator PROPERTIES OUTPUT_NAME ${DUMMYGENERATOR_EXECUTABLE}) +target_link_libraries(dummygenerator ${Qt5Core_LIBRARIES}) + +configure_file(dummygentestconfig.h.in "${CMAKE_CURRENT_BINARY_DIR}/dummygentestconfig.h" @ONLY) + +get_filename_component(APIEXTRACTOR_LIBRARY_DIRS ${APIEXTRACTOR_LIBRARY} PATH) +if(WIN32) + set(PATH_SEP ";") + find_program(APIEXTRACTOR_BINARY apiextractor.dll HINTS ${APIEXTRACTOR_LIBRARY_DIRS}) + get_filename_component(APIEXTRACTOR_BINARY_DIR ${APIEXTRACTOR_BINARY} PATH) + set(APIEXTRACTOR_LIBRARY_DIRS "${APIEXTRACTOR_LIBRARY_DIRS}${PATH_SEP}${APIEXTRACTOR_BINARY_DIR}") +else() + set(PATH_SEP ":") +endif() + +set(ENV_PATH "${generatorrunner_BINARY_DIR}${PATH_SEP}${CMAKE_CURRENT_BINARY_DIR}${PATH_SEP}$ENV{PATH}${PATH_SEP}${APIEXTRACTOR_LIBRARY_DIRS}") +set(ENV_QT_PLUGIN_PATH "${CMAKE_CURRENT_BINARY_DIR}${PATH_SEP}$ENV{QT_PLUGIN_PATH}") +if(WIN32) + string(REPLACE "\\;" ";" ENV_PATH "${ENV_PATH}") + string(REPLACE ";" "\\;" ENV_PATH "${ENV_PATH}") + string(REPLACE "\\;" ";" ENV_QT_PLUGIN_PATH "${ENV_QT_PLUGIN_PATH}") + string(REPLACE ";" "\\;" ENV_QT_PLUGIN_PATH "${ENV_QT_PLUGIN_PATH}") +endif() + +macro(m_add_test testname) + if(CMAKE_VERSION VERSION_LESS 2.8) + add_test(${testname} ${CMAKE_COMMAND} -DTEST=${testname} + -DWORKDIR=${CMAKE_CURRENT_BINARY_DIR} + -DENV_PATH=${ENV_PATH} -DENV_QT_PLUGIN_PATH=${ENV_QT_PLUGIN_PATH} + -P ${CMAKE_CURRENT_SOURCE_DIR}/run_test.cmake) + else() + add_test(${testname} ${testname}) + set_property(TEST ${testname} PROPERTY ENVIRONMENT "PATH=${ENV_PATH}" "QT_PLUGIN_PATH=${ENV_QT_PLUGIN_PATH}") + endif() +endmacro() + +macro(declare_test testname) + qt4_automoc("${testname}.cpp") + add_executable(${testname} "${testname}.cpp") + include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(${testname} + ${QT_QTTEST_LIBRARY} + ${QT_QTCORE_LIBRARY} + ${Qt5Test_LIBRARIES} + ${Qt5Core_LIBRARIES} + ) + m_add_test(${testname}) +endmacro(declare_test testname) + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/test_global.h" + "${CMAKE_CURRENT_BINARY_DIR}/test_global.h" COPYONLY) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/test_typesystem.xml" + "${CMAKE_CURRENT_BINARY_DIR}/test_typesystem.xml" COPYONLY) +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/dummygentest-project.txt.in" + "${CMAKE_CURRENT_BINARY_DIR}/dummygentest-project.txt" @ONLY) +declare_test(dummygentest) + +add_dependencies(dummygenerator generatorrunner) + diff --git a/sources/shiboken2/tests/test_generator/dummygenerator.cpp b/sources/shiboken2/tests/test_generator/dummygenerator.cpp new file mode 100644 index 000000000..40d9fb771 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygenerator.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <iostream> +#include "dummygenerator.h" + +EXPORT_GENERATOR_PLUGIN(new DummyGenerator) + +using namespace std; + +QString +DummyGenerator::fileNameForClass(const AbstractMetaClass* metaClass) const +{ + return QString("%1_generated.txt").arg(metaClass->name().toLower()); +} + +void +DummyGenerator::generateClass(QTextStream& s, const AbstractMetaClass* metaClass) +{ + s << "// Generated code for class: " << qPrintable(metaClass->name()) << endl; +} + +bool +DummyGenerator::doSetup(const QMap<QString, QString>& args) +{ + if (args.contains("dump-arguments") && !args["dump-arguments"].isEmpty()) { + QFile logFile(args["dump-arguments"]); + logFile.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream out(&logFile); + for (QMap<QString, QString>::const_iterator it = args.cbegin(), end = args.cend(); it != end; ++it) { + const QString& key = it.key(); + if (key == "arg-1") + out << "header-file"; + else if (key == "arg-2") + out << "typesystem-file"; + else + out << key; + if (!args[key].isEmpty()) + out << " = " << args[key]; + out << endl; + } + } + return true; +} + diff --git a/sources/shiboken2/tests/test_generator/dummygenerator.h b/sources/shiboken2/tests/test_generator/dummygenerator.h new file mode 100644 index 000000000..5ab95a925 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygenerator.h @@ -0,0 +1,49 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#ifndef DUMMYGENERATOR_H +#define DUMMYGENERATOR_H + +#include "generator.h" + +class GENRUNNER_API DummyGenerator : public Generator +{ +public: + DummyGenerator() {} + ~DummyGenerator() {} + bool doSetup(const QMap<QString, QString>& args); + const char* name() const { return "DummyGenerator"; } + +protected: + void writeFunctionArguments(QTextStream&, const AbstractMetaFunction*, Options) const {} + void writeArgumentNames(QTextStream&, const AbstractMetaFunction*, Options) const {} + QString fileNameForClass(const AbstractMetaClass* metaClass) const; + void generateClass(QTextStream& s, const AbstractMetaClass* metaClass); + void finishGeneration() {} +}; + +#endif // DUMMYGENERATOR_H diff --git a/sources/shiboken2/tests/test_generator/dummygentest-project.txt.in b/sources/shiboken2/tests/test_generator/dummygentest-project.txt.in new file mode 100644 index 000000000..0a076d8bd --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentest-project.txt.in @@ -0,0 +1,20 @@ +[generator-project] + +generator-set = dummy +header-file = @CMAKE_CURRENT_BINARY_DIR@/test_global.h +typesystem-file = @CMAKE_CURRENT_BINARY_DIR@/test_typesystem.xml +output-directory = /tmp/output + +dump-arguments = @CMAKE_CURRENT_BINARY_DIR@/dummygen-args.log + +include-path = /include/path/location1 +include-path = /include/path/location2 + +typesystem-path = /typesystem/path/location1 +typesystem-path = /typesystem/path/location2 + +api-version = 1.2.3 +debug = sparse + +no-suppress-warnings + diff --git a/sources/shiboken2/tests/test_generator/dummygentest.cpp b/sources/shiboken2/tests/test_generator/dummygentest.cpp new file mode 100644 index 000000000..ae028e2c8 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentest.cpp @@ -0,0 +1,138 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "dummygentest.h" +#include "dummygenerator.h" +#include "dummygentestconfig.h" +#include <QTemporaryFile> +#include <QtTest/QTest> +#include <QProcess> + +#define GENERATED_CONTENTS "// Generated code for class: Dummy" + +void DummyGenTest::initTestCase() +{ + int argc = 0; + char* argv[] = {NULL}; + QCoreApplication app(argc, argv); + workDir = QDir::currentPath(); + + headerFilePath = workDir + "/test_global.h"; + typesystemFilePath = workDir + "/test_typesystem.xml"; + projectFilePath = workDir + "/dummygentest-project.txt"; + generatedFilePath = QString("%1/dummy/dummy_generated.txt").arg(QDir::tempPath()); +} + +void DummyGenTest::testCallGenRunnerWithFullPathToDummyGenModule() +{ + QStringList args; + args.append("--generator-set=" DUMMYGENERATOR_BINARY_DIR "/dummy_generator" MODULE_EXTENSION); + args.append(QString("--output-directory=%1").arg(QDir::tempPath())); + args.append(headerFilePath); + args.append(typesystemFilePath); + int result = QProcess::execute("generatorrunner", args); + QCOMPARE(result, 0); + + QFile generatedFile(generatedFilePath); + generatedFile.open(QIODevice::ReadOnly); + QCOMPARE(generatedFile.readAll().trimmed(), QByteArray(GENERATED_CONTENTS).trimmed()); + generatedFile.close(); + + QVERIFY(generatedFile.remove()); +} + +void DummyGenTest::testCallGenRunnerWithNameOfDummyGenModule() +{ + QStringList args; + args.append("--generator-set=dummy"); + args.append(QString("--output-directory=%1").arg(QDir::tempPath())); + args.append(headerFilePath); + args.append(typesystemFilePath); + int result = QProcess::execute("generatorrunner", args); + QCOMPARE(result, 0); + + QFile generatedFile(generatedFilePath); + generatedFile.open(QIODevice::ReadOnly); + QCOMPARE(generatedFile.readAll().trimmed(), QByteArray(GENERATED_CONTENTS).trimmed()); + generatedFile.close(); + + QVERIFY(generatedFile.remove()); +} + +void DummyGenTest::testCallDummyGeneratorExecutable() +{ + QStringList args; + args.append(QString("--output-directory=%1").arg(QDir::tempPath())); + args.append(headerFilePath); + args.append(typesystemFilePath); + int result = QProcess::execute(DUMMYGENERATOR_BINARY, args); + QCOMPARE(result, 0); + + QFile generatedFile(generatedFilePath); + generatedFile.open(QIODevice::ReadOnly); + QCOMPARE(generatedFile.readAll().trimmed(), QByteArray(GENERATED_CONTENTS).trimmed()); + generatedFile.close(); + + QVERIFY(generatedFile.remove()); +} + +void DummyGenTest::testProjectFileArgumentsReading() +{ + QStringList args(QString("--project-file=%1/dummygentest-project.txt").arg(workDir)); + int result = QProcess::execute("generatorrunner", args); + QCOMPARE(result, 0); + + QFile logFile(workDir + "/dummygen-args.log"); + logFile.open(QIODevice::ReadOnly); + QStringList logContents; + while (!logFile.atEnd()) + logContents << logFile.readLine().trimmed(); + logContents.sort(); + QCOMPARE(logContents[0], QString("api-version = 1.2.3")); + QCOMPARE(logContents[1], QString("debug = sparse")); + QVERIFY(logContents[2].startsWith("dump-arguments = ")); + QVERIFY(logContents[2].endsWith("dummygen-args.log")); + QCOMPARE(logContents[3], QString("generator-set = dummy")); + QVERIFY(logContents[4].startsWith("header-file = ")); + QVERIFY(logContents[4].endsWith("test_global.h")); + QCOMPARE(logContents[5], + QDir::toNativeSeparators(QString("include-paths = /include/path/location1%1/include/path/location2").arg(PATH_SPLITTER))); + QCOMPARE(logContents[6], QString("no-suppress-warnings")); + QCOMPARE(logContents[7], QString("output-directory = /tmp/output")); + QVERIFY(logContents[8].startsWith("project-file = ")); + QVERIFY(logContents[8].endsWith("dummygentest-project.txt")); + QVERIFY(logContents[9].startsWith("typesystem-file = ")); + QVERIFY(logContents[9].endsWith("test_typesystem.xml")); + QCOMPARE(logContents[10], + QDir::toNativeSeparators(QString("typesystem-paths = /typesystem/path/location1%1/typesystem/path/location2").arg(PATH_SPLITTER))); +} + +QTEST_APPLESS_MAIN(DummyGenTest) + +#include "dummygentest.moc" + diff --git a/sources/shiboken2/tests/test_generator/dummygentest.h b/sources/shiboken2/tests/test_generator/dummygentest.h new file mode 100644 index 000000000..be8ca42af --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentest.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef DUMMYGENTABLETEST_H +#define DUMMYGENTABLETEST_H + +#include <QObject> + +class DummyGenerator; + +class DummyGenTest : public QObject +{ + Q_OBJECT + +private: + QString workDir; + QString headerFilePath; + QString typesystemFilePath; + QString generatedFilePath; + QString projectFilePath; + +private slots: + void initTestCase(); + void testCallGenRunnerWithFullPathToDummyGenModule(); + void testCallGenRunnerWithNameOfDummyGenModule(); + void testCallDummyGeneratorExecutable(); + void testProjectFileArgumentsReading(); +}; + +#endif + diff --git a/sources/shiboken2/tests/test_generator/dummygentestconfig.h.in b/sources/shiboken2/tests/test_generator/dummygentestconfig.h.in new file mode 100644 index 000000000..9da17dcd3 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/dummygentestconfig.h.in @@ -0,0 +1,15 @@ +#ifndef DUMMYGENTESTCONFIG_H +#define DUMMYGENTESTCONFIG_H + +#define MODULE_EXTENSION "@CMAKE_SHARED_LIBRARY_SUFFIX@" +#define DUMMYGENERATOR_BINARY "@DUMMYGENERATOR_EXECUTABLE@" +#define DUMMYGENERATOR_BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@" + +#ifdef _WINDOWS + #define PATH_SPLITTER ";" +#else + #define PATH_SPLITTER ":" +#endif + +#endif // DUMMYGENTESTCONFIG_H + diff --git a/sources/shiboken2/tests/test_generator/main.cpp b/sources/shiboken2/tests/test_generator/main.cpp new file mode 100644 index 000000000..5c46ea006 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/main.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of PySide2. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore> + +int main(int argc, char *argv[]) +{ + QStringList args; + args.append("--generator-set=dummy"); + for (int i = 1; i < argc; i++) + args.append(argv[i]); + return QProcess::execute("generatorrunner", args); +} + diff --git a/sources/shiboken2/tests/test_generator/run_test.cmake b/sources/shiboken2/tests/test_generator/run_test.cmake new file mode 100644 index 000000000..34a821d80 --- /dev/null +++ b/sources/shiboken2/tests/test_generator/run_test.cmake @@ -0,0 +1,11 @@ +# The tests are run through this script due to a limitation +# on versions of CMake lesser than 2.8, that prevent setting +# environment variables for tests from working. + +set(ENV{PATH} "${ENV_PATH}") +set(ENV{QT_PLUGIN_PATH} "${ENV_QT_PLUGIN_PATH}") +execute_process(COMMAND ${TEST} WORKING_DIRECTORY "${WORKDIR}" RESULT_VARIABLE OK) + +if(NOT OK EQUAL 0) + message(SEND_ERROR "${TEST} failed!") +endif() diff --git a/sources/shiboken2/tests/test_generator/test_global.h b/sources/shiboken2/tests/test_generator/test_global.h new file mode 100644 index 000000000..6a95200cf --- /dev/null +++ b/sources/shiboken2/tests/test_generator/test_global.h @@ -0,0 +1 @@ +struct Dummy {}; diff --git a/sources/shiboken2/tests/test_generator/test_typesystem.xml b/sources/shiboken2/tests/test_generator/test_typesystem.xml new file mode 100644 index 000000000..c19a4e95e --- /dev/null +++ b/sources/shiboken2/tests/test_generator/test_typesystem.xml @@ -0,0 +1,3 @@ +<typesystem package='dummy'> + <value-type name='Dummy'/> +</typesystem> |