Commit 02281ad4 authored by Christian Stenger's avatar Christian Stenger Committed by Niels Weber
Browse files

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: default avatarNiels Weber <niels.weber@theqtcompany.com>
parent 1abd193b
......@@ -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<QString, int> expectedNamesAndSets;
expectedNamesAndSets.insert(QStringLiteral("FactorialTest"), 3);
expectedNamesAndSets.insert(QStringLiteral("FactorialTest_Iterative"), 2);
expectedNamesAndSets.insert(QStringLiteral("Sum"), 2);
expectedNamesAndSets.insert(QStringLiteral("QueueTest"), 2);
QMap<QString, int> 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
......@@ -45,6 +45,7 @@ private slots:
void testCodeParser_data();
void testCodeParserSwitchStartup();
void testCodeParserSwitchStartup_data();
void testCodeParserGTest();
private:
TestTreeModel *m_model;
......
......@@ -39,5 +39,16 @@
<file>unit_test/mixed_atp/tests/auto/gui/gui.qbs</file>
<file>unit_test/mixed_atp/tests/auto/quickauto/quickauto.qbs</file>
<file>unit_test/mixed_atp/tests/auto/quickauto2/quickauto2.qbs</file>
<file>unit_test/simple_gt/src/main.cpp</file>
<file>unit_test/simple_gt/src/src.pro</file>
<file>unit_test/simple_gt/tests/gt1/further.cpp</file>
<file>unit_test/simple_gt/tests/gt1/gt1.pro</file>
<file>unit_test/simple_gt/tests/gt1/main.cpp</file>
<file>unit_test/simple_gt/tests/gt2/gt2.pro</file>
<file>unit_test/simple_gt/tests/gt2/main.cpp</file>
<file>unit_test/simple_gt/tests/gt2/queuetest.h</file>
<file>unit_test/simple_gt/tests/tests.pro</file>
<file>unit_test/simple_gt/simple_gt.pro</file>
<file>unit_test/simple_gt/tests/gtest_dependency.pri</file>
</qresource>
</RCC>
......@@ -1079,6 +1079,11 @@ int TestCodeParser::unnamedQuickTestsCount() const
{
return m_unnamedQuickDocList.size();
}
int TestCodeParser::gtestNamesAndSetsCount() const
{
return m_gtestDocList.size();
}
#endif
} // namespace Internal
......
......@@ -64,6 +64,7 @@ public:
int autoTestsCount() const;
int namedQuickTestsCount() const;
int unnamedQuickTestsCount() const;
int gtestNamesAndSetsCount() const;
#endif
signals:
......
......@@ -818,6 +818,24 @@ int TestTreeModel::dataTagsCount() const
}
return dataTagCount;
}
int TestTreeModel::gtestNamesCount() const
{
return m_googleTestRootItem ? m_googleTestRootItem->childCount() : 0;
}
QMap<QString, int> TestTreeModel::gtestNamesAndSets() const
{
QMap<QString, int> 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 **********************************/
......
......@@ -69,6 +69,8 @@ public:
int namedQuickTestsCount() const;
int unnamedQuickTestsCount() const;
int dataTagsCount() const;
int gtestNamesCount() const;
QMap<QString, int> gtestNamesAndSets() const;
#endif
signals:
......
TEMPLATE = subdirs
SUBDIRS += src \
tests
#include <QCoreApplication>
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
return a.exec();
}
TEMPLATE = app
CONFIG += qt console
CONFIG -= app_bundle
QT += core
SOURCES += main.cpp
/****************************************************************************
**
** 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 <gtest/gtest.h>
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));
}
include(../gtest_dependency.pri)
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG += thread
CONFIG -= qt
SOURCES += main.cpp \
further.cpp
/****************************************************************************
**
** 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 <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
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();
}
include(../gtest_dependency.pri)
TEMPLATE = app
CONFIG += qt console c++11
CONFIG -= app_bundle
HEADERS += \
queuetest.h
SOURCES += \
main.cpp
/****************************************************************************
**
** 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 <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#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();
}
/****************************************************************************
**
** 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 <QQueue>
class QueueTest : public ::testing::Test
{
protected:
virtual void SetUp() {
m_queue1.enqueue(1);
m_queue2.enqueue(1.0f);
}
QQueue<double> m_queue0;
QQueue<int> m_queue1;
QQueue<float> m_queue2;
};
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
TEMPLATE = subdirs
SUBDIRS += gt1 \
gt2
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment