Merge branch 'common_services'
authorAki Koskinen <maemo@akikoskinen.info>
Sun, 21 Mar 2010 15:19:26 +0000 (17:19 +0200)
committerAki Koskinen <maemo@akikoskinen.info>
Sun, 21 Mar 2010 15:19:26 +0000 (17:19 +0200)
16 files changed:
.gitignore
prepareGmock.sh [new file with mode: 0755]
tests/gmock.pri [new file with mode: 0644]
tests/ut_common.pri [new file with mode: 0644]
tests/ut_gmocktest/.gitignore [new file with mode: 0644]
tests/ut_gmocktest/mock_turtle.h [new file with mode: 0644]
tests/ut_gmocktest/painter.cpp [new file with mode: 0644]
tests/ut_gmocktest/painter.h [new file with mode: 0644]
tests/ut_gmocktest/turtle.h [new file with mode: 0644]
tests/ut_gmocktest/ut_gmocktest.cpp [new file with mode: 0644]
tests/ut_gmocktest/ut_gmocktest.pro [new file with mode: 0644]
tests/ut_template/.gitignore [new file with mode: 0644]
tests/ut_template/ut_template.cpp [new file with mode: 0644]
tests/ut_template/ut_template.h [new file with mode: 0644]
tests/ut_template/ut_template.pro [new file with mode: 0644]
tests/util/stlhelpers4qt.h [new file with mode: 0644]

index 8cb892a..ce4eb73 100644 (file)
@@ -1,4 +1,6 @@
 *.o
+moc_*
 *.la
 *.lo
 *~
+gmock/*
diff --git a/prepareGmock.sh b/prepareGmock.sh
new file mode 100755 (executable)
index 0000000..484c8dd
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+# A script to fetch GMock from source repository, compile it and run the tests.
+
+PRE_WD=`pwd`
+
+# Go to correct directory
+cd `dirname $0`
+
+# Get gmock from svn
+echo "Fetching GMock source from http://googlemock.googlecode.com"
+svn checkout http://googlemock.googlecode.com/svn/trunk/ gmock
+
+cd gmock
+echo "Running autoreconf..."
+AUTOMAKE=automake-1.9 ACLOCAL=aclocal-1.9 autoreconf -f -v -i
+
+mkdir build
+cd build
+
+echo "Running configure..."
+../configure
+
+echo "Running make..."
+make -j3
+echo "Running make check..."
+make check
+
+echo "All done"
+# Go back to original directory
+cd $PRE_WD
diff --git a/tests/gmock.pri b/tests/gmock.pri
new file mode 100644 (file)
index 0000000..9596887
--- /dev/null
@@ -0,0 +1,3 @@
+GMOCKBUILDDIR = ../gmock/build
+QMAKE_CXXFLAGS += $$system($$GMOCKBUILDDIR/scripts/gmock-config --cppflags --cxxflags)
+QMAKE_LIBS += ../$$GMOCKBUILDDIR/lib/.libs/libgmock.a ../$$GMOCKBUILDDIR/gtest/lib/.libs/libgtest.a
diff --git a/tests/ut_common.pri b/tests/ut_common.pri
new file mode 100644 (file)
index 0000000..d813d82
--- /dev/null
@@ -0,0 +1 @@
+INCLUDEPATH += ../util
diff --git a/tests/ut_gmocktest/.gitignore b/tests/ut_gmocktest/.gitignore
new file mode 100644 (file)
index 0000000..b74f358
--- /dev/null
@@ -0,0 +1,2 @@
+Makefile
+ut_gmocktest
diff --git a/tests/ut_gmocktest/mock_turtle.h b/tests/ut_gmocktest/mock_turtle.h
new file mode 100644 (file)
index 0000000..aea8904
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef MOCK_TURTLE_H
+#define MOCK_TURTLE_H
+
+#include "turtle.h"
+#include <gmock/gmock.h>
+
+class MockTurtle : public Turtle {
+public:
+    MOCK_METHOD0(PenUp, void());
+    MOCK_METHOD0(PenDown, void());
+    MOCK_METHOD1(Forward, void(int distance));
+    MOCK_METHOD1(Turn, void(int degrees));
+    MOCK_METHOD2(GoTo, void(int x, int y));
+    MOCK_CONST_METHOD0(GetX, int());
+    MOCK_CONST_METHOD0(GetY, int());
+};
+
+#endif // MOCK_TURTLE_H
diff --git a/tests/ut_gmocktest/painter.cpp b/tests/ut_gmocktest/painter.cpp
new file mode 100644 (file)
index 0000000..66c456b
--- /dev/null
@@ -0,0 +1,16 @@
+#include "painter.h"
+#include "turtle.h"
+
+Painter::Painter(Turtle *turtle) :
+        turtle(turtle)
+{
+}
+
+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
new file mode 100644 (file)
index 0000000..9e76bdc
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef PAINTER_H
+#define PAINTER_H
+
+#include <QObject>
+
+class Turtle;
+
+class Painter : public QObject
+{
+    Q_OBJECT
+
+    Turtle *turtle;
+
+public:
+    Painter(Turtle *turtle);
+    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/turtle.h b/tests/ut_gmocktest/turtle.h
new file mode 100644 (file)
index 0000000..416b7cd
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef TURTLE_H
+#define TURTLE_H
+
+class Turtle {
+public:
+    virtual ~Turtle() {}
+    virtual void PenUp() = 0;
+    virtual void PenDown() = 0;
+    virtual void Forward(int distance) = 0;
+    virtual void Turn(int degrees) = 0;
+    virtual void GoTo(int x, int y) = 0;
+    virtual int GetX() const = 0;
+    virtual int GetY() const = 0;
+};
+
+#endif // TURTLE_H
diff --git a/tests/ut_gmocktest/ut_gmocktest.cpp b/tests/ut_gmocktest/ut_gmocktest.cpp
new file mode 100644 (file)
index 0000000..e756c8c
--- /dev/null
@@ -0,0 +1,40 @@
+#include "mock_turtle.h"
+#include "painter.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <QSignalSpy>
+#include "stlhelpers4qt.h"
+
+using ::testing::AtLeast;
+
+TEST(PainterTest, TestTurtlePenDownCalledAtLeastOnceWhenDrawCircleCalled)
+{
+    MockTurtle turtle;
+    EXPECT_CALL(turtle, PenDown()).Times(AtLeast(1));
+
+    Painter painter(&turtle);
+
+    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);
+    return RUN_ALL_TESTS();
+}
diff --git a/tests/ut_gmocktest/ut_gmocktest.pro b/tests/ut_gmocktest/ut_gmocktest.pro
new file mode 100644 (file)
index 0000000..8a516e3
--- /dev/null
@@ -0,0 +1,18 @@
+include(../ut_common.pri)
+
+TARGET = ut_gmocktest
+QT += testlib
+QT -= gui
+CONFIG += console
+CONFIG -= app_bundle
+TEMPLATE = app
+OBJECTS_DIR = .obj
+MOC_DIR = .moc
+SOURCES += ut_gmocktest.cpp \
+    painter.cpp
+HEADERS += \
+    turtle.h \
+    mock_turtle.h \
+    painter.h
+
+include(../gmock.pri)
diff --git a/tests/ut_template/.gitignore b/tests/ut_template/.gitignore
new file mode 100644 (file)
index 0000000..3e300fc
--- /dev/null
@@ -0,0 +1,2 @@
+Makefile
+ut_template
diff --git a/tests/ut_template/ut_template.cpp b/tests/ut_template/ut_template.cpp
new file mode 100644 (file)
index 0000000..58afcbb
--- /dev/null
@@ -0,0 +1,24 @@
+#include "ut_template.h"
+#include <QtTest/QtTest>
+
+void Ut_template::initTestCase()
+{
+
+}
+
+void Ut_template::cleanupTestCase()
+{
+
+}
+
+void Ut_template::init()
+{
+
+}
+
+void Ut_template::cleanup()
+{
+
+}
+
+QTEST_MAIN(Ut_template)
diff --git a/tests/ut_template/ut_template.h b/tests/ut_template/ut_template.h
new file mode 100644 (file)
index 0000000..91a2b47
--- /dev/null
@@ -0,0 +1,22 @@
+#ifndef UT_TEMPLATE_H
+#define UT_TEMPLATE_H
+
+#include <QObject>
+
+class Ut_template : public QObject
+{
+    Q_OBJECT
+
+private slots:
+    // Will be called before the first testfunction is executed.
+    void initTestCase();
+    // Will be called after the last testfunction was executed.
+    void cleanupTestCase();
+    // Will be called before each testfunction is executed.
+    void init();
+    // Will be called after every testfunction.
+    void cleanup();
+
+};
+
+#endif // UT_TEMPLATE_H
diff --git a/tests/ut_template/ut_template.pro b/tests/ut_template/ut_template.pro
new file mode 100644 (file)
index 0000000..dd7ce43
--- /dev/null
@@ -0,0 +1,12 @@
+include(../ut_common.pri)
+
+TARGET = ut_template
+QT += testlib
+QT -= gui
+CONFIG += console
+CONFIG -= app_bundle
+TEMPLATE = app
+OBJECTS_DIR = .obj
+MOC_DIR = .moc
+SOURCES += ut_template.cpp
+HEADERS += ut_template.h
diff --git a/tests/util/stlhelpers4qt.h b/tests/util/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