unit_base.h

Go to the documentation of this file.
00001 #ifndef UNIT_BASE_GROUP
00002 #define UNIT_BASE_GROUP
00003 
00004 /*
00005 *  Name   : unit_base tools for unit testing
00006 *  Author : Chris Koeritz
00007 **
00008 * Copyright (c) 2009-$now By Author.  This program is free software; you can  *
00009 * redistribute it and/or modify it under the terms of the GNU General Public  *
00010 * License as published by the Free Software Foundation; either version 2 of   *
00011 * the License or (at your option) any later version.  This is online at:      *
00012 *     http://www.fsf.org/copyleft/gpl.html                                    *
00013 * Please send any updates to: fred@gruntose.com                               *
00014 */
00015 
00017 
00021 #include <basis/astring.h>
00022 #include <basis/contracts.h>
00023 #include <basis/enhance_cpp.h>
00024 #include <basis/mutex.h>
00025 #include <structures/string_table.h>
00026 
00027 namespace unit_test {
00028 
00029 // these macros can be used to put more information into the test name.
00030 // using these is preferable to calling the class methods directly, since
00031 // those cannot print out the original version of the formal parameters a
00032 // and b for the test; the macros show the expressions that failed rather
00033 // than just the values.
00034 // note that these require the calling object to be derived from unit_base.
00035 #define UNIT_BASE_THIS_OBJECT (*this)
00036   // the macro for UNIT_BASE_THIS_OBJECT allows tests to use a single unit_base object by
00037   // changing the value of UNIT_BASE_THIS_OBJECT to refer to the proper object's name.
00038 #define ASSERT_EQUAL(a, b, test_name) { \
00039   BASE_FUNCTION(func); \
00040   UNIT_BASE_THIS_OBJECT.assert_equal(a, b, function_name, test_name, basis::astring(#a) + " must be equal to " + #b); \
00041 }
00042 #define ASSERT_INEQUAL(a, b, test_name) { \
00043   BASE_FUNCTION(func); \
00044   UNIT_BASE_THIS_OBJECT.assert_not_equal(a, b, function_name, test_name, basis::astring(#a) + " must be inequal to " + #b); \
00045 }
00046 #define ASSERT_TRUE(a, test_name) { \
00047   BASE_FUNCTION(func); \
00048   UNIT_BASE_THIS_OBJECT.assert_true(a, function_name, test_name, basis::astring(#a) + " must be true"); \
00049 }
00050 #define ASSERT_FALSE(a, test_name) { \
00051   BASE_FUNCTION(func); \
00052   UNIT_BASE_THIS_OBJECT.assert_false(a, function_name, test_name, basis::astring(#a) + " must be false"); \
00053 }
00054 // pointer versions for nicer syntax.
00055 #define ASSERT_NULL(x, y) ASSERT_FALSE(x, y)
00056 #define ASSERT_NON_NULL(x, y) ASSERT_TRUE(x, y)
00057 
00058 class unit_base : public virtual basis::nameable
00059 {
00060 public:
00061   unit_base();
00062   virtual ~unit_base();
00063 
00064   DEFINE_CLASS_NAME("unit_base");
00065 
00066   int total_tests() const;  
00067   int passed_tests() const;  
00068   int failed_tests() const;  
00069 
00070   void assert_equal(const basis::hoople_standard &a, const basis::hoople_standard &b,
00071       const basis::astring &class_name, const basis::astring &test_name,
00072       const basis::astring &diagnostic_info);
00074   void assert_not_equal(const basis::hoople_standard &a, const basis::hoople_standard &b,
00075       const basis::astring &class_name, const basis::astring &test_name,
00076       const basis::astring &diagnostic_info);
00078 
00079   void assert_equal(const basis::byte_array &a, const basis::byte_array &b,
00080       const basis::astring &class_name, const basis::astring &test_name,
00081       const basis::astring &diagnostic_info);
00083   void assert_not_equal(const basis::byte_array &a, const basis::byte_array &b,
00084       const basis::astring &class_name, const basis::astring &test_name,
00085       const basis::astring &diagnostic_info);
00087 
00088   void assert_equal(int a, int b,
00089       const basis::astring &class_name, const basis::astring &test_name,
00090       const basis::astring &diagnostic_info);
00092   void assert_not_equal(int a, int b,
00093       const basis::astring &class_name, const basis::astring &test_name,
00094       const basis::astring &diagnostic_info);
00096 
00097   void assert_equal(double a, double b,
00098       const basis::astring &class_name, const basis::astring &test_name,
00099       const basis::astring &diagnostic_info);
00101   void assert_not_equal(double a, double b,
00102       const basis::astring &class_name, const basis::astring &test_name,
00103       const basis::astring &diagnostic_info);
00105 
00106   void assert_equal(const void *a, const void *b,
00107       const basis::astring &class_name, const basis::astring &test_name,
00108       const basis::astring &diagnostic_info);
00110 
00113   void assert_not_equal(const void *a, const void *b,
00114       const basis::astring &class_name, const basis::astring &test_name,
00115       const basis::astring &diagnostic_info);
00117 
00118   void assert_true(bool result, 
00119       const basis::astring &class_name, const basis::astring &test_name,
00120       const basis::astring &diagnostic_info);
00122   void assert_false(bool result, 
00123       const basis::astring &class_name, const basis::astring &test_name,
00124       const basis::astring &diagnostic_info);
00126   
00127   // these two methods can be used in an ad hoc manner when using the above
00128   // assert methods is not helpful.  the "test_name" should be provided
00129   // like above, but the "diagnostic_info" can be phrased in any way needed
00130   // to describe the pass or fail event.
00131   void record_pass(const basis::astring &class_name,
00132           const basis::astring &test_name,
00133           const basis::astring &diagnostic_info);
00135   void record_fail(const basis::astring &class_name,
00136           const basis::astring &test_name,
00137           const basis::astring &diagnostic_info);
00139 
00140   int final_report();
00142 
00146 private:
00147   basis::mutex c_lock;  
00148   int c_total_tests;  
00149   int c_passed_tests;  
00150   structures::string_table c_successful;  
00151   structures::string_table c_failed;  
00152 
00153   void write_cppunit_xml();
00155 
00156   void count_successful_test(const basis::astring &class_name, const basis::astring &test_name);
00158 
00159   void count_failed_test(const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diag);
00161 
00162   void record_successful_assertion(const basis::astring &class_name, const basis::astring &test_name,
00163       const basis::astring &assertion_name);
00165 
00166   void record_failed_object_compare(const basis::hoople_standard &a,
00167       const basis::hoople_standard &b, const basis::astring &class_name,
00168       const basis::astring &test_name, const basis::astring &assertion_name);
00169   void record_failed_int_compare(int a, int b,
00170       const basis::astring &class_name, const basis::astring &test_name,
00171       const basis::astring &assertion_name);
00172   void record_failed_double_compare(double a, double b,
00173       const basis::astring &class_name, const basis::astring &test_name,
00174       const basis::astring &assertion_name);
00175   void record_failed_tf_assertion(bool result, bool expected_result,
00176       const basis::astring &class_name, const basis::astring &test_name,
00177       const basis::astring &assertion_name);
00178   void record_failed_pointer_compare(const void *a, const void *b,
00179       const basis::astring &class_name, const basis::astring &test_name,
00180       const basis::astring &assertion_name);
00181 };
00182 
00183 } //namespace.
00184 
00185 #endif
00186 
Generated on Sat Jan 28 04:22:36 2012 for hoople2 project by  doxygen 1.6.3