From 3e80f52414e097bbf81f059f1e6f7ead538105d3 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar <nikolai.kosjar@digia.com> Date: Wed, 15 May 2013 11:06:36 +0200 Subject: [PATCH] C++: Tests: Generalize for using different test data dirs ...for the modelmanager tests. Change-Id: I91f163df1ed958a3915ce263b12e3e30cda0b78f Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com> --- src/plugins/cpptools/cppmodelmanager_test.cpp | 94 ++++++++++++------- 1 file changed, 62 insertions(+), 32 deletions(-) diff --git a/src/plugins/cpptools/cppmodelmanager_test.cpp b/src/plugins/cpptools/cppmodelmanager_test.cpp index 3385768c152..7069262dd18 100644 --- a/src/plugins/cpptools/cppmodelmanager_test.cpp +++ b/src/plugins/cpptools/cppmodelmanager_test.cpp @@ -32,8 +32,9 @@ #include "cpppreprocessor.h" #include "modelmanagertesthelper.h" -#include <QtTest> #include <QDebug> +#include <QFileInfo> +#include <QtTest> using namespace CppTools::Internal; @@ -44,31 +45,51 @@ typedef CppTools::ProjectFile ProjectFile; typedef ProjectExplorer::Project Project; namespace { -QString testDataDir(const QString& subdir, bool cleaned = true) -{ - QString path = QLatin1String(SRCDIR "/../../../tests/cppmodelmanager/testdata"); - if (!subdir.isEmpty()) - path += QLatin1String("/") + subdir; - if (cleaned) - return CppPreprocessor::cleanPath(path); - else - return path; -} -QString testIncludeDir(bool cleaned = true) +class TestDataDirectory { - return testDataDir(QLatin1String("include"), cleaned); -} +public: + TestDataDirectory(const QString &testDataDirectory) + : m_testDataDirectory(QLatin1String(SRCDIR "/../../../tests/cppmodelmanager/") + + testDataDirectory) + { + QFileInfo testDataDir(m_testDataDirectory); + QVERIFY(testDataDir.exists()); + QVERIFY(testDataDir.isDir()); + } -QString testFrameworksDir(bool cleaned = true) -{ - return testDataDir(QLatin1String("frameworks"), cleaned); -} -QString testSource(const QString &fileName) -{ - return testDataDir(QLatin1String("sources")) + fileName; -} + QString includeDir(bool cleaned = true) const + { + return testDataDir(QLatin1String("include"), cleaned); + } + + QString frameworksDir(bool cleaned = true) const + { + return testDataDir(QLatin1String("frameworks"), cleaned); + } + + QString fileFromSourcesDir(const QString &fileName) const + { + return testDataDir(QLatin1String("sources")) + fileName; + } + +private: + QString testDataDir(const QString& subdir, bool cleaned = true) const + { + QString path = m_testDataDirectory; + if (!subdir.isEmpty()) + path += QLatin1String("/") + subdir; + if (cleaned) + return CppPreprocessor::cleanPath(path); + else + return path; + } + +private: + const QString m_testDataDirectory; +}; + } // anonymous namespace void CppToolsPlugin::test_modelmanager_paths() @@ -76,6 +97,8 @@ void CppToolsPlugin::test_modelmanager_paths() ModelManagerTestHelper helper; CppModelManager *mm = CppModelManager::instance(); + const TestDataDirectory testDataDir(QLatin1String("testdata")); + Project *project = helper.createProject(QLatin1String("test_modelmanager_paths")); ProjectInfo pi = mm->projectInfo(project); QCOMPARE(pi.project().data(), project); @@ -85,18 +108,18 @@ void CppToolsPlugin::test_modelmanager_paths() part->cxxVersion = ProjectPart::CXX98; part->qtVersion = ProjectPart::Qt5; part->defines = QByteArray("#define OH_BEHAVE -1\n"); - part->includePaths = QStringList() << testIncludeDir(false); - part->frameworkPaths = QStringList() << testFrameworksDir(false); + part->includePaths = QStringList() << testDataDir.includeDir(false); + part->frameworkPaths = QStringList() << testDataDir.frameworksDir(false); mm->updateProjectInfo(pi); QStringList includePaths = mm->includePaths(); QCOMPARE(includePaths.size(), 1); - QVERIFY(includePaths.contains(testIncludeDir())); + QVERIFY(includePaths.contains(testDataDir.includeDir())); QStringList frameworkPaths = mm->frameworkPaths(); QCOMPARE(frameworkPaths.size(), 1); - QVERIFY(frameworkPaths.contains(testFrameworksDir())); + QVERIFY(frameworkPaths.contains(testDataDir.frameworksDir())); } void CppToolsPlugin::test_modelmanager_framework_headers() @@ -104,6 +127,8 @@ void CppToolsPlugin::test_modelmanager_framework_headers() ModelManagerTestHelper helper; CppModelManager *mm = CppModelManager::instance(); + const TestDataDirectory testDataDir(QLatin1String("testdata")); + Project *project = helper.createProject(QLatin1String("test_modelmanager_framework_headers")); ProjectInfo pi = mm->projectInfo(project); QCOMPARE(pi.project().data(), project); @@ -113,9 +138,10 @@ void CppToolsPlugin::test_modelmanager_framework_headers() part->cxxVersion = ProjectPart::CXX98; part->qtVersion = ProjectPart::Qt5; part->defines = QByteArray("#define OH_BEHAVE -1\n"); - part->includePaths << testIncludeDir(); - part->frameworkPaths << testFrameworksDir(); - const QString &source = testSource(QLatin1String("test_modelmanager_framework_headers.cpp")); + part->includePaths << testDataDir.includeDir(); + part->frameworkPaths << testDataDir.frameworksDir(); + const QString &source = testDataDir.fileFromSourcesDir( + QLatin1String("test_modelmanager_framework_headers.cpp")); part->files << ProjectFile(source, ProjectFile::CXXSource); mm->updateProjectInfo(pi); @@ -145,8 +171,12 @@ void CppToolsPlugin::test_modelmanager_refresh() ModelManagerTestHelper helper; CppModelManager *mm = CppModelManager::instance(); - const QString testCpp(testSource(QLatin1String("test_modelmanager_refresh.cpp"))); - const QString testHeader(testSource(QLatin1String("test_modelmanager_refresh.h"))); + const TestDataDirectory testDataDir(QLatin1String("testdata")); + + const QString testCpp(testDataDir.fileFromSourcesDir( + QLatin1String("test_modelmanager_refresh.cpp"))); + const QString testHeader(testDataDir.fileFromSourcesDir( + QLatin1String("test_modelmanager_refresh.h"))); Project *project = helper.createProject(QLatin1String("test_modelmanager_refresh")); ProjectInfo pi = mm->projectInfo(project); @@ -157,7 +187,7 @@ void CppToolsPlugin::test_modelmanager_refresh() part->cxxVersion = ProjectPart::CXX98; part->qtVersion = ProjectPart::Qt5; part->defines = QByteArray("#define OH_BEHAVE -1\n"); - part->includePaths = QStringList() << testIncludeDir(false); + part->includePaths = QStringList() << testDataDir.includeDir(false); part->files.append(ProjectFile(testCpp, ProjectFile::CXXSource)); mm->updateProjectInfo(pi); -- GitLab