From 02281ad4294f1f3e91c9c63de7b048f09b068e75 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Thu, 10 Dec 2015 13:46:04 +0100 Subject: [PATCH] Provide plugin unit test for gtest code parsing This test needs environment variable GOOGLETEST_DIR to point to an existing checkout of the googletest repository, or it will be skipped. Change-Id: I1e0ba250da84143b50f70589113ab6d6ea3a5b73 Reviewed-by: Niels Weber --- plugins/autotest/autotestunittests.cpp | 41 +++++++++++ plugins/autotest/autotestunittests.h | 1 + plugins/autotest/autotestunittests.qrc | 11 +++ plugins/autotest/testcodeparser.cpp | 5 ++ plugins/autotest/testcodeparser.h | 1 + plugins/autotest/testtreemodel.cpp | 18 +++++ plugins/autotest/testtreemodel.h | 2 + .../unit_test/simple_gt/simple_gt.pro | 5 ++ .../autotest/unit_test/simple_gt/src/main.cpp | 6 ++ .../autotest/unit_test/simple_gt/src/src.pro | 9 +++ .../unit_test/simple_gt/tests/gt1/further.cpp | 42 +++++++++++ .../unit_test/simple_gt/tests/gt1/gt1.pro | 10 +++ .../unit_test/simple_gt/tests/gt1/main.cpp | 71 +++++++++++++++++++ .../unit_test/simple_gt/tests/gt2/gt2.pro | 12 ++++ .../unit_test/simple_gt/tests/gt2/main.cpp | 48 +++++++++++++ .../unit_test/simple_gt/tests/gt2/queuetest.h | 32 +++++++++ .../simple_gt/tests/gtest_dependency.pri | 25 +++++++ .../unit_test/simple_gt/tests/tests.pro | 4 ++ 18 files changed, 343 insertions(+) create mode 100644 plugins/autotest/unit_test/simple_gt/simple_gt.pro create mode 100644 plugins/autotest/unit_test/simple_gt/src/main.cpp create mode 100644 plugins/autotest/unit_test/simple_gt/src/src.pro create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt1/further.cpp create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt1/gt1.pro create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt1/main.cpp create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt2/gt2.pro create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt2/main.cpp create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt2/queuetest.h create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gtest_dependency.pri create mode 100644 plugins/autotest/unit_test/simple_gt/tests/tests.pro diff --git a/plugins/autotest/autotestunittests.cpp b/plugins/autotest/autotestunittests.cpp index c80aae964eb..9582a23c68e 100644 --- a/plugins/autotest/autotestunittests.cpp +++ b/plugins/autotest/autotestunittests.cpp @@ -189,5 +189,46 @@ void AutoTestUnitTests::testCodeParserSwitchStartup_data() << expectedUnnamedQuickTests << expectedDataTagsCount; } +void AutoTestUnitTests::testCodeParserGTest() +{ + if (qgetenv("GOOGLETEST_DIR").isEmpty()) + QSKIP("This test needs googletest - set GOOGLETEST_DIR (point to googletest repository)"); + + NavigationWidget *navigation = NavigationWidget::instance(); + navigation->activateSubWidget(Constants::AUTOTEST_ID); + + CppTools::Tests::ProjectOpenerAndCloser projectManager; + CppTools::ProjectInfo projectInfo = projectManager.open( + QString(m_tmpDir->path() + QLatin1String("/simple_gt/simple_gt.pro")), true); + QVERIFY(projectInfo.isValid()); + + QSignalSpy parserSpy(m_model->parser(), SIGNAL(parsingFinished())); + QVERIFY(parserSpy.wait(20000)); + + QCOMPARE(m_model->gtestNamesCount(), 4); + QCOMPARE(m_model->parser()->gtestNamesAndSetsCount(), 9); // 9 == 3 + 2 + 2 + 2, see below + + QMap expectedNamesAndSets; + expectedNamesAndSets.insert(QStringLiteral("FactorialTest"), 3); + expectedNamesAndSets.insert(QStringLiteral("FactorialTest_Iterative"), 2); + expectedNamesAndSets.insert(QStringLiteral("Sum"), 2); + expectedNamesAndSets.insert(QStringLiteral("QueueTest"), 2); + + QMap foundNamesAndSets = m_model->gtestNamesAndSets(); + QCOMPARE(expectedNamesAndSets.size(), foundNamesAndSets.size()); + foreach (const QString &name, expectedNamesAndSets.keys()) + QCOMPARE(expectedNamesAndSets.value(name), foundNamesAndSets.value(name)); + + // check also that no Qt related tests have been found + QCOMPARE(m_model->autoTestsCount(), 0); + QCOMPARE(m_model->namedQuickTestsCount(), 0); + QCOMPARE(m_model->unnamedQuickTestsCount(), 0); + QCOMPARE(m_model->dataTagsCount(), 0); + + QCOMPARE(m_model->parser()->autoTestsCount(), 0); + QCOMPARE(m_model->parser()->namedQuickTestsCount(), 0); + QCOMPARE(m_model->parser()->unnamedQuickTestsCount(), 0); +} + } // namespace Internal } // namespace Autotest diff --git a/plugins/autotest/autotestunittests.h b/plugins/autotest/autotestunittests.h index e359e4175dc..ddd5249739c 100644 --- a/plugins/autotest/autotestunittests.h +++ b/plugins/autotest/autotestunittests.h @@ -45,6 +45,7 @@ private slots: void testCodeParser_data(); void testCodeParserSwitchStartup(); void testCodeParserSwitchStartup_data(); + void testCodeParserGTest(); private: TestTreeModel *m_model; diff --git a/plugins/autotest/autotestunittests.qrc b/plugins/autotest/autotestunittests.qrc index 25ce4044feb..96e5b05a31a 100644 --- a/plugins/autotest/autotestunittests.qrc +++ b/plugins/autotest/autotestunittests.qrc @@ -39,5 +39,16 @@ unit_test/mixed_atp/tests/auto/gui/gui.qbs unit_test/mixed_atp/tests/auto/quickauto/quickauto.qbs unit_test/mixed_atp/tests/auto/quickauto2/quickauto2.qbs + unit_test/simple_gt/src/main.cpp + unit_test/simple_gt/src/src.pro + unit_test/simple_gt/tests/gt1/further.cpp + unit_test/simple_gt/tests/gt1/gt1.pro + unit_test/simple_gt/tests/gt1/main.cpp + unit_test/simple_gt/tests/gt2/gt2.pro + unit_test/simple_gt/tests/gt2/main.cpp + unit_test/simple_gt/tests/gt2/queuetest.h + unit_test/simple_gt/tests/tests.pro + unit_test/simple_gt/simple_gt.pro + unit_test/simple_gt/tests/gtest_dependency.pri diff --git a/plugins/autotest/testcodeparser.cpp b/plugins/autotest/testcodeparser.cpp index af48f475682..0b73568443f 100644 --- a/plugins/autotest/testcodeparser.cpp +++ b/plugins/autotest/testcodeparser.cpp @@ -1079,6 +1079,11 @@ int TestCodeParser::unnamedQuickTestsCount() const { return m_unnamedQuickDocList.size(); } + +int TestCodeParser::gtestNamesAndSetsCount() const +{ + return m_gtestDocList.size(); +} #endif } // namespace Internal diff --git a/plugins/autotest/testcodeparser.h b/plugins/autotest/testcodeparser.h index ef2c555ba94..5861009e5ac 100644 --- a/plugins/autotest/testcodeparser.h +++ b/plugins/autotest/testcodeparser.h @@ -64,6 +64,7 @@ public: int autoTestsCount() const; int namedQuickTestsCount() const; int unnamedQuickTestsCount() const; + int gtestNamesAndSetsCount() const; #endif signals: diff --git a/plugins/autotest/testtreemodel.cpp b/plugins/autotest/testtreemodel.cpp index e79d2325e98..b52a768ef2b 100644 --- a/plugins/autotest/testtreemodel.cpp +++ b/plugins/autotest/testtreemodel.cpp @@ -818,6 +818,24 @@ int TestTreeModel::dataTagsCount() const } return dataTagCount; } + +int TestTreeModel::gtestNamesCount() const +{ + return m_googleTestRootItem ? m_googleTestRootItem->childCount() : 0; +} + +QMap TestTreeModel::gtestNamesAndSets() const +{ + QMap result; + + if (m_googleTestRootItem) { + for (int row = 0, count = m_googleTestRootItem->childCount(); row < count; ++row) { + const TestTreeItem *current = m_googleTestRootItem->childItem(row); + result.insert(current->name(), current->childCount()); + } + } + return result; +} #endif /***************************** Sort/Filter Model **********************************/ diff --git a/plugins/autotest/testtreemodel.h b/plugins/autotest/testtreemodel.h index bef6ec86f00..2f7f00a4e87 100644 --- a/plugins/autotest/testtreemodel.h +++ b/plugins/autotest/testtreemodel.h @@ -69,6 +69,8 @@ public: int namedQuickTestsCount() const; int unnamedQuickTestsCount() const; int dataTagsCount() const; + int gtestNamesCount() const; + QMap gtestNamesAndSets() const; #endif signals: diff --git a/plugins/autotest/unit_test/simple_gt/simple_gt.pro b/plugins/autotest/unit_test/simple_gt/simple_gt.pro new file mode 100644 index 00000000000..9ce665aceff --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/simple_gt.pro @@ -0,0 +1,5 @@ +TEMPLATE = subdirs + +SUBDIRS += src \ + tests + diff --git a/plugins/autotest/unit_test/simple_gt/src/main.cpp b/plugins/autotest/unit_test/simple_gt/src/main.cpp new file mode 100644 index 00000000000..25579cd61bc --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/src/main.cpp @@ -0,0 +1,6 @@ +#include + +int main(int argc, char *argv[]) { + QCoreApplication a(argc, argv); + return a.exec(); +} diff --git a/plugins/autotest/unit_test/simple_gt/src/src.pro b/plugins/autotest/unit_test/simple_gt/src/src.pro new file mode 100644 index 00000000000..03886429244 --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/src/src.pro @@ -0,0 +1,9 @@ +TEMPLATE = app + +CONFIG += qt console +CONFIG -= app_bundle + +QT += core + +SOURCES += main.cpp + diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt1/further.cpp b/plugins/autotest/unit_test/simple_gt/tests/gt1/further.cpp new file mode 100644 index 00000000000..559bde41635 --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gt1/further.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd +** All rights reserved. +** For any questions to The Qt Company, please use contact form at +** http://www.qt.io/contact-us +** +** This file is part of the Qt Creator Enterprise Auto Test Add-on. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. +** +** If you have questions regarding the use of this file, please use +** contact form at http://www.qt.io/contact-us +** +****************************************************************************/ +#include + +using namespace testing; + +int sum(int a, int b) +{ + return a + b; +} + +TEST(Sum, HandlePositives) +{ + EXPECT_EQ(5, sum(2, 3)); + EXPECT_EQ(5, sum(3, 2)); +} + +TEST(Sum, HandleZero) +{ + EXPECT_EQ(1, sum(0, 0)); +} + +TEST(FactorialTest, DISABLED_Fake) +{ + EXPECT_EQ(-4, sum(-2, -2)); +} diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt1/gt1.pro b/plugins/autotest/unit_test/simple_gt/tests/gt1/gt1.pro new file mode 100644 index 00000000000..1ebd037c070 --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gt1/gt1.pro @@ -0,0 +1,10 @@ +include(../gtest_dependency.pri) + +TEMPLATE = app +CONFIG += console c++11 +CONFIG -= app_bundle +CONFIG += thread +CONFIG -= qt + +SOURCES += main.cpp \ + further.cpp diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt1/main.cpp b/plugins/autotest/unit_test/simple_gt/tests/gt1/main.cpp new file mode 100644 index 00000000000..64e386b8862 --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gt1/main.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd +** All rights reserved. +** For any questions to The Qt Company, please use contact form at +** http://www.qt.io/contact-us +** +** This file is part of the Qt Creator Enterprise Auto Test Add-on. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. +** +** If you have questions regarding the use of this file, please use +** contact form at http://www.qt.io/contact-us +** +****************************************************************************/ +#include +#include + +using namespace testing; + +int factorial(int a) +{ + if (a == 0) + return 1; + if (a == 1) + return a; + return a * factorial(a - 1); +} + +int factorial_it(int a) +{ + int result = 1; + for (int i = 2; i <= a; ++i) + result *= i; + return result; +} + +TEST(FactorialTest, HandlesZeroInput) +{ + EXPECT_EQ(1, factorial(0)); +} + +TEST(FactorialTest, HandlesPositiveInput) +{ + ASSERT_EQ(1, factorial(1)); + ASSERT_THAT(factorial(2), Eq(2)); + EXPECT_EQ(6, factorial(3)); + EXPECT_EQ(40320, factorial(8)); +} + +TEST(FactorialTest_Iterative, HandlesZeroInput) +{ + ASSERT_EQ(1, factorial_it(0)); +} + +TEST(FactorialTest_Iterative, DISABLED_HandlesPositiveInput) +{ + ASSERT_EQ(1, factorial_it(1)); + ASSERT_EQ(2, factorial_it(2)); + ASSERT_EQ(6, factorial_it(3)); + ASSERT_EQ(40320, factorial_it(8)); +} + +int main(int argc, char *argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt2/gt2.pro b/plugins/autotest/unit_test/simple_gt/tests/gt2/gt2.pro new file mode 100644 index 00000000000..65796da9abe --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gt2/gt2.pro @@ -0,0 +1,12 @@ +include(../gtest_dependency.pri) + +TEMPLATE = app +CONFIG += qt console c++11 +CONFIG -= app_bundle + +HEADERS += \ + queuetest.h + +SOURCES += \ + main.cpp + diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt2/main.cpp b/plugins/autotest/unit_test/simple_gt/tests/gt2/main.cpp new file mode 100644 index 00000000000..2bda4c12d3f --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gt2/main.cpp @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd +** All rights reserved. +** For any questions to The Qt Company, please use contact form at +** http://www.qt.io/contact-us +** +** This file is part of the Qt Creator Enterprise Auto Test Add-on. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. +** +** If you have questions regarding the use of this file, please use +** contact form at http://www.qt.io/contact-us +** +****************************************************************************/ +#include +#include + +#include "queuetest.h" + +using namespace testing; + +TEST_F(QueueTest, IsEmptyInitialized) +{ + EXPECT_EQ(0, m_queue0.size()); +} + +TEST_F(QueueTest, DequeueWorks) +{ + ASSERT_FALSE(m_queue1.isEmpty()); + int n = m_queue1.dequeue(); + ASSERT_TRUE(m_queue1.isEmpty()); + EXPECT_EQ(1, n); + + ASSERT_FALSE(m_queue2.isEmpty()); + float f = m_queue2.dequeue(); + ASSERT_TRUE(m_queue2.isEmpty()); + EXPECT_EQ(1.0f, f); +} + +int main(int argc, char *argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt2/queuetest.h b/plugins/autotest/unit_test/simple_gt/tests/gt2/queuetest.h new file mode 100644 index 00000000000..aebabf0f336 --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gt2/queuetest.h @@ -0,0 +1,32 @@ +/**************************************************************************** +** +** Copyright (C) 2015 The Qt Company Ltd +** All rights reserved. +** For any questions to The Qt Company, please use contact form at +** http://www.qt.io/contact-us +** +** This file is part of the Qt Creator Enterprise Auto Test Add-on. +** +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. +** +** If you have questions regarding the use of this file, please use +** contact form at http://www.qt.io/contact-us +** +****************************************************************************/ +#include + +class QueueTest : public ::testing::Test +{ +protected: + virtual void SetUp() { + m_queue1.enqueue(1); + m_queue2.enqueue(1.0f); + } + + QQueue m_queue0; + QQueue m_queue1; + QQueue m_queue2; +}; diff --git a/plugins/autotest/unit_test/simple_gt/tests/gtest_dependency.pri b/plugins/autotest/unit_test/simple_gt/tests/gtest_dependency.pri new file mode 100644 index 00000000000..bd22edc2f20 --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/gtest_dependency.pri @@ -0,0 +1,25 @@ +isEmpty(GOOGLETEST_DIR):GOOGLETEST_DIR=$$(GOOGLETEST_DIR) + +isEmpty(GOOGLETEST_DIR):linux-* { + GTEST_SRCDIR = /usr/include/gtest + GMOCK_SRCDIR = /usr/include/gmock +} else { + GTEST_SRCDIR = $$GOOGLETEST_DIR/googletest + GMOCK_SRCDIR = $$GOOGLETEST_DIR/googlemock +} + +requires(exists($$GTEST_SRCDIR):exists($$GMOCK_SRCDIR)) +!exists($$GOOGLETEST_DIR):message("No googletest src dir found - set GOOGLETEST_DIR to enable.") + +DEFINES += \ + GTEST_LANG_CXX11 + +INCLUDEPATH *= \ + $$GTEST_SRCDIR \ + $$GTEST_SRCDIR/include \ + $$GMOCK_SRCDIR \ + $$GMOCK_SRCDIR/include + +SOURCES += \ + $$GTEST_SRCDIR/src/gtest-all.cc \ + $$GMOCK_SRCDIR/src/gmock-all.cc diff --git a/plugins/autotest/unit_test/simple_gt/tests/tests.pro b/plugins/autotest/unit_test/simple_gt/tests/tests.pro new file mode 100644 index 00000000000..77f3621946d --- /dev/null +++ b/plugins/autotest/unit_test/simple_gt/tests/tests.pro @@ -0,0 +1,4 @@ +TEMPLATE = subdirs + +SUBDIRS += gt1 \ + gt2