Source

Dojo / include / dojo / TestUnit.h

Full commit
//
//  Tests.h
//
//  Created by Tommaso Checchi on 6/24/11.
//  Copyright 2011 none. All rights reserved.
//

#ifndef TestUnit_h__
#define TestUnit_h__

#include "dojo/dojo_common_header.h"

#include <dojo/Timer.h>

namespace Dojo
{		
	class TestUnit
	{
	public:
		
		TestUnit( const std::string& testName, std::ostream& out = std::cout ) :
		total( 0 ),
		failed( 0 ),
		timeTotal( 0 ),
		name( testName ),
		outStream( out )
		{
			DEBUG_ASSERT( name.size() );
			
			outStream << "RUNNING " << name;
		}
		
		~TestUnit()
		{
			report();
		}
		
		inline std::ostream& test( bool condition )
		{
			double timeSinceLastTest = testTimer.getElapsedTime();
			timeTotal += timeSinceLastTest;
			
			++total;
			
			outStream << std::endl << "[" << total << "]";
			
			if( !condition )
			{
				++failed;				
				outStream << " FAIL: ";				
			}
			else
				outStream << " OK\t\t" << timeSinceLastTest*1000.0 << " ms";
			
			testTimer.reset();
			
			return condition ? successStream : outStream;
		}
		
		inline std::ostream& testFalse( bool condition )
		{
			return test( !condition );
		}
		
		inline void ensure( bool condition )
		{
			test( condition );
			
			assert( condition );
		}
		
		void report()
		{
			outStream << std::endl;
			
			if( failed )
				outStream << "FAILED " << name << ": " << failed << " fail on " << total << " tests";
			else
				outStream << "OK " << total << " tests\t" << timeTotal*1000.0 << " ms";
			
			outStream << std::endl << std::endl;
		}
		
	protected:
				
		uint total, failed;
		double timeTotal;
		
		Timer testTimer;
		
		std::string name;
		std::stringstream successStream;
		std::ostream& outStream;
	};
}

#endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.