VC2008でCppUTest

http://cpputest.github.io/ からzipをダウンロード。

Releaseバージョンは3.4


解凍して、CppUTest_VS2008.slnを開く。


AllTestsとCppUTestというプロジェクトがあるので、CppUTestをビルドすると、CppUTest.libができる。これと、includeフォルダを自分のプロジェクトフォルダに丸々コピーした。(いや、パスが通っていればいいだけだとは思うが…)

このCppUTest.libを使うと、「関数定義が被ってるぜ!」ってエラーが出るので、とりあえずCPPUTEST_USE_STD_CPP_LIB=0を定義してビルドしなおした。

再定義エラーはでなくなったので試してみる。

#include "CppUtest/CommandLineTestRunner.h"

int _tmain(int argc, _TCHAR* argv[])
{
	return CommandLineTestRunner::RunAllTests(argc, argv);
}

この状態で、[Unicode文字セットを使用する]でビルドすると、エラーが出た。wcharバージョンはないらしい。

もしかして、文字列比較(STRCMP_EQUAL)とかもwchar版は無いのかな…?よくは調べなかったがとりあえず不便なので以下のマクロで対処。

std::string widechar_to_multibyte(const wchar_t * wide)
{
	std::string multi_byte;
	int size = (wcslen(wide) + 1) * 2;

	char * buf = new char[size];
	if (buf) {
		int ret = ::WideCharToMultiByte(CP_ACP, 0,
			wide, -1, buf, size, NULL, NULL)
		if (ret > 0) {
			multi_byte = buf;
		}
		delete [] buf;
	}
	return multi_byte;
}

#define WSTRCMP_EQUAL(expected, actual) STRCMP_EQUAL(\
	widechar_to_multibyte(expected).c_str(),\
	widechar_to_multibyte(actual).c_str())

これで、

TEST_GROUP(MyTestGroup)
{
};

TEST(MyTestGroup, wide_char_comp)
{
	std::wstring a = L"a", b = L"b";
	WSTRCMP_EQUAL(L"ab", (a + b).c_str());
}

うん、良さそう。