SMBIOS Library
outputctl.h
Go to the documentation of this file.
1 // vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:
2 /*
3  * Copyright (C) 2005 Dell Inc.
4  * by Michael Brown <Michael_E_Brown@dell.com>
5  * Licensed under the Open Software License version 2.1
6  *
7  * Alternatively, you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License,
10  * or (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  */
17 
18 
19 #ifndef _OUTPUTCTL_H
20 #define _OUTPUTCTL_H
21 
22 #include "smbios/compat.h"
23 
24 #include <exception>
25 #include <typeinfo> // for typeid()
26 
27 // A collection of macros to use in the unit tests.
28 // They print things out in a standard format, or shorten unit tests.
29 
30 class skip_test : public std::exception
31  { public: virtual ~skip_test() throw() {}; }
32 ;
33 
34 
35 #ifdef LIBSMBIOS_HAS_PRETTY_FUNCTION
36 #define WHEREAMI "\t" << __PRETTY_FUNCTION__ << "... "
37 #else
38 #define WHEREAMI typeid(*this).name() << " (line " << __LINE__ << ")... "
39 #endif
40 
41 #define GET_FLAGS() std::ios::fmtflags old_opts = cout.flags()
42 #define RESTORE_FLAGS() cout.flags(old_opts)
43 
44 #define startTest(arg) do{GET_FLAGS(); cout << arg << WHEREAMI; RESTORE_FLAGS();}while(0)
45 #define passTest(arg) do{GET_FLAGS(); cout << "[ ok ]" << arg << endl; RESTORE_FLAGS();} while(0)
46 #define failTest(arg) do{GET_FLAGS(); cout << "[FAIL]" << arg << endl; RESTORE_FLAGS();} while(0)
47 #define skipTest(arg) do{GET_FLAGS(); cout << "[SKIP]" << arg << endl; RESTORE_FLAGS();} while(0)
48 
49 // standard stuff
50 
51 // Standard test start/end header
52 #ifdef LIBSMBIOS_HAS_PRETTY_FUNCTION
53 #define STD_TEST_START_CHECKSKIP(arg) startTest(arg); bool skip=false; cout << flush; try { checkSkipTest(__FUNCTION__)
54 #define STD_TEST_START(arg) startTest(arg); bool skip=false; cout << flush; try {
55 #else
56 #define STD_TEST_START_CHECKSKIP(arg) startTest(arg); bool skip=false; cout << flush; try {
57 #define STD_TEST_START(arg) startTest(arg); bool skip=false; cout << flush; try {
58 #endif
59 
60 #define STD_TEST_END(arg) \
61  } catch (const skip_test &) { \
62  skip = true; \
63  } catch ( const CppUnit::Exception &e ) { \
64  failTest(arg); \
65  throw; \
66  } catch ( const std::exception &e ) { \
67  failTest(arg); \
68  CPPUNIT_FAIL( e.what() ); \
69  } catch (...) { \
70  failTest(arg); \
71  throw; \
72  } \
73  if( skip ) \
74  skipTest(arg); \
75  else \
76  passTest(arg)
77 
78 // extra macros that make CPPUNIT tests shorter.
79 #define ASSERT_THROWS( expr, exc ) \
80  do { \
81  bool caught = false; \
82  try \
83  { \
84  expr; \
85  } \
86  catch( const exc & ) \
87  { \
88  caught = true; \
89  } \
90  catch( const std::exception &e ) \
91  { \
92  ostringstream ost; \
93  ost << "Executed: " #expr "\nCaught wrong exception. Expected: " #exc; \
94  ost << "\nLine: " << __LINE__; \
95  ost << "\nFile: " << __FILE__; \
96  ost << "\nException Caught: " << typeid(e).name(); \
97  CPPUNIT_FAIL (ost.str().c_str()); \
98  } \
99  catch( ... ) \
100  { \
101  ostringstream ost; \
102  ost << "Executed: " #expr "\nCaught wrong exception. Expected: " #exc; \
103  ost << "\nLine: " << __LINE__; \
104  ost << "\nFile: " << __FILE__; \
105  CPPUNIT_FAIL (ost.str().c_str()); \
106  } \
107  if ( ! caught ) \
108  CPPUNIT_FAIL ("Executed: " #expr "\nShould have thrown an exception, but did not. Expected: " #exc);\
109  } while(0)
110 
111 
112 
113 #endif /* ! defined _OUTPUTCTL_H */