From: Aki Koskinen Date: Sat, 20 Mar 2010 09:45:58 +0000 (+0200) Subject: GMock unit test tryout X-Git-Url: http://git.maemo.org/git/?p=ptas;a=commitdiff_plain;h=e78a932997fd0d4701a419d248238cb0588ccf6e GMock unit test tryout --- diff --git a/tests/ut_gmocktest/.gitignore b/tests/ut_gmocktest/.gitignore new file mode 100644 index 0000000..b74f358 --- /dev/null +++ b/tests/ut_gmocktest/.gitignore @@ -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 index 0000000..aea8904 --- /dev/null +++ b/tests/ut_gmocktest/mock_turtle.h @@ -0,0 +1,18 @@ +#ifndef MOCK_TURTLE_H +#define MOCK_TURTLE_H + +#include "turtle.h" +#include + +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 index 0000000..c5facf7 --- /dev/null +++ b/tests/ut_gmocktest/painter.cpp @@ -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 index 0000000..00a8129 --- /dev/null +++ b/tests/ut_gmocktest/painter.h @@ -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 index 0000000..416b7cd --- /dev/null +++ b/tests/ut_gmocktest/turtle.h @@ -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 index 0000000..5e416cb --- /dev/null +++ b/tests/ut_gmocktest/ut_gmocktest.cpp @@ -0,0 +1,24 @@ +#include "mock_turtle.h" +#include "painter.h" + +#include +#include + +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 index 0000000..2f2244f --- /dev/null +++ b/tests/ut_gmocktest/ut_gmocktest.pro @@ -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