1 #include <cppunit/TestFailure.h>
2 #include <cppunit/TestListener.h>
3 #include <cppunit/TestResult.h>
4 #include <algorithm>
5
6 namespace CppUnit {
7
8 /// Construct a TestResult
TestResult(SynchronizationObject * syncObject)9 TestResult::TestResult( SynchronizationObject *syncObject )
10 : SynchronizedObject( syncObject )
11 {
12 reset();
13 }
14
15
16 /// Destroys a test result
~TestResult()17 TestResult::~TestResult()
18 {
19 }
20
21
22 /** Resets the result for a new run.
23 *
24 * Clear the previous run result.
25 */
26 void
reset()27 TestResult::reset()
28 {
29 ExclusiveZone zone( m_syncObject );
30 m_stop = false;
31 }
32
33
34 /** Adds an error to the list of errors.
35 * The passed in exception
36 * caused the error
37 */
38 void
addError(Test * test,Exception * e)39 TestResult::addError( Test *test,
40 Exception *e )
41 {
42 addFailure( TestFailure( test, e, true ) );
43 }
44
45
46 /** Adds a failure to the list of failures. The passed in exception
47 * caused the failure.
48 */
49 void
addFailure(Test * test,Exception * e)50 TestResult::addFailure( Test *test, Exception *e )
51 {
52 addFailure( TestFailure( test, e, false ) );
53 }
54
55
56 /** Called to add a failure to the list of failures.
57 */
58 void
addFailure(const TestFailure & failure)59 TestResult::addFailure( const TestFailure &failure )
60 {
61 ExclusiveZone zone( m_syncObject );
62 for ( TestListeners::iterator it = m_listeners.begin();
63 it != m_listeners.end();
64 ++it )
65 (*it)->addFailure( failure );
66 }
67
68
69 /// Informs the result that a test will be started.
70 void
startTest(Test * test)71 TestResult::startTest( Test *test )
72 {
73 ExclusiveZone zone( m_syncObject );
74 for ( TestListeners::iterator it = m_listeners.begin();
75 it != m_listeners.end();
76 ++it )
77 (*it)->startTest( test );
78 }
79
80
81 /// Informs the result that a test was completed.
82 void
endTest(Test * test)83 TestResult::endTest( Test *test )
84 {
85 ExclusiveZone zone( m_syncObject );
86 for ( TestListeners::iterator it = m_listeners.begin();
87 it != m_listeners.end();
88 ++it )
89 (*it)->endTest( test );
90 }
91
92
93 /// Returns whether testing should be stopped
94 bool
shouldStop() const95 TestResult::shouldStop() const
96 {
97 ExclusiveZone zone( m_syncObject );
98 return m_stop;
99 }
100
101
102 /// Stop testing
103 void
stop()104 TestResult::stop()
105 {
106 ExclusiveZone zone( m_syncObject );
107 m_stop = true;
108 }
109
110
111 void
addListener(TestListener * listener)112 TestResult::addListener( TestListener *listener )
113 {
114 ExclusiveZone zone( m_syncObject );
115 m_listeners.push_back( listener );
116 }
117
118
119 void
removeListener(TestListener * listener)120 TestResult::removeListener ( TestListener *listener )
121 {
122 ExclusiveZone zone( m_syncObject );
123 m_listeners.erase( remove( m_listeners.begin(),
124 m_listeners.end(),
125 listener ),
126 m_listeners.end());
127 }
128
129 } // namespace CppUnit
130