简体   繁体   English

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

[英]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.

相关问题 无法在 C/C++ Visual Studio 中使用宽字符串,即使它可以在使用 MinGW 的 CodeBlocks 中使用 - Cannot use wide strings in C/C++ Visual Studio, even though it works in CodeBlocks using MinGW 在 Visual Studio Code 中对本机 C++ 进行单元测试 - Unit Testing Native C++ In Visual Studio Code 需要Visual Studio 2005兼容的C ++单元测试框架 - Need Visual Studio 2005 Compatible C++ Unit Testing Framework 使用Visual Studio测试框架进行C ++单元测试 - C++ unit testing with Visual Studio test framework 如何使用CppUnitTestFramework在Visual Studio本机C ++单元测试中获取$(ProjectDir)路径? - How to get the $(ProjectDir) path in a Visual Studio native C++ Unit Testing using CppUnitTestFramework? 比较两个宽字符串Visual C ++ - compare two wide character strings visual c++ 宽字符串Visual Studio 2005与2017? - Wide strings Visual Studio 2005 vs 2017? Visual Studio 2010的单元测试功能是否可用于本机C ++代码? - Is Visual Studio 2010's unit testing feature usable for native C++ code? 如何将Visual Studio项目从使用宽字符串转换为普通字符串 - How do you convert a Visual Studio project from using wide strings to ordinary strings 在Visual Studio 2012中对C ++控制台应用程序进行单元测试 - Unit testing c++ console application in Visual Studio 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM