Changeset 913
- Timestamp:
- Sep 8, 2011, 12:21:26 AM (12 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lol/unit.h
r912 r913 18 18 19 19 #include <iostream> 20 #include <sstream> 20 21 #include <cstdio> 21 22 #include <cmath> … … 35 36 36 37 protected: 37 FixtureBase() : m_next(NULL), m_test s(0), m_fails(0) {}38 FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {} 38 39 virtual ~FixtureBase() {} 39 40 … … 54 55 } 55 56 56 virtual intRunFixture() = 0;57 virtual void RunFixture() = 0; 57 58 58 59 /* Prevent compiler complaints about unreachable code */ … … 60 61 61 62 FixtureBase *m_next; 62 int m_tests, m_fails; 63 int m_testcases, m_failcases; 64 int m_asserts, m_failure; 65 char const *m_fixturename, *m_currentname; 66 std::stringstream m_errorlog; 63 67 }; 64 68 … … 72 76 public: 73 77 void (FixtureClass::* m_fun)(); 74 char const *m_ name;78 char const *m_testname; 75 79 TestCase *m_next; 76 80 … … 78 82 void (FixtureClass::*fun)()) 79 83 { 80 that->m_ name = name;84 that->m_testname = name; 81 85 that->m_fun = fun; 82 86 GetOrSetTestCase(that); … … 90 94 91 95 /* Run all test cases in this fixture. */ 92 virtual int RunFixture() 93 { 94 for (TestCase *head = GetOrSetTestCase(); head; head = head->m_next) 95 { 96 (static_cast<FixtureClass *>(this)->*head->m_fun)(); 97 std::cout << "."; 98 } 99 return 0; 96 virtual void RunFixture() 97 { 98 m_errorlog.str(""); 99 m_testcases = 0; 100 m_failcases = 0; 101 for (TestCase *c = GetOrSetTestCase(); c; c = c->m_next) 102 { 103 m_testcases++; 104 m_asserts = 0; 105 m_failure = false; 106 m_currentname = c->m_testname; 107 (static_cast<FixtureClass *>(this)->*c->m_fun)(); 108 std::cout << (m_failure ? 'F' : '.'); 109 } 100 110 } 101 111 … … 118 128 { 119 129 public: 120 int Run() 121 { 122 int ret = 0; 123 for (FixtureBase *head = FixtureBase::GetOrSetTest(); head; head = head->m_next) 124 { 125 head->setUp(); 126 if (head->RunFixture()) 127 ret = 1; 128 head->tearDown(); 130 bool Run() 131 { 132 bool ret = true; 133 std::stringstream errors(""); 134 int failcases = 0, testcases = 0; 135 136 for (FixtureBase *f = FixtureBase::GetOrSetTest(); f; f = f->m_next) 137 { 138 f->setUp(); 139 f->RunFixture(); 140 f->tearDown(); 141 142 errors << f->m_errorlog.str(); 143 testcases += f->m_testcases; 144 failcases += f->m_failcases; 129 145 } 130 146 std::cout << std::endl; 147 148 std::cout << std::endl << std::endl; 149 if (failcases) 150 { 151 std::cout << "!!!FAILURES!!!" << std::endl; 152 std::cout << "Test Results:" << std::endl; 153 std::cout << "Run: " << testcases 154 << " Failures: " << failcases 155 << " Errors: 0" << std::endl; /* TODO: handle errors */ 156 157 std::cout << errors.str(); 158 ret = false; 159 } 160 else 161 { 162 std::cout << "OK (" << testcases << " tests)" << std::endl; 163 } 164 std::cout << std::endl << std::endl; 165 131 166 return ret; 132 167 } … … 134 169 135 170 #define LOLUNIT_FIXTURE(FixtureName) \ 171 class FixtureName; \ 172 static char const *LolUnitFixtureName(FixtureName *p) \ 173 { \ 174 (void)p; \ 175 return #FixtureName; \ 176 } \ 136 177 class FixtureName : public lol::Fixture<FixtureName> 137 178 … … 146 187 } \ 147 188 }; \ 148 TestCase##TestCaseName test_case_##TestCaseName; \189 TestCase##TestCaseName lol_unit_test_case_##TestCaseName; \ 149 190 void TestCaseName() 150 191 … … 153 194 154 195 #define LOLUNIT_ASSERT(cond) \ 155 do { if (True() && !(cond)) \ 156 { \ 157 std::cout << "FAIL! " #cond << std::endl; \ 158 return; \ 159 } } while(!True()) 196 do { \ 197 m_asserts++; \ 198 if (True() && !(cond)) \ 199 { \ 200 m_errorlog << std::endl << std::endl; \ 201 m_errorlog << ++m_failcases << ") test: " \ 202 << LolUnitFixtureName(this) << "::" << m_currentname \ 203 << " (F) line: " << __LINE__ << " " \ 204 << __FILE__ << std::endl; \ 205 m_errorlog << "assertion failed" << std::endl; \ 206 m_errorlog << "- Expression: " << #cond << std::endl; \ 207 m_failure = true; \ 208 return; \ 209 } \ 210 } while(!True()) 160 211 161 212 #define LOLUNIT_ASSERT_EQUAL(a, b) \ 162 do { if (True() && (a) != (b)) \ 163 { \ 164 std::cout << "FAIL! " #a " != " #b << std::endl; \ 165 std::cout << "expected: " << (a) << std::endl; \ 166 std::cout << "returned: " << (b) << std::endl; \ 167 return; \ 168 } } while(!True()) 213 do { \ 214 m_asserts++; \ 215 if (True() && (a) != (b)) \ 216 { \ 217 m_errorlog << std::endl << std::endl; \ 218 m_errorlog << ++m_failcases << ") test: " \ 219 << LolUnitFixtureName(this) << "::" << m_currentname \ 220 << " (F) line: " << __LINE__ << " " \ 221 << __FILE__ << std::endl; \ 222 m_errorlog << "equality assertion failed" << std::endl; \ 223 m_errorlog << "- Expected: " << (b) << std::endl; \ 224 m_errorlog << "- Actual : " << (a) << std::endl; \ 225 m_errorlog << message; \ 226 m_failure = true; \ 227 return; \ 228 } \ 229 } while(!True()) 169 230 170 231 #define LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, t) \ 171 do { if (True() && fabs((a) - (b)) > fabs((t))) \ 172 { \ 173 std::cout << "FAIL! " #a " != " #b << std::endl; \ 174 std::cout << "expected: " << (a) << std::endl; \ 175 std::cout << "returned: " << (b) << std::endl; \ 176 return; \ 177 } } while(!True()) 232 do { \ 233 m_asserts++; \ 234 if (True() && fabs((a) - (b)) > fabs((t))) \ 235 { \ 236 m_errorlog << std::endl << std::endl; \ 237 m_errorlog << ++m_failcases << ") test: " \ 238 << LolUnitFixtureName(this) << "::" << m_currentname \ 239 << " (F) line: " << __LINE__ << " " \ 240 << __FILE__ << std::endl; \ 241 m_errorlog << "double equality assertion failed" << std::endl; \ 242 m_errorlog << "- Expected: " << (b) << std::endl; \ 243 m_errorlog << "- Actual : " << (a) << std::endl; \ 244 m_errorlog << "- Delta : " << (t) << std::endl; \ 245 m_failure = true; \ 246 return; \ 247 } \ 248 } while(!True()) 178 249 179 250 } /* namespace lol */ -
trunk/test/lol-test.cpp
r912 r913 14 14 15 15 #include <cstdio> 16 #include <cstdlib> 16 17 17 18 #include <lol/unit.h> … … 20 21 { 21 22 lol::TestRunner runner; 22 int ret = !runner.Run();23 return ret;23 bool success = runner.Run(); 24 return success ? EXIT_SUCCESS : EXIT_FAILURE; 24 25 } 25 26
Note: See TracChangeset
for help on using the changeset viewer.