2014-12-24 70 views
1

我正在定義我自己的ASSERT宏 - 比如ASSERT_FOO如何定義gtest ASSERT_ASSERT宏

它非常複雜,它根據宏定義等來改變它的行爲。我想創建測試來測試它。我需要像ASSERT_ASSERT(ASSERT_FOO(foo()));

爲了達到這個目的,我需要類似測試重置來消除積累的斷言並且測試成功。

if (::testing::Test::HasFatalFailure()) { 
    // reset the test ? 
} else { 
    FAIL(); 
} 

我該如何存檔?

回答

0
// 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; 
}