Cygwinä¸ã®g++ã§Google Mockãåãã
"å®è·µãã¹ãé§åéçº"è¶ è¯ãã§ããã
ã¨ãããã¨ã§ãC++ã§ä½¿ããåä½ãã¹ããã¬ã¼ã ã¯ã¼ã¯ã§ããGoogle testã¨ã¢ãã¯ãã¬ã¼ã ã¯ã¼ã¯ã§ããGoogle Mockã使ããç°å¢ãæ´ãã¾ãããã¨ãã話ã«ãªãã¾ããã
Cygwinä¸ã§åãããããã«ãªãã°ããã¨ã¯ã©ãã§ãåãããã ããã¨æã£ãã®ã§Cygwinä¸ã§åãããããã«ãã¦ã¿ã¾ããã
ã§ã¯æ©éã
Google Mockã®ãã¦ã³ãã¼ã
ã¨ããããä¿åç¨ã®ãã£ã¬ã¯ããªãä½ã£ã¦ãããã«ãã¦ã³ãã¼ããã¦ã解åãã¾ãããã
$ mkdir gmock $ cd gmock $ wget http://googlemock.googlecode.com/files/gmock-1.6.0.zip $ unzip gmock-1.6.0.zip
ç¡çãã使ããããã«ãã
ãã¾ãããã°ãã¾ã使ããã®ããããã¾ãããã¨ãããã
http://d.hatena.ne.jp/E_Mattsan/20120215/1329311774
ãã®æ¹ãåèã«ããã¦ãããã¾ãã
$ cd gmock-1.6.0/fused-src $ g++ -I. -I./gmock -I./gtest -c gmock_main.cc $ g++ -I. -I./gmock -I./gtest -c gmock-gtest-all.cc
ããã§
gmock_main.o gmock-gtest-all.o
ã®äºã¤ã®ãã¡ã¤ã«ãã§ãã¾ããfused-srcä¸ã®ããããã¡ã¤ã«(fused-src/gtest/gtest.hã¨fused-src/gmock/gmock.h)ãã¤ã³ã¯ã«ã¼ãããæçµçã«ãããã®ãªãã¸ã§ã¯ããã¡ã¤ã«ããªã³ã¯ãã¦ããã°è¯ããã¨ãããã¨ã«ãªãã¾ãã
å®éã«ä½¿ã£ã¦ã¿ãã
~/src/gmock_test/ãã©ã«ãä¸ã«ãµã³ãã«ããã°ã©ã ãä½æãã¦å®è¡ãã¦ã¿ããã¨æãã¾ãã
$ cp gmock_main.o ~/src/gmock_test/ $ cp gmock-gtest-all.o ~/src/gmock_test/ $ cp -R gmock/ ~/src/gmock_test/ $ cp -R gtest/ ~/src/gmock_test/
Google Mockè¶
å
¥éç·¨
http://opencv.jp/googlemockdocs/fordummies.html
ã®Turtleã®ãã¤ãåããã¦ã¿ã¾ãã
ã¨ãããã以ä¸ã®ãã¡ã¤ã«ãã°ãããä½ã£ã¦ããã¾ãã
Painter.h
#ifndef _PAINTER_H #define _PAINTER_H #include "Turtle.h" class Painter { public: Painter(Turtle* turtle) : turtle_(turtle){} ~Painter(){} bool DrawCircle(int x, int y, int r); private: Turtle* turtle_; }; #endif // _PAINTER_H
Painter.cpp
#include "Painter.h" bool Painter::DrawCircle(int x, int y, int r) { turtle_->PenDown(); return true; }
Turtle.h
#ifndef _TURTLE_H #define _TURTLE_H class Turtle { public: Turtle(){} virtual ~Turtle() {} virtual void PenUp() = 0; virtual void PenDown() = 0; }; #endif // _TURTLE_H
MockTurtle.h
#ifndef _MOCK_TURTLE_H #define _MOCK_TURTLE_H #include "gmock/gmock.h" #include "Turtle.h" class MockTurtle : public Turtle { public: MOCK_METHOD0(PenUp, void()); MOCK_METHOD0(PenDown, void()); }; #endif // _MOCK_TURTLE_H
PainterTest.cpp
#include "MockTurtle.h" #include "Painter.h" #include "gmock/gmock.h" #include "gtest/gtest.h" using ::testing::AtLeast; TEST(PainterTest, CanDrawSomething) { MockTurtle turtle; EXPECT_CALL(turtle, PenDown()) .Times(AtLeast(1)); Painter painter(&turtle); EXPECT_TRUE(painter.DrawCircle(0, 0, 10)); }
ã²ã¼
ããã§
$ g++ Painter.cpp PainterTest.cpp gmock_main.o gmock-gtest-all.o -I.
ã£ã¦ããã¨ãã«ãã§ãããã
$ ./a.exe
ã¨ãããã¨ã¡ããã¨åããã®ããã£ã¨ç¢ºèªã§ããã¨æããï¼
æå¾ã®ã»ããã¹ããæ¸ãã¦å®è£ ãã¦ã¨ãã¡ããã¨ããããã£ããã¡ããã¨Makefileã¨ãæ¸ãããã£ããã©ããç ããï¼
å¯ããï¼
ãããã¿ãªããï¼