首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何定义gtest ASSERT_ASSERT宏

如何定义gtest ASSERT_ASSERT宏
EN

Stack Overflow用户
提问于 2014-12-25 02:07:52
回答 1查看 199关注 0票数 1

我正在定义我自己的ASSERT宏--比如说ASSERT_FOO

它非常复杂,它根据宏定义等改变其行为。我想创建测试来测试它。我需要像ASSERT_ASSERT(ASSERT_FOO(foo()))这样的东西;

为了实现这一点,我需要像测试重置这样的东西,它可以消除累积的断言,并使测试成功。

代码语言:javascript
复制
if (::testing::Test::HasFatalFailure()) {
    // reset the test ?
} else {
    FAIL();
}

我怎样才能做到这一点呢?

EN

回答 1

Stack Overflow用户

发布于 2014-12-26 19:43:56

代码语言:javascript
复制
// Substitute non-verbose assert() macro
// http://stackoverflow.com/questions/27640730/
#ifndef assert
    #include <cstdlib> // abort()
    static int assert_count     =  0; // count assertion failures
    const unsigned ASSERT_COUNT = 15; // when to stop assertion executions
    /** (cond ? () : cout << "cond" ) */
    #define assert(cond)    \
        do {                \
            if (!(cond)) {  \
                std::cout << ":" << __LINE__ << " fail-> " #cond "\n";  \
                assert_count++;                                 \
                if ( ASSERT_COUNT==assert_count ) { abort(); }  \
            }                                                   \
        } while (false)
#endif

/** "C:/DIR/SubDir/file_name.ext" ==> "file_name.ext" */
const char* remove_dir_name( const char* str ) {
    const char *p = str;     /* p = str+strlen(str); */
    while ( *p != 0 ) { ++p; }
    while ( *p != '/' && *p != '\\' && *p != ':' ) {
        if ( p == str ) { return str; }
        --p;
    }
    return p+1;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27640730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档