~ubuntu-branches/ubuntu/gutsy/poco/gutsy

« back to all changes in this revision

Viewing changes to CppUnit/src/TestRunner.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2007-04-27 18:33:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070427183348-xgnpct0qd6a2ip34
Tags: upstream-1.2.9
ImportĀ upstreamĀ versionĀ 1.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TestRunner.cpp
 
3
//
 
4
// $Id: //poco/1.2/CppUnit/src/TestRunner.cpp#1 $
 
5
//
 
6
 
 
7
 
 
8
#include "CppUnit/TestRunner.h"
 
9
#include "CppUnit/Test.h"
 
10
#include "CppUnit/TestSuite.h"
 
11
#include "CppUnit/TextTestResult.h"
 
12
#include <iostream>
 
13
 
 
14
 
 
15
namespace CppUnit {
 
16
 
 
17
 
 
18
TestRunner::TestRunner()
 
19
{
 
20
}
 
21
 
 
22
 
 
23
TestRunner::~TestRunner()
 
24
{
 
25
        for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it)
 
26
                delete it->second;
 
27
}
 
28
 
 
29
 
 
30
void TestRunner::printBanner()
 
31
{
 
32
    std::cout 
 
33
                << "Usage: driver [-all] [-print] [-wait] [name] ..." << std::endl
 
34
                << "       where name is the name of a test case class" << std::endl;
 
35
}
 
36
 
 
37
 
 
38
bool TestRunner::run(const std::vector<std::string>& args)
 
39
{
 
40
        std::string testCase;
 
41
        int numberOfTests = 0;
 
42
        bool success = true;
 
43
        bool all     = false;
 
44
        bool wait    = false;
 
45
        bool printed = false;
 
46
 
 
47
        for (int i = 1; i < args.size(); i++) 
 
48
        {
 
49
                const std::string& arg = args[i];
 
50
                if (arg == "-wait") 
 
51
                {
 
52
                        wait = true;
 
53
                        continue;
 
54
                }
 
55
                else if (arg == "-all")
 
56
                {
 
57
                        all = true;
 
58
                        continue;
 
59
                }
 
60
                else if (arg == "-print")
 
61
                {
 
62
                        for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) 
 
63
                        {
 
64
                                print(it->first, it->second, 0);
 
65
                        }
 
66
                        printed = true;
 
67
                        continue;
 
68
                }
 
69
 
 
70
                if (!all)
 
71
                {
 
72
                        testCase = arg;
 
73
 
 
74
                        if (testCase == "")
 
75
                        {
 
76
                                printBanner();
 
77
                                return false;
 
78
                        }
 
79
 
 
80
                        Test* testToRun = 0;
 
81
                        for (Mappings::iterator it = _mappings.begin(); !testToRun && it != _mappings.end(); ++it) 
 
82
                        {
 
83
                                testToRun = find(testCase, it->second, it->first);
 
84
                        }
 
85
                        if (testToRun)
 
86
                        {
 
87
                                if (!run(testToRun)) success = false;
 
88
                        }
 
89
                        numberOfTests++;
 
90
 
 
91
                        if (!testToRun) 
 
92
                        {
 
93
                                std::cout << "Test " << testCase << " not found." << std::endl;
 
94
                                return false;
 
95
                        }
 
96
                }
 
97
        }
 
98
 
 
99
        if (all)
 
100
        {
 
101
                for (Mappings::iterator it = _mappings.begin(); it != _mappings.end(); ++it) 
 
102
                {
 
103
                        if (!run(it->second)) success = false;
 
104
                        numberOfTests++;
 
105
                }
 
106
        }
 
107
        
 
108
        if (numberOfTests == 0 && !printed) 
 
109
        {
 
110
                printBanner();
 
111
                return false;
 
112
        }
 
113
 
 
114
        if (wait) 
 
115
        {
 
116
                std::cout << "<RETURN> to continue" << std::endl;
 
117
                std::cin.get();
 
118
        }
 
119
 
 
120
        return success;
 
121
}
 
122
 
 
123
 
 
124
bool TestRunner::run(Test* test)
 
125
{
 
126
        TextTestResult result;
 
127
 
 
128
        test->run(&result);
 
129
        std::cout << result << std::endl;
 
130
 
 
131
        return result.wasSuccessful();
 
132
}
 
133
 
 
134
 
 
135
void TestRunner::addTest(const std::string& name, Test* test)
 
136
{
 
137
        _mappings.push_back(Mapping(name, test));
 
138
}
 
139
 
 
140
 
 
141
void TestRunner::print(const std::string& name, Test* pTest, int indent)
 
142
{
 
143
        for (int i = 0; i < indent; ++i)
 
144
                std::cout << "    ";
 
145
        std::cout << name << std::endl;
 
146
        TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
 
147
        if (pSuite)
 
148
        {
 
149
                const std::vector<Test*>& tests = pSuite->tests();
 
150
                for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
 
151
                {
 
152
                        print((*it)->toString(), *it, indent + 1);
 
153
                }
 
154
        }
 
155
}
 
156
 
 
157
 
 
158
Test* TestRunner::find(const std::string& name, Test* pTest, const std::string& testName)
 
159
{
 
160
        if (testName.find(name) != std::string::npos)
 
161
        {
 
162
                return pTest;
 
163
        }
 
164
        else
 
165
        {
 
166
                TestSuite* pSuite = dynamic_cast<TestSuite*>(pTest);
 
167
                if (pSuite)
 
168
                {
 
169
                        const std::vector<Test*>& tests = pSuite->tests();
 
170
                        for (std::vector<Test*>::const_iterator it = tests.begin(); it != tests.end(); ++it)
 
171
                        {
 
172
                                Test* result = find(name, *it, (*it)->toString());
 
173
                                if (result) return result;
 
174
                        }
 
175
                }
 
176
                return 0;
 
177
        }
 
178
}
 
179
 
 
180
 
 
181
} // namespace CppUnit