test: Add GTest printers for commonly used Qt types
authorManuel Nickschas <sputnick@quassel-irc.org>
Thu, 4 Oct 2018 16:59:59 +0000 (18:59 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
Because GTest by default prints out unknown types as hexdump or other
badly readably formats, provide PrintTo functions for Qt types we use
in tests. That way, GTest will use those instead of falling back to
its own default.

Reuse Qt's own QDebug stream support to avoid having to reimplement
printing functionality for all the types. This makes it very easy
to see e.g. the contents of a QVariantMap or other complex types.

src/test/global/CMakeLists.txt
src/test/global/printers.cpp [new file with mode: 0644]
src/test/global/printers.h [new file with mode: 0644]
src/test/global/testglobal.h

index f3f4ed2..f5ff81a 100644 (file)
@@ -1,6 +1,7 @@
 quassel_add_module(Test::Global EXPORT NOINSTALL)
 
 target_sources(${TARGET} PRIVATE
+    printers.cpp
     testglobal.h
 )
 
diff --git a/src/test/global/printers.cpp b/src/test/global/printers.cpp
new file mode 100644 (file)
index 0000000..d3dbbf8
--- /dev/null
@@ -0,0 +1,67 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#include "printers.h"
+
+#include <QDebug>
+
+namespace {
+
+template<typename T>
+void debugOut(const T& value, ::std::ostream* os)
+{
+    // Just use Qt's own debug print support to print the value into a string
+    QString out;
+    QDebug dbg(&out);
+    dbg.nospace() << value;
+    *os << out.toStdString();
+}
+
+}  // anon
+
+void PrintTo(const QByteArray& value, std::ostream* os)
+{
+    debugOut(value, os);
+}
+
+void PrintTo(const QDateTime& value, std::ostream* os)
+{
+    debugOut(value, os);
+}
+
+void PrintTo(const QString& value, std::ostream* os)
+{
+    debugOut(value, os);
+}
+
+void PrintTo(const QVariant& value, std::ostream* os)
+{
+    debugOut(value, os);
+}
+
+void PrintTo(const QVariantList& value, std::ostream* os)
+{
+    debugOut(value, os);
+}
+
+void PrintTo(const QVariantMap& value, std::ostream* os)
+{
+    debugOut(value, os);
+}
diff --git a/src/test/global/printers.h b/src/test/global/printers.h
new file mode 100644 (file)
index 0000000..108abaf
--- /dev/null
@@ -0,0 +1,43 @@
+/***************************************************************************
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
+ ***************************************************************************/
+
+#pragma once
+
+#include "test-global-export.h"
+
+#include <iostream>
+
+#include <QByteArray>
+#include <QDateTime>
+#include <QString>
+#include <QVariant>
+#include <QVariantList>
+#include <QVariantMap>
+
+#include "testglobal.h"
+
+// GTest printers for commonly used Qt types
+
+TEST_GLOBAL_EXPORT void PrintTo(const QByteArray&, std::ostream*);
+TEST_GLOBAL_EXPORT void PrintTo(const QDateTime&, std::ostream*);
+TEST_GLOBAL_EXPORT void PrintTo(const QString&, std::ostream*);
+TEST_GLOBAL_EXPORT void PrintTo(const QVariant&, std::ostream*);
+TEST_GLOBAL_EXPORT void PrintTo(const QVariantList&, std::ostream*);
+TEST_GLOBAL_EXPORT void PrintTo(const QVariantMap&, std::ostream*);
index cc7ce97..a2ad4ce 100644 (file)
@@ -20,6 +20,6 @@
 
 #pragma once
 
-#include "test-global-export.h"
-
 #include <gmock/gmock.h>
+
+#include "printers.h"