A test how to use QSignalSpy with GMock
authorAki Koskinen <maemo@akikoskinen.info>
Sun, 21 Mar 2010 13:09:49 +0000 (15:09 +0200)
committerAki Koskinen <maemo@akikoskinen.info>
Sun, 21 Mar 2010 13:15:54 +0000 (15:15 +0200)
tests/stlhelpers4qt.h [new file with mode: 0644]
tests/ut_gmocktest/painter.cpp
tests/ut_gmocktest/painter.h
tests/ut_gmocktest/ut_gmocktest.cpp
tests/ut_gmocktest/ut_gmocktest.pro

diff --git a/tests/stlhelpers4qt.h b/tests/stlhelpers4qt.h
new file mode 100644 (file)
index 0000000..b3c297f
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef STLHELPERS4QT_H
+#define STLHELPERS4QT_H
+
+#include <ostream>
+#include <QList>
+#include <QVariant>
+
+/**
+ * Outputs the contents of a QList<QVariant> 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<QVariant> &val)
+{
+    bool first = true;
+
+    foreach (const QVariant &v, val) {
+        if (!first) {
+            stream << ", ";
+        }
+        stream << v.toString().toStdString();
+        first = false;
+    }
+    return stream;
+}
+
+#endif // STLHELPERS4QT_H
index c5facf7..66c456b 100644 (file)
@@ -11,5 +11,6 @@ Painter::~Painter() {
 
 bool Painter::DrawCircle(int x, int y, int radius) {
     turtle->PenDown();
+    emit DrawCircleCalled(x, y, radius);
     return true;
 }
index 00a8129..9e76bdc 100644 (file)
@@ -1,10 +1,14 @@
 #ifndef PAINTER_H
 #define PAINTER_H
 
+#include <QObject>
+
 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
index 5e416cb..e307bd6 100644 (file)
@@ -4,8 +4,10 @@
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 
-using ::testing::AtLeast;
+#include <QSignalSpy>
+#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<QVariant> expected = QList<QVariant>() << 0 << 0 << 10;
+    ASSERT_EQ(expected, spy.at(0));
+}
+
 int main(int argc, char *argv[])
 {
     ::testing::InitGoogleMock(&argc, argv);
index 2f2244f..1a9706b 100644 (file)
@@ -1,4 +1,5 @@
 TARGET = ut_gmocktest
+QT += testlib
 QT -= gui
 CONFIG += console
 CONFIG -= app_bundle