修改TestAssert.h (cppunit)——为什么包含的顺序对宏展开很重要
Modifying TestAssert.h (cppunit) - why the order of includes matter for macro expansion?
我有一个项目,它有两个基异常类;两者都有相同的名称,只是方法/成员不同(一个有返回消息的方法,另一个只有一个可以访问异常信息的字符串成员)。
我使用CPPUNIT进行测试,在框架中有TestAssert.hpp文件,其中包含每个断言的宏(CPPUNIT_ASSERT_NO_THROW等)。我已经修改了,在其中添加了另一个宏来测试测试类中包含的异常头,所以我可以捕获我的基本异常;宏看起来像这样:
#ifdef BASE_EXCEPTION_CLASS_ONE_HPP
#define MY_CPPUNIT
catch (BaseException &ex) {
ex.getMessage();
}
#endif
#ifdef BASE_EXCEPTION_CLASS_TWO_HPP
#define MY_CPPUNIT
catch (BaseException &ex) {
cout << "Caught: " ex.comment <<endl;
}
#endif
这两个宏将在TestAssert.h
中这样使用/** Asserts that the given expression does not throw any exceptions.
* ingroup Assertions
* Example of usage:
* code
* std::vector<int> v;
* v.push_back( 10 );
* CPPUNIT_ASSERT_NO_THROW( v.at( 0 ) );
* endcode
*/
# define CPPUNIT_ASSERT_NO_THROW( expression )
try {
expression;
} catch ( const std::exception &e ) {
CPPUNIT_NS::Message message( "Unexpected exception caught" );
message.addDetail( "Type: " +
CPPUNIT_EXTRACT_EXCEPTION_TYPE_( e,
"std::exception or derived" ) );
message.addDetail( std::string("What: ") + e.what() );
CPPUNIT_NS::Asserter::fail( message,
CPPUNIT_SOURCELINE() ); }
MY_CPPUNIT
catch ( ... ) {
CPPUNIT_NS::Asserter::fail( "Unexpected exception caught",
CPPUNIT_SOURCELINE() );
}
可以,但前提是在测试类中第一个#include是#include "test.hpp"
那么,宏扩展是如何受到包含顺序的影响的,或者我如何才能看到预处理部分,以便我能够以某种方式计算出来?
非常感谢!
您可以使用gcc -E
查看预处理器后的输出。它包括所有展开的宏,并允许您查看宏展开时发生的情况。您还可以在第二步中编译该文件,如果问题最初在其中一个宏中,则可以获得更好的错误报告。
相关文章:
- 具有包含其他对象的类的对象创建顺序
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 为什么更改包含 psapi.h 的顺序会产生编译错误?(标识符 BOOL 未定义)
- 按类成员的顺序对包含类对象的C++向量进行排序
- C++:当顺序真正重要时,如何保证在项目中包含头文件的顺序
- 正确的包含顺序是什么
- 更改AC_CHECK_HEADER中包含项的顺序
- 检查两个 Boost.MPL 序列是否按任何顺序包含相同的类型
- 使用包含每个重复数量的列表生成重复,上升整数的顺序,并用推力
- 指定 gcc 和 g++ 包含和库路径中的顺序
- 按字典顺序从范围中获取下一个数字(不包含所有字符串的容器)
- Qmake-如何配置包含路径顺序
- 当C++模板类中存在循环依赖关系时,修复包含顺序
- 编写一个函数,返回一个堆栈,该堆栈包含所有小于给定数字的元素,并且顺序相同
- 如何按顺序包含页眉
- 正在解析包含顺序
- 修改TestAssert.h (cppunit)——为什么包含的顺序对宏展开很重要
- c++应用程序中包含的头文件的顺序
- 检查字符串是否按顺序包含子字符串的字符序列,但不一定紧挨着
- 我以什么顺序包含头文件?