LoginSignup
1
1

More than 5 years have passed since last update.

[VisualStudio2015]VC++ のユニットテストの結果が文字化けする原因

Posted at

このテストは失敗する。

TEST_METHOD(BuggyAssertion)
{
    Assert::AreEqual("あいうえお", "かきくけこ");
}

問題なのは、実行結果。こうなる。

assertion.png

原因

以下にインストールされているヘッダー内に、expectとactualをstd::wstring型にコンバートするtemplateが実装されている。

  • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\UnitTest\include
CppUnitTestAssert.h
// std::string → std::wstring 版
template<> inline std::wstring ToString<std::string>           (const std::string& t)           { RETURN_WIDE_STRING(t.c_str()) ; }

// char* → std::wstring 版
template<> inline std::wstring ToString<char>(char* t) { if (NULL == t) return std::wstring(L"(NULL)"); RETURN_WIDE_STRING(t); }

共通で使用されるRETURN_WIDE_STRINGマクロはこうなっている。

#define RETURN_WIDE_STRING(inputValue)      std::wstringstream _s;_s << inputValue; return _s.str()

見やすくしてみる。

std::wstring RETURN_WIDE_STRING(/* std::string or char* */ inputValue)
{
    std::wstringstream _s;
    _s << inputValue;
    return _s.str();
}

std::wstringstreamストリームにただ流し込んでいるだけだった。
スマートに解決する方法が思いつかない。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1