1 /* _________________________________________________________________________ 2 * 3 * UTILIB: A utility library for developing portable C++ codes. 4 * Copyright (c) 2008 Sandia Corporation. 5 * This software is distributed under the BSD License. 6 * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 7 * the U.S. Government retains certain rights in this software. 8 * For more information, see the README file in the top UTILIB directory. 9 * _________________________________________________________________________ 10 */ 11 12 #ifndef TEST_UNIT_TESTUTILS_h 13 #define TEST_UNIT_TESTUTILS_h 14 15 #include <iostream> 16 17 #include <cxxtest/TestSuite.h> 18 #include <cxxtest/ValueTraits.h> 19 #include <cxxtest/StdValueTraits.h> 20 21 #define TEST_WHAT_LIMIT(EXCEPTION, STRING, LIMIT) \ 22 if ( true ) { \ 23 std::string msg(EXCEPTION.what()); \ 24 std::string test(STRING); \ 25 \ 26 /* remove the "File: line#: " */ \ 27 size_t pos = msg.find(":"); \ 28 if ( pos != std::string::npos ) \ 29 pos = msg.find(":", pos+1); \ 30 if ( pos == std::string::npos ) \ 31 pos = 0; \ 32 else \ 33 pos += 2; \ 34 \ 35 size_t end = 0; \ 36 if ( LIMIT ) \ 37 end = test.size(); \ 38 else \ 39 { \ 40 /* strip trailing whitespace */ \ 41 end = msg.find_last_not_of(" \n\t"); \ 42 if ( end < pos ) \ 43 end = std::string::npos; \ 44 else \ 45 end = end - pos + 1; \ 46 } \ 47 \ 48 TS_ASSERT_EQUALS(test, msg.substr(pos,end)); \ 49 } else static_cast<void>(0) 50 51 #define TEST_WHAT(EXCEPTION, STRING) TEST_WHAT_LIMIT(EXCEPTION, STRING, true) 52 53 #define TEST_WHAT_CONTAINS(EXCEPTION, STRING) \ 54 if ( true ) { \ 55 std::string msg(EXCEPTION.what()); \ 56 std::string test(STRING); \ 57 if ( msg.find(test) == std::string::npos ) \ 58 TS_FAIL("\"" + test + "\" not found in " + msg); \ 59 } else static_cast<void>(0) 60 61 #define TEST_WHAT_ENDSWITH(EXCEPTION, STRING) \ 62 if ( true ) { \ 63 std::string msg(EXCEPTION.what()); \ 64 std::string test(STRING); \ 65 size_t end = msg.rfind("\nStack trace"); \ 66 /* strip trailing whitespace */ \ 67 end = msg.find_last_not_of(" \n\t", end); \ 68 size_t pos = end <= test.size() ? 0 : 1 + end - test.size(); \ 69 TS_ASSERT_EQUALS(msg.substr(pos, test.size()), test); \ 70 } else static_cast<void>(0) 71 72 73 #endif // define TEST_UNIT_TESTUTILS_h 74