gpt4 book ai didi

c++ - 使用带有字符串而不是宽字符串的 Visual Studio native C++ 单元测试框架

转载 作者:行者123 更新时间:2023-12-05 06:07:28 25 4
gpt4 key购买 nike

有没有办法配置 Visual Studio native C++ 单元测试框架以使用 std::string 而不是 std::wstrings

Assert::Equal<class T>(const T & t1,const T &t2)

需要函数

template<class T> std::wstring ToSring<class T>(const T & t) /* note the wstring here */

由测试编写者为要测试的对象(类型 T)编写/专门化。我已经有这个功能了:

ostream & operator<<(ostream & o, const T & t) /* note the ostream vs wostream */

我想重新使用(建立第三方窄字符串库),但我没有 wostream 等效项,也不想重写一个。

我有哪些选择?

最佳答案

如果您的类确实wostream方法,那么您对ToString的特化可以简单地使用提供的宏RETURN_WIDE_STRING:

template<> static std::wstring ToString(const Foo &t) { RETURN_WIDE_STRING(t); }

但在不更改被测代码的情况下,您可以编写类似的宏(或函数)将 ostream 转换为 wstring 并以相同的方式使用它:

template<> static std::wstring ToString(const Foo &t) { RETURN_WIDE_FROM_NARROW(t); }

新宏可能类似于:

#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();

您还可以通过使用不需要它的 Assert 变体来避免整个 ToString 特化问题:

Assert::IsTrue(t1 == t2, L"Some descriptive fail message");

不过,这可能需要更多或更多的工作,具体取决于您希望在失败消息中包含多少详细信息。

关于c++ - 使用带有字符串而不是宽字符串的 Visual Studio native C++ 单元测试框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65415332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com