From 8b4b70a766440fae80289e799282c02d742dfffa Mon Sep 17 00:00:00 2001 From: Aki Koskinen Date: Sun, 21 Mar 2010 15:09:49 +0200 Subject: [PATCH] A test how to use QSignalSpy with GMock --- tests/stlhelpers4qt.h | 28 ++++++++++++++++++++++++++++ tests/ut_gmocktest/painter.cpp | 1 + tests/ut_gmocktest/painter.h | 9 ++++++++- tests/ut_gmocktest/ut_gmocktest.cpp | 18 +++++++++++++++++- tests/ut_gmocktest/ut_gmocktest.pro | 1 + 5 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 tests/stlhelpers4qt.h diff --git a/tests/stlhelpers4qt.h b/tests/stlhelpers4qt.h new file mode 100644 index 0000000..b3c297f --- /dev/null +++ b/tests/stlhelpers4qt.h @@ -0,0 +1,28 @@ +#ifndef STLHELPERS4QT_H +#define STLHELPERS4QT_H + +#include +#include +#include + +/** + * Outputs the contents of a QList container to a std::ostream object. + * @param stream the stream to output to + * @param val the container that is put to the stream + * @return the same stream that was passed as the parameter @a stream + */ +std::ostream& operator<<(std::ostream& stream, const QList &val) +{ + bool first = true; + + foreach (const QVariant &v, val) { + if (!first) { + stream << ", "; + } + stream << v.toString().toStdString(); + first = false; + } + return stream; +} + +#endif // STLHELPERS4QT_H diff --git a/tests/ut_gmocktest/painter.cpp b/tests/ut_gmocktest/painter.cpp index c5facf7..66c456b 100644 --- a/tests/ut_gmocktest/painter.cpp +++ b/tests/ut_gmocktest/painter.cpp @@ -11,5 +11,6 @@ Painter::~Painter() { bool Painter::DrawCircle(int x, int y, int radius) { turtle->PenDown(); + emit DrawCircleCalled(x, y, radius); return true; } diff --git a/tests/ut_gmocktest/painter.h b/tests/ut_gmocktest/painter.h index 00a8129..9e76bdc 100644 --- a/tests/ut_gmocktest/painter.h +++ b/tests/ut_gmocktest/painter.h @@ -1,10 +1,14 @@ #ifndef PAINTER_H #define PAINTER_H +#include + class Turtle; -class Painter +class Painter : public QObject { + Q_OBJECT + Turtle *turtle; public: @@ -12,6 +16,9 @@ public: virtual ~Painter(); bool DrawCircle(int x, int y, int radius); + +signals: + void DrawCircleCalled(int x, int y, int radius); }; #endif // PAINTER_H diff --git a/tests/ut_gmocktest/ut_gmocktest.cpp b/tests/ut_gmocktest/ut_gmocktest.cpp index 5e416cb..e307bd6 100644 --- a/tests/ut_gmocktest/ut_gmocktest.cpp +++ b/tests/ut_gmocktest/ut_gmocktest.cpp @@ -4,8 +4,10 @@ #include #include -using ::testing::AtLeast; +#include +#include "../stlhelpers4qt.h" +using ::testing::AtLeast; TEST(PainterTest, TestTurtlePenDownCalledAtLeastOnceWhenDrawCircleCalled) { @@ -17,6 +19,20 @@ TEST(PainterTest, TestTurtlePenDownCalledAtLeastOnceWhenDrawCircleCalled) EXPECT_TRUE(painter.DrawCircle(0, 0, 10)); } +TEST(PainterTest, TestSignalEmittedWhenDrawCircleCalled) +{ + MockTurtle turtle; + + Painter painter(&turtle); + + QSignalSpy spy(&painter, SIGNAL(DrawCircleCalled(int,int,int))); + + painter.DrawCircle(0, 0, 10); + ASSERT_EQ(1, spy.count()); + QList expected = QList() << 0 << 0 << 10; + ASSERT_EQ(expected, spy.at(0)); +} + int main(int argc, char *argv[]) { ::testing::InitGoogleMock(&argc, argv); diff --git a/tests/ut_gmocktest/ut_gmocktest.pro b/tests/ut_gmocktest/ut_gmocktest.pro index 2f2244f..1a9706b 100644 --- a/tests/ut_gmocktest/ut_gmocktest.pro +++ b/tests/ut_gmocktest/ut_gmocktest.pro @@ -1,4 +1,5 @@ TARGET = ut_gmocktest +QT += testlib QT -= gui CONFIG += console CONFIG -= app_bundle -- 1.7.9.5