⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testrunner.cpp

📁 This software aims to create an applet and panel tools to manage a wireless interface card, such as
💻 CPP
字号:
//
// TestRunner.cpp
//
// $Id: //poco/Main/CppUnit/src/TestRunner.cpp#5 $
//


#include "CppUnit/TestRunner.h"
#include "CppUnit/Test.h"
#include "CppUnit/TestSuite.h"
#include "CppUnit/TextTestResult.h"
#include <iostream>


CppUnit_BEGIN


TestRunner::TestRunner()
{
}


TestRunner::~TestRunner()
{
	for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
		delete it->second;
}


void TestRunner::printBanner()
{
    std::cout 
		<< "Usage: driver [-all] [-print] [-wait] [name] ..." << std::endl
		<< "       where name is the name of a test case class" << std::endl;
}


bool TestRunner::run(const std::vector<std::string>& args)
{
	std::string testCase;
	int numberOfTests = 0;
	bool success = true;
	bool all     = false;
	bool wait    = false;
	bool printed = false;

	for (int i = 1; i < args.size(); i++) 
	{
		const std::string& arg = args[i];
		if (arg == "-wait") 
		{
			wait = true;
			continue;
		}
		else if (arg == "-all")
		{
			all = true;
			continue;
		}
		else if (arg == "-print")
		{
			for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) 
			{
				print(it->first, it->second, 0);
			}
			printed = true;
			continue;
		}

		if (!all)
		{
			testCase = arg;

			if (testCase == "")
			{
				printBanner();
				return false;
			}

			Test* testToRun = 0;
			for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it) 
			{
				testToRun = find(testCase, it->second, it->first);
			}
			if (testToRun)
			{
				if (!run(testToRun)) success = false;
			}
			numberOfTests++;

			if (!testToRun) 
			{
				std::cout << "Test " << testCase << " not found." << std::endl;
				return false;
			}
		}
	}

	if (all)
	{
		for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) 
		{
			if (!run(it->second)) success = false;
			numberOfTests++;
		}
	}
	
	if (numberOfTests == 0 && !printed) 
	{
		printBanner();
		return false;
	}

	if (wait) 
	{
		std::cout << "<RETURN> to continue" << std::endl;
		std::cin.get();
	}

	return success;
}


bool TestRunner::run(Test* test)
{
	TextTestResult result;

	test->run(&result);
	std::cout << result << std::endl;

	return result.wasSuccessful();
}


void TestRunner::addTest(const std::string& name, Test* test)
{
	_mappings.push_back(Mapping(name, test));
}


void TestRunner::print(const std::string& name, Test* pTest, int indent)
{
	for (int i = 0; i < indent; ++i)
		std::cout << "    ";
	std::cout << name << std::endl;
	TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
	if (pSuite)
	{
		const std::vector<Test*>& tests = pSuite->tests();
		for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
		{
			print((*it)->toString(), *it, indent + 1);
		}
	}
}


Test* TestRunner::find(const std::string& name, Test* pTest, const std::string& testName)
{
	if (testName.find(name) != std::string::npos)
	{
		return pTest;
	}
	else
	{
		TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
		if (pSuite)
		{
			const std::vector<Test*>& tests = pSuite->tests();
			for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
			{
				Test* result = find(name, *it, (*it)->toString());
				if (result) return result;
			}
		}
		return 0;
	}
}


CppUnit_END

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -