Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

outputctl.h

Go to the documentation of this file.
00001 // vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:
00002 /*
00003  * Copyright (C) 2005 Dell Inc.
00004  *  by Michael Brown <Michael_E_Brown@dell.com>
00005  * Licensed under the Open Software License version 2.1 
00006  * 
00007  * Alternatively, you can redistribute it and/or modify 
00008  * it under the terms of the GNU General Public License as published 
00009  * by the Free Software Foundation; either version 2 of the License, 
00010  * or (at your option) any later version.
00011 
00012  * This program is distributed in the hope that it will be useful, but 
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of 
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00015  * See the GNU General Public License for more details.
00016  */
00017 
00018 
00019 #ifndef _OUTPUTCTL_H
00020 #define _OUTPUTCTL_H
00021 
00022 #include "smbios/compat.h"
00023 
00024 #include <exception>
00025 #include <typeinfo>     // for typeid()
00026 
00027 // A collection of macros to use in the unit tests.
00028 // They print things out in a standard format, or shorten unit tests.
00029 
00030 class skip_test : public std::exception
00031     { public: virtual ~skip_test() throw() {}; }
00032 ;
00033 
00034 
00035 #ifdef LIBSMBIOS_HAS_PRETTY_FUNCTION
00036 #define WHEREAMI "\t" << __PRETTY_FUNCTION__ << "... "
00037 #else
00038 #define WHEREAMI typeid(*this).name() << " (line " << __LINE__ << ")... "
00039 #endif 
00040 
00041 #define GET_FLAGS() std::ios::fmtflags old_opts = cout.flags()
00042 #define RESTORE_FLAGS() cout.flags(old_opts)
00043 
00044 #define startTest(arg) do{GET_FLAGS(); cout << arg << WHEREAMI;         RESTORE_FLAGS();}while(0)
00045 #define passTest(arg)  do{GET_FLAGS(); cout << "[ ok ]" << arg << endl; RESTORE_FLAGS();} while(0)
00046 #define failTest(arg)  do{GET_FLAGS(); cout << "[FAIL]" << arg << endl; RESTORE_FLAGS();} while(0)
00047 #define skipTest(arg)  do{GET_FLAGS(); cout << "[SKIP]" << arg << endl; RESTORE_FLAGS();} while(0)
00048 
00049 // standard stuff
00050 
00051 // Standard test start/end header
00052 #ifdef LIBSMBIOS_HAS_PRETTY_FUNCTION
00053 #define STD_TEST_START_CHECKSKIP(arg) startTest(arg);  bool skip=false; cout << flush; try { checkSkipTest(__FUNCTION__)
00054 #define STD_TEST_START(arg)           startTest(arg);  bool skip=false; cout << flush; try {
00055 #else
00056 #define STD_TEST_START_CHECKSKIP(arg) startTest(arg);  bool skip=false; cout << flush; try {
00057 #define STD_TEST_START(arg)           startTest(arg);  bool skip=false; cout << flush; try {
00058 #endif
00059 
00060 #define STD_TEST_END(arg)                           \
00061         } catch (const skip_test &) {               \
00062                 skip = true;                        \
00063         } catch ( const CppUnit::Exception &e ) {   \
00064                 failTest(arg);                      \
00065                 throw;                              \
00066         } catch ( const std::exception &e ) {       \
00067                 failTest(arg);                      \
00068                 CPPUNIT_FAIL( e.what() );           \
00069         } catch (...) {         \
00070                 failTest(arg);  \
00071                 throw;          \
00072         }                       \
00073         if( skip )              \
00074             skipTest(arg);      \
00075         else                    \
00076             passTest(arg)
00077 
00078 // extra macros that make CPPUNIT tests shorter.
00079 #define ASSERT_THROWS( expr, exc )  \
00080     do {                        \
00081     bool caught = false;        \
00082     try                         \
00083     {                           \
00084         expr;                   \
00085     }                           \
00086     catch( const exc & )        \
00087     {                           \
00088             caught = true;      \
00089     }                           \
00090     catch( const std::exception &e ) \
00091     {                           \
00092         ostringstream ost;      \
00093         ost << "Executed: " #expr "\nCaught wrong exception. Expected: " #exc;    \
00094         ost << "\nLine: " << __LINE__;   \
00095         ost << "\nFile: " << __FILE__;   \
00096         ost << "\nException Caught: " << typeid(e).name();        \
00097         CPPUNIT_FAIL (ost.str().c_str());   \
00098     }                           \
00099     catch( ... )                \
00100     {                           \
00101         ostringstream ost;      \
00102         ost << "Executed: " #expr "\nCaught wrong exception. Expected: " #exc;    \
00103         ost << "\nLine: " << __LINE__;   \
00104         ost << "\nFile: " << __FILE__;   \
00105         CPPUNIT_FAIL (ost.str().c_str());   \
00106     }                           \
00107     if ( ! caught )             \
00108         CPPUNIT_FAIL ("Executed: " #expr "\nShould have thrown an exception, but did not. Expected: " #exc);\
00109     } while(0)
00110 
00111 
00112 
00113 #endif  /* ! defined _OUTPUTCTL_H */

Generated on Wed Apr 11 16:25:10 2007 for SMBIOS Library by doxygen 1.3.5