1/*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "TestManager.h"
8
9#include <string.h>
10
11#include "TestOutput.h"
12#include "TestVisitor.h"
13
14
15TestManager::TestManager()
16	:
17	TestSuite("all")
18{
19}
20
21
22TestManager::~TestManager()
23{
24}
25
26
27void
28TestManager::ListTests(TestOutput& output)
29{
30	struct Visitor : TestVisitor {
31		Visitor(TestOutput& output)
32			:
33			fOutput(output),
34			fLevel(0)
35		{
36		}
37
38		virtual bool VisitTest(Test* test)
39		{
40			fOutput.Print("%*s%s\n", fLevel * 2, "", test->Name());
41			return false;
42		}
43
44		virtual bool VisitTestSuitePre(TestSuite* suite)
45		{
46			if (fLevel > 0)
47				VisitTest(suite);
48			fLevel++;
49			return false;
50		}
51
52		virtual bool VisitTestSuitePost(TestSuite* suite)
53		{
54			fLevel--;
55			return false;
56		}
57
58	private:
59		TestOutput&	fOutput;
60		int			fLevel;
61	} visitor(output);
62
63	output.Print("Available tests:\n");
64	Visit(visitor);
65}
66
67
68void
69TestManager::RunTests(GlobalTestContext& globalContext,
70	const char* const* tests, int testCount)
71{
72	TestContext context(&globalContext);
73
74	context.Print("Running tests:\n");
75
76	if (testCount == 0 || (testCount == 1 && strcmp(tests[0], "all") == 0)) {
77		Run(context);
78	} else {
79		for (int i = 0; i < testCount; i++) {
80			bool result = Run(context, tests[i]);
81			if (!result && context.Options().quitAfterFailure)
82				break;
83		}
84	}
85
86	context.Print("run tests: %ld, failed tests: %ld\n",
87		globalContext.TotalTests(), globalContext.FailedTests());
88}
89