From: Manuel Nickschas Date: Thu, 4 Oct 2018 16:59:59 +0000 (+0200) Subject: test: Add GTest printers for commonly used Qt types X-Git-Tag: test-travis-01~111 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=98ffecc6e97b208712c6f5c5da1b4ba10aa09333 test: Add GTest printers for commonly used Qt types 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. --- diff --git a/src/test/global/CMakeLists.txt b/src/test/global/CMakeLists.txt index f3f4ed2f..f5ff81a8 100644 --- a/src/test/global/CMakeLists.txt +++ b/src/test/global/CMakeLists.txt @@ -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 index 00000000..d3dbbf8c --- /dev/null +++ b/src/test/global/printers.cpp @@ -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 + +namespace { + +template +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 index 00000000..108abaf9 --- /dev/null +++ b/src/test/global/printers.h @@ -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 + +#include +#include +#include +#include +#include +#include + +#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*); diff --git a/src/test/global/testglobal.h b/src/test/global/testglobal.h index cc7ce978..a2ad4ce1 100644 --- a/src/test/global/testglobal.h +++ b/src/test/global/testglobal.h @@ -20,6 +20,6 @@ #pragma once -#include "test-global-export.h" - #include + +#include "printers.h"