#define BOOST_TEST_MODULE f_to_c_test_suite #include <boost/test/unit_test.hpp> #include <boost/test/included/unit_test.hpp> // new to catch output #include <boost/test/tools/output_test_stream.hpp> #include "FtoC.h" // so we can use the elements of the output_test_stream without // a horrid list of namespaces using boost::test_tools::output_test_stream; using namespace std;
output_test_stream output; streambuf * old = cout.rdbuf(output.rdbuf()); // force the error FtoC(-500); // restore cout. cout.rdbuf(old);
BOOST_AUTO_TEST_CASE(Front_Error_Test) { output_test_stream output; streambuf * old = cout.rdbuf(output.rdbuf()); string goodMessage = "Illegal Temperature in F.\n"; string goodMessage = "illegal temp\n"; FtoC(-500); cout.rdbuf(old); BOOST_TEST(not output.is_empty(false)); BOOST_TEST(output.check_length(goodMessage.size(),false)); BOOST_TEST(output.check_length(badMessage.size(),false), "Expect fail"); BOOST_TEST(output.is_equal(goodMessage,false)); BOOST_TEST(output.is_equal(badMessage,false),"Expect Fail"); // clean the output stream for reuse. // not needed here, but ... output.flush(); return; }
call
to throw an exception of exception_type
// these will warn if the exception does not occur BOOST_CHECK_THROW(CtoF(-400),exception); BOOST_CHECK_THROW(CtoF(-400),invalid_argument); // we expect an exception, but one does not occur cout << "Here is an error" << endl; BOOST_CHECK_THROW(CtoF(400),exception); cout << "After error " << endl; cout << endl; // these will bomb if the exception does not occur. BOOST_REQUIRE_THROW(CtoF(-400),exception); BOOST_REQUIRE_THROW(CtoF(-400),invalid_argument); // require a throw but it does not happen. cout << "Here is an error, we required a throw but it did not" << endl; BOOST_REQUIRE_THROW(CtoF(400),invalid_argument); cout << "Did not get here." << endl;
BOOST_AUTO_TEST_CASE(no_throw_test) { cout << "We made it to the no_throw case." << endl; BOOST_CHECK_NO_THROW(CtoF(100)); cout << "This should complain " << endl; BOOST_CHECK_NO_THROW(CtoF(-2100)); cout << "Done with the no_throw case." <<endl; }