feisty meow concerns codebase  2.140
unit_base.h
Go to the documentation of this file.
1 #ifndef UNIT_BASE_GROUP
2 #define UNIT_BASE_GROUP
3 
4 /*
5 * Name : unit_base tools for unit testing
6 * Author : Chris Koeritz
7 **
8 * Copyright (c) 2009-$now By Author. This program is free software; you can *
9 * redistribute it and/or modify it under the terms of the GNU General Public *
10 * License as published by the Free Software Foundation; either version 2 of *
11 * the License or (at your option) any later version. This is online at: *
12 * http://www.fsf.org/copyleft/gpl.html *
13 * Please send any updates to: fred@gruntose.com *
14 */
15 
17 
21 #include <basis/astring.h>
22 #include <basis/contracts.h>
23 #include <basis/enhance_cpp.h>
24 #include <basis/mutex.h>
26 
27 namespace unit_test {
28 
29 // these macros can be used to put more information into the test name.
30 // using these is preferable to calling the class methods directly, since
31 // those cannot print out the original version of the formal parameters a
32 // and b for the test; the macros show the expressions that failed rather
33 // than just the values.
34 // note that these require the calling object to be derived from unit_base.
35 #define UNIT_BASE_THIS_OBJECT (*this)
36  // the macro for UNIT_BASE_THIS_OBJECT allows tests to use a single unit_base object by
37  // changing the value of UNIT_BASE_THIS_OBJECT to refer to the proper object's name.
38 #define ASSERT_EQUAL(a, b, test_name) { \
39  BASE_FUNCTION(func); \
40  UNIT_BASE_THIS_OBJECT.assert_equal(a, b, function_name, test_name, basis::astring(#a) + " must be equal to " + #b); \
41 }
42 #define ASSERT_INEQUAL(a, b, test_name) { \
43  BASE_FUNCTION(func); \
44  UNIT_BASE_THIS_OBJECT.assert_not_equal(a, b, function_name, test_name, basis::astring(#a) + " must be inequal to " + #b); \
45 }
46 #define ASSERT_TRUE(a, test_name) { \
47  BASE_FUNCTION(func); \
48  UNIT_BASE_THIS_OBJECT.assert_true(a, function_name, test_name, basis::astring(#a) + " must be true"); \
49 }
50 #define ASSERT_FALSE(a, test_name) { \
51  BASE_FUNCTION(func); \
52  UNIT_BASE_THIS_OBJECT.assert_false(a, function_name, test_name, basis::astring(#a) + " must be false"); \
53 }
54 // pointer versions for nicer syntax.
55 #define ASSERT_NULL(x, y) ASSERT_FALSE(x, y)
56 #define ASSERT_NON_NULL(x, y) ASSERT_TRUE(x, y)
57 
58 class unit_base : public virtual basis::nameable
59 {
60 public:
61  unit_base();
62  virtual ~unit_base();
63 
64  DEFINE_CLASS_NAME("unit_base");
65 
66  int total_tests() const;
67  int passed_tests() const;
68  int failed_tests() const;
69 
72  const basis::astring &diagnostic_info);
76  const basis::astring &diagnostic_info);
78 
79  void assert_equal(const basis::byte_array &a, const basis::byte_array &b,
81  const basis::astring &diagnostic_info);
85  const basis::astring &diagnostic_info);
87 
88  void assert_equal(int a, int b,
90  const basis::astring &diagnostic_info);
92  void assert_not_equal(int a, int b,
94  const basis::astring &diagnostic_info);
96 
97  void assert_equal(double a, double b,
99  const basis::astring &diagnostic_info);
101  void assert_not_equal(double a, double b,
103  const basis::astring &diagnostic_info);
105 
106  void assert_equal(const void *a, const void *b,
108  const basis::astring &diagnostic_info);
110 
113  void assert_not_equal(const void *a, const void *b,
115  const basis::astring &diagnostic_info);
117 
118  void assert_true(bool result,
120  const basis::astring &diagnostic_info);
122  void assert_false(bool result,
124  const basis::astring &diagnostic_info);
126 
127  // these two methods can be used in an ad hoc manner when using the above
128  // assert methods is not helpful. the "test_name" should be provided
129  // like above, but the "diagnostic_info" can be phrased in any way needed
130  // to describe the pass or fail event.
132  const basis::astring &test_name,
133  const basis::astring &diagnostic_info);
136  const basis::astring &test_name,
137  const basis::astring &diagnostic_info);
139 
140  int final_report();
142 
146 private:
147  basis::mutex c_lock;
148  int c_total_tests;
149  int c_passed_tests;
150  structures::string_table c_successful;
151  structures::string_table c_failed;
152 
153  void write_cppunit_xml();
155 
156  void count_successful_test(const basis::astring &class_name, const basis::astring &test_name);
158 
159  void count_failed_test(const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diag);
161 
162  void record_successful_assertion(const basis::astring &class_name, const basis::astring &test_name,
163  const basis::astring &assertion_name);
165 
166  void record_failed_object_compare(const basis::hoople_standard &a,
168  const basis::astring &test_name, const basis::astring &assertion_name);
169  void record_failed_int_compare(int a, int b,
171  const basis::astring &assertion_name);
172  void record_failed_double_compare(double a, double b,
174  const basis::astring &assertion_name);
175  void record_failed_tf_assertion(bool result, bool expected_result,
177  const basis::astring &assertion_name);
178  void record_failed_pointer_compare(const void *a, const void *b,
180  const basis::astring &assertion_name);
181 };
182 
183 } //namespace.
184 
185 #endif
186 
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
the base class of the most easily used and tested objects in the library.
Definition: contracts.h:161
Root object for any class that knows its own name.
Definition: contracts.h:123
virtual const char * class_name() const =0
Returns the bare name of this class as a constant character pointer.
Provides a symbol_table that holds strings as the content.
Definition: string_table.h:32
void record_pass(const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diagnostic_info)
very general recording of a successful test; better to use asserts.
Definition: unit_base.cpp:70
int total_tests() const
the total count of tests that have been run.
Definition: unit_base.cpp:53
virtual ~unit_base()
Definition: unit_base.cpp:51
void record_fail(const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diagnostic_info)
very general recording of a failed test; better to use asserts.
Definition: unit_base.cpp:93
void assert_false(bool result, const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diagnostic_info)
tests that the "result" is a false boolean.
Definition: unit_base.cpp:259
int passed_tests() const
count of successful tests run.
Definition: unit_base.cpp:55
void assert_not_equal(const basis::hoople_standard &a, const basis::hoople_standard &b, const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diagnostic_info)
tests that objects a and b are NOT equal.
Definition: unit_base.cpp:164
void assert_equal(const basis::hoople_standard &a, const basis::hoople_standard &b, const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diagnostic_info)
tests that the objects a and b are equal.
Definition: unit_base.cpp:155
DEFINE_CLASS_NAME("unit_base")
void assert_true(bool result, const basis::astring &class_name, const basis::astring &test_name, const basis::astring &diagnostic_info)
tests that the "result" is a true boolean.
Definition: unit_base.cpp:252
int final_report()
generates a report of the total number of tests that succeeded.
Definition: unit_base.cpp:266
int failed_tests() const
count of number of failed tests.
Definition: unit_base.cpp:57
Useful support functions for unit testing, especially within hoople.
Definition: unit_base.cpp:35
#define test_name()