summaryrefslogtreecommitdiff
path: root/testshl2/source/cppunit/result/TestResult.cpp
blob: 5fa5e444e540e0af6a09bc5d731c8e3812aae9be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_testshl2.hxx"

#include <cppunit/TestFailure.h>
#include <testshl/result/TestListener.h>
#include "testshl/getopt.hxx"
#include <testshl/result/TestResult.h>
#include <algorithm>
#include "testshl/result/outputter.hxx"
#include <cppunit/Test.h>
#include <testshl/cmdlinebits.hxx>

namespace CppUnit {

/// Construct a TestResult
TestResult::TestResult( GetOpt &_pOptions, SynchronizationObject *syncObject )
    : SynchronizedObject( syncObject ),
      m_aOptionHelper(_pOptions),
      m_nExitValue(0)
{
  reset();
}


/// Destroys a test result
TestResult::~TestResult()
{
}


/** Resets the result for a new run.
 *
 * Clear the previous run result.
 */
void
TestResult::reset()
{
  ExclusiveZone zone( m_syncObject );
  m_stop = false;
}


/** Adds an error to the list of errors.
 *  The passed in exception
 *  caused the error
 */
void
TestResult::addError( Test *test,
                      Exception *e, ErrorType::num _eType )
{
  TestFailure aTestFailure( test, e, _eType );
  addFailure( aTestFailure );
}


/** Adds a failure to the list of failures. The passed in exception
 * caused the failure.
 */
void
TestResult::addFailure( Test *test, Exception *e )
{
  TestFailure aTestFailure( test, e, ErrorType::ET_FAILURE );
  addFailure( aTestFailure );
}


/** Called to add a failure to the list of failures.
 */
void
TestResult::addFailure( const TestFailure &failure )
{
  ExclusiveZone zone( m_syncObject );

  // LLA:
  // this set the global returnvalue, due to the fact, there occurs a failure, we have to return a non zero value
  // at the moment this seams to be a good place.
  setExitValue(1);

  for ( TestListeners::iterator it = m_listeners.begin();
        it != m_listeners.end();
        ++it )
  {
      TestListener *pListener = *it;
      pListener->addFailure( failure );
  }
}


/// Informs the result that a test will be started.
void
TestResult::startTest( Test *test )
{
  ExclusiveZone zone( m_syncObject );
  if (m_aOptionHelper.isVerbose())
  {
      std::string aStr;
      if (test)
      {
          aStr = getNodeName();
          aStr += ".";
          aStr += test->getName();
      }
      // fprintf(stderr, "Start test: %s\n", aStr.c_str());
      t_print( T_VERBOSE, "Start test: %s\n", aStr.c_str());
  }

  for ( TestListeners::iterator it = m_listeners.begin();
        it != m_listeners.end();
        ++it )
  {
      TestListener *pListener = *it;
      pListener->startTest( test );
  }
}


/// Informs the result that a test was completed.
void
TestResult::endTest( Test *test )
{
  ExclusiveZone zone( m_syncObject );
  for ( TestListeners::iterator it = m_listeners.begin();
        it != m_listeners.end();
        ++it )
  {
      TestListener *pListener = *it;
      pListener->endTest( test );
  }
}


/// Returns whether testing should be stopped
bool
TestResult::shouldStop() const
{
  ExclusiveZone zone( m_syncObject );
  return m_stop;
}


/// Stop testing
void
TestResult::stop()
{
  ExclusiveZone zone( m_syncObject );
  m_stop = true;
}


void
TestResult::addListener( TestListener *listener )
{
  ExclusiveZone zone( m_syncObject );
  m_listeners.push_back( listener );
}


void
TestResult::removeListener ( TestListener *listener )
{
  ExclusiveZone zone( m_syncObject );
#if defined(_MSC_VER) && (_MSC_VER >=1400)
  m_listeners.erase( remove( m_listeners.begin(),
#else
  m_listeners.erase( std::remove( m_listeners.begin(),
#endif
                                  m_listeners.end(),
                                  listener ),
                     m_listeners.end());
}

void
TestResult::addInfo(Test *test, const char* _aStr)
{
    ExclusiveZone zone( m_syncObject );
    for ( TestListeners::iterator it = m_listeners.begin();
          it != m_listeners.end();
          ++it )
    {
        TestListener *pListener = *it;
        pListener->addInfo( test, _aStr );
    }
}

// old: void
// old: TestResult::enterNode(const char* _aStr)
// old: {
// old:         ExclusiveZone zone( m_syncObject );
// old:         for ( TestListeners::iterator it = m_listeners.begin();
// old:                   it != m_listeners.end();
// old:                   ++it )
// old:         {
// old:                 TestListener *pListener = *it;
// old:                 pListener->enterNode( _aStr );
// old:         }
// old: }
// old:
// old: void
// old: TestResult::leaveNode(const char* _aStr)
// old: {
// old:         ExclusiveZone zone( m_syncObject );
// old:
// old:         for ( TestListeners::iterator it = m_listeners.begin();
// old:                   it != m_listeners.end();
// old:                   ++it )
// old:         {
// old:                 TestListener *pListener = *it;
// old:                 pListener->leaveNode( _aStr );
// old:         }
// old: }

void TestResult::enterNode(const char* _sNode)
{
    ExclusiveZone zone( m_syncObject );
    m_aCurrentNodeNames.push_back(std::string(_sNode));
}

void TestResult::leaveNode(const char* /*_sNode*/)
{
    ExclusiveZone zone( m_syncObject );
    std::string sBack = m_aCurrentNodeNames.back();
    m_aCurrentNodeNames.pop_back();

    // due to a -Wall warning, comment out.
    // if (sBack != std::string(_sNode))
    // {
    //     volatile int dummy = 0;
    //     // problem?!
    // }
}

std::string TestResult::getNodeName()
{
    std::string sName;
    for (std::vector<std::string>::const_iterator it = m_aCurrentNodeNames.begin();
         it != m_aCurrentNodeNames.end();
         ++it)
    {
        if (sName.size() != 0)
        {
            sName += ".";
        }
        sName += *it;
    }
    return sName;
}

// -----------------------------------------------------------------------------
bool TestResult::isAllowedToExecute(std::string const& _sName)
{
    return m_aOptionHelper.isAllowedToExecute(getNodeName(), _sName);
}
// -----------------------------------------------------------------------------
bool TestResult::isOptionWhereAmI()
{
    return m_aOptionHelper.isOptionWhereAmI();
}

// -----------------------------------------------------------------------------
void TestResult::print(Outputter&)
{
}

} // namespace CppUnit