From 9055ac5f50f023d6bacb2b6c26cbb5fa7a995503 Mon Sep 17 00:00:00 2001
From: Christian Stenger <christian.stenger@theqtcompany.com>
Date: Wed, 13 Jan 2016 09:10:11 +0100
Subject: [PATCH] Provide parameterized gtest for plugin unit tests

Change-Id: I17a42a9070546a5b461fcb27643bd6db7b5d7b4f
Reviewed-by: Niels Weber <niels.weber@theqtcompany.com>
---
 plugins/autotest/autotestunittests.cpp        | 13 +--
 plugins/autotest/autotestunittests.qrc        |  3 +
 plugins/autotest/testtreemodel.cpp            |  4 +-
 plugins/autotest/testtreemodel.h              |  2 +-
 .../unit_test/simple_gt/tests/gt3/dummytest.h | 37 ++++++++
 .../unit_test/simple_gt/tests/gt3/gt3.pro     | 12 +++
 .../unit_test/simple_gt/tests/gt3/main.cpp    | 85 +++++++++++++++++++
 .../unit_test/simple_gt/tests/tests.pro       |  3 +-
 8 files changed, 150 insertions(+), 9 deletions(-)
 create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt3/dummytest.h
 create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt3/gt3.pro
 create mode 100644 plugins/autotest/unit_test/simple_gt/tests/gt3/main.cpp

diff --git a/plugins/autotest/autotestunittests.cpp b/plugins/autotest/autotestunittests.cpp
index 9582a23c68e..95bc954502f 100644
--- a/plugins/autotest/autotestunittests.cpp
+++ b/plugins/autotest/autotestunittests.cpp
@@ -205,19 +205,22 @@ void AutoTestUnitTests::testCodeParserGTest()
     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
+    QCOMPARE(m_model->gtestNamesCount(), 6);
+    // 11 == 3 + 2 + 2 + 2 + 1 + 1, see below
+    QCOMPARE(m_model->parser()->gtestNamesAndSetsCount(), 11);
 
-    QMap<QString, int> expectedNamesAndSets;
+    QMultiMap<QString, int> expectedNamesAndSets;
     expectedNamesAndSets.insert(QStringLiteral("FactorialTest"), 3);
     expectedNamesAndSets.insert(QStringLiteral("FactorialTest_Iterative"), 2);
     expectedNamesAndSets.insert(QStringLiteral("Sum"), 2);
     expectedNamesAndSets.insert(QStringLiteral("QueueTest"), 2);
+    expectedNamesAndSets.insert(QStringLiteral("DummyTest"), 1); // used as parameterized test
+    expectedNamesAndSets.insert(QStringLiteral("DummyTest"), 1); // used as 'normal' test
 
-    QMap<QString, int> foundNamesAndSets = m_model->gtestNamesAndSets();
+    QMultiMap<QString, int> foundNamesAndSets = m_model->gtestNamesAndSets();
     QCOMPARE(expectedNamesAndSets.size(), foundNamesAndSets.size());
     foreach (const QString &name, expectedNamesAndSets.keys())
-        QCOMPARE(expectedNamesAndSets.value(name), foundNamesAndSets.value(name));
+        QCOMPARE(expectedNamesAndSets.values(name), foundNamesAndSets.values(name));
 
     // check also that no Qt related tests have been found
     QCOMPARE(m_model->autoTestsCount(), 0);
diff --git a/plugins/autotest/autotestunittests.qrc b/plugins/autotest/autotestunittests.qrc
index 96e5b05a31a..861f5c26654 100644
--- a/plugins/autotest/autotestunittests.qrc
+++ b/plugins/autotest/autotestunittests.qrc
@@ -50,5 +50,8 @@
         <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>
+        <file>unit_test/simple_gt/tests/gt3/dummytest.h</file>
+        <file>unit_test/simple_gt/tests/gt3/gt3.pro</file>
+        <file>unit_test/simple_gt/tests/gt3/main.cpp</file>
     </qresource>
 </RCC>
diff --git a/plugins/autotest/testtreemodel.cpp b/plugins/autotest/testtreemodel.cpp
index b52a768ef2b..d6e4d1d724d 100644
--- a/plugins/autotest/testtreemodel.cpp
+++ b/plugins/autotest/testtreemodel.cpp
@@ -824,9 +824,9 @@ int TestTreeModel::gtestNamesCount() const
     return m_googleTestRootItem ? m_googleTestRootItem->childCount() : 0;
 }
 
-QMap<QString, int> TestTreeModel::gtestNamesAndSets() const
+QMultiMap<QString, int> TestTreeModel::gtestNamesAndSets() const
 {
-    QMap<QString, int> result;
+    QMultiMap<QString, int> result;
 
     if (m_googleTestRootItem) {
         for (int row = 0, count = m_googleTestRootItem->childCount(); row < count; ++row) {
diff --git a/plugins/autotest/testtreemodel.h b/plugins/autotest/testtreemodel.h
index 2f7f00a4e87..c8eefd042fc 100644
--- a/plugins/autotest/testtreemodel.h
+++ b/plugins/autotest/testtreemodel.h
@@ -70,7 +70,7 @@ public:
     int unnamedQuickTestsCount() const;
     int dataTagsCount() const;
     int gtestNamesCount() const;
-    QMap<QString, int> gtestNamesAndSets() const;
+    QMultiMap<QString, int> gtestNamesAndSets() const;
 #endif
 
 signals:
diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt3/dummytest.h b/plugins/autotest/unit_test/simple_gt/tests/gt3/dummytest.h
new file mode 100644
index 00000000000..b8fac8e799d
--- /dev/null
+++ b/plugins/autotest/unit_test/simple_gt/tests/gt3/dummytest.h
@@ -0,0 +1,37 @@
+/****************************************************************************
+**
+** 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 <QString>
+
+class DummyTest : public ::testing::TestWithParam<const char *>
+{
+protected:
+    virtual void SetUp() {
+        m_str1 = QString::fromLatin1(GetParam()).toLower();
+
+        m_str2 = QString::fromLatin1(GetParam()).toUpper();
+
+        m_str3 = QString::fromLatin1(GetParam()).toHtmlEscaped();
+    }
+
+    QString m_str1;
+    QString m_str2;
+    QString m_str3;
+};
diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt3/gt3.pro b/plugins/autotest/unit_test/simple_gt/tests/gt3/gt3.pro
new file mode 100644
index 00000000000..4830351f08f
--- /dev/null
+++ b/plugins/autotest/unit_test/simple_gt/tests/gt3/gt3.pro
@@ -0,0 +1,12 @@
+include(../gtest_dependency.pri)
+
+TEMPLATE = app
+CONFIG += qt console c++11
+CONFIG -= app_bundle
+
+HEADERS += \
+    dummytest.h
+
+SOURCES += \
+    main.cpp
+
diff --git a/plugins/autotest/unit_test/simple_gt/tests/gt3/main.cpp b/plugins/autotest/unit_test/simple_gt/tests/gt3/main.cpp
new file mode 100644
index 00000000000..088506ec555
--- /dev/null
+++ b/plugins/autotest/unit_test/simple_gt/tests/gt3/main.cpp
@@ -0,0 +1,85 @@
+/****************************************************************************
+**
+** 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 "dummytest.h"
+
+using namespace testing;
+
+#include <QMap>
+#include <QStringList>
+
+static QMap<const char *, QStringList> testValues = {
+    { "DummyTest",
+      {QStringLiteral("dummytest"), QStringLiteral("DUMMYTEST"), QStringLiteral("DummyTest")}
+    },
+    { "Hello World",
+      {QStringLiteral("hello world"), QStringLiteral("HELLO WORLD"), QStringLiteral("Hello World")}
+    },
+    { "#include <QString>\n#include \"test.h\"",
+      {QStringLiteral("#include <qstring>\n#include \"test.h\""),
+       QStringLiteral("#INCLUDE <QSTRING>\n#INCLUDE \"TEST.H\""),
+       QStringLiteral("#include &lt;QString&gt;\n#include &quot;test.h&quot;")
+      }
+    }
+};
+
+static QMap<const char *, QStringList> testValuesSec = {
+    { "BlAh",
+      {QStringLiteral("blah"), QStringLiteral("BLAH"), QStringLiteral("BlAh")}
+    },
+    { "<html>",
+      {QStringLiteral("<html>"), QStringLiteral("<HTML>"), QStringLiteral("&lt;html&gt;")}
+    },
+};
+
+INSTANTIATE_TEST_CASE_P(First, DummyTest, ::testing::ValuesIn(testValues.keys()));
+INSTANTIATE_TEST_CASE_P(Second, DummyTest, ::testing::ValuesIn(testValuesSec.keys()));
+
+TEST_P(DummyTest, Easy)
+{
+    // total wrong usage, but this is for testing purpose
+    bool first = testValues.contains(GetParam());
+    bool second = testValuesSec.contains(GetParam());
+    QStringList expected;
+    if (first)
+        expected = testValues.value(GetParam());
+    else if (second)
+        expected = testValuesSec.value(GetParam());
+    else
+        FAIL();
+
+    ASSERT_EQ(3, expected.size());
+
+    EXPECT_EQ(m_str1, expected.at(0));
+    EXPECT_EQ(m_str2, expected.at(1));
+    EXPECT_EQ(m_str3, expected.at(2));
+}
+
+TEST(DummyTest, BlaBlubb)
+{
+    ASSERT_EQ(3, testValues.size());
+}
+
+int main(int argc, char *argv[])
+{
+    InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}
diff --git a/plugins/autotest/unit_test/simple_gt/tests/tests.pro b/plugins/autotest/unit_test/simple_gt/tests/tests.pro
index 77f3621946d..424362adbdf 100644
--- a/plugins/autotest/unit_test/simple_gt/tests/tests.pro
+++ b/plugins/autotest/unit_test/simple_gt/tests/tests.pro
@@ -1,4 +1,5 @@
 TEMPLATE = subdirs
 
 SUBDIRS += gt1 \
-           gt2
+           gt2 \
+           gt3
-- 
GitLab