GMock unit test tryout
authorAki Koskinen <maemo@akikoskinen.info>
Sat, 20 Mar 2010 09:45:58 +0000 (11:45 +0200)
committerAki Koskinen <maemo@akikoskinen.info>
Sun, 21 Mar 2010 13:15:54 +0000 (15:15 +0200)
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]

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..c5facf7
--- /dev/null
@@ -0,0 +1,15 @@
+#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();
+    return true;
+}
diff --git a/tests/ut_gmocktest/painter.h b/tests/ut_gmocktest/painter.h
new file mode 100644 (file)
index 0000000..00a8129
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef PAINTER_H
+#define PAINTER_H
+
+class Turtle;
+
+class Painter
+{
+    Turtle *turtle;
+
+public:
+    Painter(Turtle *turtle);
+    virtual ~Painter();
+
+    bool DrawCircle(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..5e416cb
--- /dev/null
@@ -0,0 +1,24 @@
+#include "mock_turtle.h"
+#include "painter.h"
+
+#include <gmock/gmock.h>
+#include <gtest/gtest.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));
+}
+
+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..2f2244f
--- /dev/null
@@ -0,0 +1,17 @@
+TARGET = ut_gmocktest
+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
+
+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