Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

at_unit_test.h

Go to the documentation of this file.
00001 //
00002 // The Austria library is copyright (c) Gianni Mariani 2004.
00003 // 
00004 // Grant Of License.  Grants to LICENSEE the non-exclusive right to use the Austria
00005 // library subject to the terms of the LGPL.
00006 // 
00007 // A copy of the license is available in this directory or one may be found at this URL:
00008 //  http://www.gnu.org/copyleft/lesser.txt
00009 // 
00015 #ifndef x_at_unit_test_h_x
00016 #define x_at_unit_test_h_x 1
00017 
00018 #include "at_exports.h"
00019 #include "at_factory.h"
00020 #include "at_types.h"
00021 #include "at_source_locator.h"
00022 #include "at_start_up.h"
00023 
00024 // Austria namespace
00025 namespace at
00026 {
00027 
00028 // ======== UnitTestTraits =========================================
00034 class AUSTRIA_EXPORT UnitTestTraits
00035 {
00036     public:
00037 
00042     enum t_TestResult
00043     {
00049         DefaultResult,
00050 
00055         NotTested,
00056 
00061         TestSuccess,
00062 
00068         TestConstructorFailure,
00069 
00074         TestRunFailure,
00075 
00081         TestDestructorFailure
00082 
00083     };
00084 
00085 };
00086 
00087 }; // namespace
00088 
00089 
00090 namespace std {
00091 
00092 // ======== basic_ostream<i_char_type, i_traits>& operator << =========
00101 template<
00102     typename        i_char_type,
00103     class           i_traits
00104 >                   
00105 basic_ostream<i_char_type, i_traits>& operator << (
00106     basic_ostream<i_char_type, i_traits>            & i_ostream,
00107     at::UnitTestTraits::t_TestResult                & i_value
00108 ) {
00109 
00110     switch ( i_value )
00111     {
00112         
00113         case at::UnitTestTraits::DefaultResult : {
00114             i_ostream << "No Result";
00115             break;
00116         }
00117 
00118         case at::UnitTestTraits::NotTested : {
00119             i_ostream << "Not tested";
00120             break;
00121         }
00122 
00123         case at::UnitTestTraits::TestSuccess : {
00124             i_ostream << "Success";
00125             break;
00126         }
00127 
00128         case at::UnitTestTraits::TestConstructorFailure : {
00129             i_ostream << "Test Constructor Failed";
00130             break;
00131         }
00132 
00133         case at::UnitTestTraits::TestRunFailure : {
00134             i_ostream << "Test Run Failed";
00135             break;
00136         }
00137 
00138         case at::UnitTestTraits::TestDestructorFailure : {
00139             i_ostream << "Test Destructor Failed";
00140             break;
00141         }
00142 
00143         default : {
00144             i_ostream << "INVALID TEST CODE";
00145             break;
00146         }
00147     }
00148 
00149     return i_ostream;
00150     
00151 } // end basic_ostream<i_char_type, i_traits>& operator <<
00152 
00153 } // namespace std
00154 
00155 // Austria namespace
00156 namespace at
00157 {
00158 
00159 // ======== TestCase_Exception =====================================
00165 class AUSTRIA_EXPORT TestCase_Exception
00166 {
00167     public:
00168 
00169     TestCase_Exception(
00170         const AT_String                             & i_description,
00171         const char                                  * i_filename,
00172         int                                           i_lineno
00173     )
00174       : m_description( i_description ),
00175         m_locator( i_filename, i_lineno )
00176     {
00177     }
00178 
00179     TestCase_Exception()
00180       : m_locator( 0, 0 )
00181     {
00182     }
00183 
00188     AT_String                                         m_description;
00189 
00194     SourceLocator_Basic                           m_locator;
00195     
00196 };
00197 
00198 
00199 
00200 // ======== AT_TCAssert ===============================================
00206 #define AT_TCAssert( x_expr, x_description )                            \
00207     if ( !( x_expr ) ) {                                                \
00208         throw TestCase_Exception(                                       \
00209             AT_String( x_description ),                                 \
00210             __FILE__,                                                   \
00211             __LINE__                                                    \
00212         );                                                              \
00213     }                                                                   \
00214 // end of macro
00215 
00216 }; // namespace
00217 
00218 namespace std {
00219 
00220 // ======== basic_ostream<i_char_type, i_traits>& operator << =========
00229 template<
00230     typename        i_char_type,
00231     class           i_traits
00232 >                   
00233 ::std::basic_ostream<i_char_type, i_traits>& operator << (
00234     ::std::basic_ostream<i_char_type, i_traits>         & i_ostream,
00235     const at::TestCase_Exception                        & i_value
00236 ) {
00237 
00238     i_ostream << "Test Case Exception : " << i_value.m_locator << " - " << i_value.m_description;
00239 
00240     return i_ostream;
00241     
00242 } // end basic_ostream<i_char_type, i_traits>& operator <<
00243 
00244 } // namespace std
00245 
00246 namespace at
00247 {
00248 
00249 // ======== UnitTestArea_Basic =====================================
00255 class AUSTRIA_EXPORT UnitTestArea_Basic
00256 {
00257     public:
00258 
00259     UnitTestArea_Basic(
00260         const char                                  * i_name,
00261         const char                                  * i_description,
00262         const char                                  * i_filename,
00263         int                                           i_lineno
00264     )
00265       : m_name( i_name ),
00266         m_description( i_description ),
00267         m_locator( i_filename, i_lineno )
00268     {
00269     }
00270 
00271 
00272     //
00273     // member variables.
00274     //
00275 
00281     const char                                      * m_name;
00282 
00287     const char                                      * m_description;
00288 
00293     SourceLocator_Basic                           m_locator;
00294 };
00295 
00296 }; // namespace
00297 
00298 namespace std {
00299 
00300 // ======== basic_ostream<i_char_type, i_traits>& operator << =========
00309 template<
00310     typename        i_char_type,
00311     class           i_traits
00312 >                   
00313 ::std::basic_ostream<i_char_type, i_traits>& operator << (
00314     ::std::basic_ostream<i_char_type, i_traits>         & i_ostream,
00315     const at::UnitTestArea_Basic                        & i_value
00316 ) {
00317     return i_ostream << i_value.m_name << " - " << i_value.m_locator << " - " << i_value.m_description;
00318     
00319 } // end basic_ostream<i_char_type, i_traits>& operator <<
00320 
00321 } // namespace std
00322 
00323 namespace at
00324 {
00325 
00326 // ======== UnitTestKey_Basic ======================================
00332 class AUSTRIA_EXPORT UnitTestKey_Basic
00333 {
00334     public:
00335 
00336     UnitTestKey_Basic(
00337         const UnitTestArea_Basic                    * i_area,
00338         const char                                  * i_name,
00339         const char                                  * i_description,
00340         const char                                  * i_filename,
00341         int                                           i_lineno,
00342         UnitTestTraits::t_TestResult                  i_expected_result = UnitTestTraits::TestSuccess,
00343         int                                           i_level = m_level_default
00344     )
00345       : m_area( i_area ),
00346         m_name( i_name ),
00347         m_description( i_description ),
00348         m_locator( i_filename, i_lineno ),
00349         m_expected_result( i_expected_result ),
00350         m_level( i_level )
00351     {
00352     }
00353     
00354     bool operator<( UnitTestKey_Basic const & i_value ) const;
00355     //
00356     // member variables.
00357     //
00358 
00363     const UnitTestArea_Basic                        * m_area;
00364 
00370     const char                                      * m_name;
00371 
00376     const char                                      * m_description;
00377 
00382     SourceLocator_Basic                           m_locator;
00383 
00390     UnitTestTraits::t_TestResult                  m_expected_result;
00391 
00398     int                                           m_level;   
00399 
00403     static const unsigned                         m_level_default = 10;
00404 };
00405 
00406 }; // namespace
00407 
00408 namespace std {
00409 
00410 // ======== basic_ostream<i_char_type, i_traits>& operator << =========
00419 template<
00420     typename        i_char_type,
00421     class           i_traits
00422 >                   
00423 ::std::basic_ostream<i_char_type, i_traits>& operator << (
00424     ::std::basic_ostream<i_char_type, i_traits>         & i_ostream,
00425     const ::at::UnitTestKey_Basic                   & i_value
00426 ) {
00427     return i_ostream << i_value.m_name << " - " << i_value.m_locator;
00428     
00429 } // end basic_ostream<i_char_type, i_traits>& operator <<
00430 
00431 } // namespace std
00432 
00433 
00434 namespace at
00435 {
00436 
00437 
00438 // ======== UnitTestContext ===========================================
00444 class AUSTRIA_EXPORT UnitTestContext
00445 {
00446     public:
00447 
00448 
00449     // ======== GetValue ==============================================
00458     virtual const std::string * GetValue(
00459         const std::string       & i_name
00460     ) const = 0;
00461 
00462 };
00463 
00464 
00465 // ======== UnitTest ===============================================
00474 class AUSTRIA_EXPORT UnitTest
00475 {
00476     public: 
00477 
00478     UnitTest( const UnitTestContext * i_context = 0 )
00479       : m_expected_result( UnitTestTraits::DefaultResult ),
00480         m_context( i_context )
00481     {
00482     }
00483 
00484     // ======== UnitTest ===========================================
00488     virtual ~UnitTest(  ) {}
00489 
00490 
00491     // ======== Run ===================================================
00497     virtual void Run() = 0;
00498 
00499 
00500     // ======== Locator ===============================================
00506     virtual UnitTestKey_Basic * Locator() = 0;
00507 
00508 
00509     // ======== GetArg ================================================
00519     virtual const std::string & GetArg(
00520         const std::string       & i_name,
00521         const std::string       & i_default
00522     ) {
00523         if ( m_context )
00524         {
00525             const std::string * l_val = m_context->GetValue( i_name );
00526 
00527             if ( l_val )
00528             {
00529                 return * l_val;
00530             }
00531         }
00532         return i_default;
00533     }
00534 
00535     
00536 
00543     UnitTestTraits::t_TestResult                        m_expected_result;
00544 
00548     const UnitTestContext                             * m_context;
00549     
00550 };
00551 
00552 
00553 
00554 // ======== AT_TestArea ===============================================
00560 #define AT_TestArea( x_area, x_description )                            \
00561     static UnitTestArea_Basic   x_g_TestArea_ ## x_area(                \
00562         # x_area, x_description, __FILE__, __LINE__                     \
00563     )                                                                   \
00564 // end of macro AT_TestArea
00565 
00566 
00567 // ======== AT_DefineTest =============================================
00573 #define AT_DefineTest_( x_name, x_area, x_description, x_expect, x_level ) \
00574                                                                          \
00575     UnitTestKey_Basic   __g_TestKey_ ## x_area ## _ ## x_name(           \
00576         & x_g_TestArea_ ## x_area,                                       \
00577         # x_name,                                                        \
00578         x_description,                                                   \
00579         __FILE__,                                                        \
00580         __LINE__,                                                        \
00581         x_expect,                                                        \
00582         x_level                                                          \
00583     );                                                                   \
00584                                                                          \
00585     class __g_TestLocatorClass_ ## x_area ## _ ## x_name                 \
00586       : public UnitTest                                                  \
00587     {                                                                    \
00588         public:                                                          \
00589                                                                          \
00590         UnitTestKey_Basic * Locator()                                    \
00591         {                                                                \
00592             return & __g_TestKey_ ## x_area ## _ ## x_name;              \
00593         }                                                                \
00594     };                                                                   \
00595                                                                          \
00596                                                                          \
00597     class __g_TestClass_ ## x_area ## _ ## x_name                        \
00598       : public __g_TestLocatorClass_ ## x_area ## _ ## x_name            \
00599 // end of macro
00600 
00601 #define AT_DefineTestExpect( x_name, x_area, x_description, x_expect )  \
00602     AT_DefineTest_(                                                     \
00603         x_name,                                                         \
00604         x_area,                                                         \
00605         x_description,                                                  \
00606         x_expect,                                                       \
00607         UnitTestKey_Basic::m_level_default                              \
00608     )                                                                   \
00609 // end of macro
00610 
00611 #define AT_DefineTestLevel( x_name, x_area, x_description, x_expect, x_level )  \
00612     AT_DefineTest_(                                                     \
00613         x_name,                                                         \
00614         x_area,                                                         \
00615         x_description,                                                  \
00616         x_expect,                                                       \
00617         UnitTestKey_Basic::m_level_default + (x_level)                  \
00618     )                                                                   \
00619 // end of macro
00620 
00621 #define AT_DefineTest( x_name, x_area, x_description )                  \
00622     AT_DefineTest_(                                                     \
00623         x_name,                                                         \
00624         x_area,                                                         \
00625         x_description,                                                  \
00626         UnitTestTraits::TestSuccess,                                    \
00627         UnitTestKey_Basic::m_level_default                              \
00628     )                                                                   \
00629 // end of macro 
00630 
00631 
00632 // ======== AT_RegisterTest ===========================================
00638 #define AT_RegisterTest( x_name, x_area )                               \
00639                                                                         \
00640     FactoryImpl0P<                                                      \
00641         __g_TestClass_ ## x_area ## _ ## x_name,                        \
00642         UnitTest,                                                       \
00643         UnitTestKey_Basic                                               \
00644     >   __g_TestFactory_ ## x_area ## _ ## x_name(                      \
00645         __g_TestKey_ ## x_area ## _ ## x_name,                          \
00646         __FILE__,                                                       \
00647         __LINE__,                                                       \
00648         "Unit test " # x_area " " # x_name                              \
00649     )                                                                   \
00650 // end of macro
00651 
00652 
00653 // ======== UnitTestAssertModifier ====================================
00660 class UnitTestAssertModifier;
00661 
00662 // ======== UnitTestExec ===========================================
00672 extern AUSTRIA_EXPORT int UnitTestExec(
00673     int             i_argc,
00674     const char   ** i_argv
00675 );
00676 
00677 
00678 // ======== AT_MAINTEST ===============================================
00685 #define AT_MAINTEST int main( int argc, const char ** argv )            \
00686 {                                                                       \
00687     MainInitializer     l_initializer( argc, argv );                    \
00688     return UnitTestExec( argc, argv );                                  \
00689 }                                                                       \
00690 
00691 }; // namespace
00692 
00693 #endif // x_at_unit_test_h_x
00694 
00695 

Generated for Austria by doxygen and MakeXS at Sun Oct 24 17:35:34 PDT 2004