[英]Using Visual Studio native C++ unit testing framework with strings instead of wide strings
Is there a way to configure Visual Studio native C++ unit testing framework to work with std::string instead of std::wstrings ?有没有办法配置 Visual Studio 本机 C++ 单元测试框架以使用std::string而不是std::wstrings ?
Assert::Equal<class T>(const T & t1,const T &t2)
requires the function需要 function
template<class T> std::wstring ToSring<class T>(const T & t) /* note the wstring here */
to be written/specialized by the test writer for the object to be tested (of type T).由测试编写者编写/专门用于要测试的 object(T 型)。 I already have this function:
我已经有了这个 function:
ostream & operator<<(ostream & o, const T & t) /* note the ostream vs wostream */
I would like to re-use (build uppon a third party narrow strings library), but I don't have the wostream equivalent, and don't want to rewrite one.我想重新使用(建立第三方窄字符串库),但我没有 wostream 等价物,也不想重写一个。
What are my options?我有哪些选择?
If your class did have the wostream
method then your specialization for ToString
could simply use the provided macro RETURN_WIDE_STRING
:如果您的 class确实具有
wostream
方法,那么您对ToString
的专业化可以简单地使用提供的宏RETURN_WIDE_STRING
:
template<> static std::wstring ToString(const Foo &t) { RETURN_WIDE_STRING(t); }
But without changing the code under test, you could write a similar macro (or function) to convert the ostream
to a wstring
and use it in the same way:但是在不更改被测代码的情况下,您可以编写一个类似的宏(或函数)来将
ostream
转换为wstring
并以相同的方式使用它:
template<> static std::wstring ToString(const Foo &t) { RETURN_WIDE_FROM_NARROW(t); }
The new macro might look something like:新宏可能类似于:
#define RETURN_WIDE_FROM_NARROW(inputValue) \
std::stringstream ss;\
ss << inputValue;\
auto str = ss.str();\
std::wstringstream wss;\
wss << std::wstring(str.begin(), str.end());\
return wss.str();
You can also avoid the whole ToString
specialization problem by using Assert
variants that don't require it:您还可以通过使用不需要的
Assert
变体来避免整个ToString
化问题:
Assert::IsTrue(t1 == t2, L"Some descriptive fail message");
This is likely to be as much or more work though, depending how much detail you want in the fail messages.不过,这可能需要做更多或更多的工作,具体取决于您希望在失败消息中包含多少细节。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.