00001 // unittest.h 00002 00003 /* Copyright 2009 10gen Inc. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #pragma once 00019 00020 namespace mongo { 00021 00022 /* The idea here is to let all initialization of global variables (classes inheriting from UnitTest) 00023 complete before we run the tests -- otherwise order of initilization being arbitrary may mess 00024 us up. The app's main() function should call runTests(). 00025 00026 To define a unit test, inherit from this and implement run. instantiate one object for the new class 00027 as a global. 00028 00029 These tests are ran on *every* startup of mongod, so they have to be very lightweight. But it is a 00030 good quick check for a bad build. 00031 */ 00032 struct UnitTest { 00033 UnitTest() { 00034 registerTest(this); 00035 } 00036 virtual ~UnitTest() {} 00037 00038 // assert if fails 00039 virtual void run() = 0; 00040 00041 static bool testsInProgress() { return running; } 00042 private: 00043 static vector<UnitTest*> *tests; 00044 static bool running; 00045 public: 00046 static void registerTest(UnitTest *t) { 00047 if ( tests == 0 ) 00048 tests = new vector<UnitTest*>(); 00049 tests->push_back(t); 00050 } 00051 00052 static void runTests() { 00053 running = true; 00054 for ( vector<UnitTest*>::iterator i = tests->begin(); i != tests->end(); i++ ) { 00055 (*i)->run(); 00056 } 00057 running = false; 00058 } 00059 }; 00060 00061 00062 } // namespace mongo