google test创建测试工程的资料请参考其它文章。
注意:cocos2d-x的win32工程默认是Multithreaded-debug DLL,所以修改gtest工程为这个(工程->属性->C/C++->Code Generation->Runtime Library),编译的lib给cocos2d-x就正常链接了。
其它按照gtest测试工程添加头,库和变量_VARIADIC_MAX=10。
然后修改win32下的main.cpp(我加入了一个#if是为了方便切换到测试模式。)
#include "main.h"
#include "AppDelegate.h"
#include "cocos2d.h"
#include <shellapi.h>
#include <gtest\gtest.h>
int Foo(int a, int b){
if (a == 0 || b == 0) {
throw "don't do that";
}
int c = a % b;
if (c == 0){
return b; }
return Foo(b, c);
}
TEST(FooTest, HandleNoneZeroInput){
EXPECT_EQ(2, Foo(4, 10));
EXPECT_EQ(6, Foo(30, 18));
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
#if 0
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
return Application::getInstance()->run();
#else
AllocConsole(); //创建一个控制台
freopen("CONIN$","r",stdin); //将标准输入输出流定位到这个控制台
freopen("CONOUT$","w",stdout);
freopen("CONOUT$","w",stderr);
LPWSTR *szArgList;
int argCount;
szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
testing::InitGoogleTest(&argCount, szArgList );
RUN_ALL_TESTS();
system("pause");
FreeConsole();
return 0; #endif
}