1 #include <cppunit/Message.h>
2 #include <stdexcept>
3
4
5 CPPUNIT_NS_BEGIN
6
Message(const Message & other)7 Message::Message( const Message &other )
8 : m_shortDescription()
9 , m_details()
10 {
11 *this = other;
12 }
13
Message(const std::string & shortDescription)14 Message::Message( const std::string &shortDescription )
15 : m_shortDescription( shortDescription )
16 , m_details()
17 {
18 }
19
20
Message(const std::string & shortDescription,const std::string & detail1)21 Message::Message( const std::string &shortDescription,
22 const std::string &detail1 )
23 : m_shortDescription( shortDescription )
24 , m_details()
25 {
26 addDetail( detail1 );
27 }
28
29
Message(const std::string & shortDescription,const std::string & detail1,const std::string & detail2)30 Message::Message( const std::string &shortDescription,
31 const std::string &detail1,
32 const std::string &detail2 )
33 : m_shortDescription( shortDescription )
34 , m_details()
35 {
36 addDetail( detail1, detail2 );
37 }
38
39
Message(const std::string & shortDescription,const std::string & detail1,const std::string & detail2,const std::string & detail3)40 Message::Message( const std::string &shortDescription,
41 const std::string &detail1,
42 const std::string &detail2,
43 const std::string &detail3 )
44 : m_shortDescription( shortDescription )
45 , m_details()
46 {
47 addDetail( detail1, detail2, detail3 );
48 }
49
~Message()50 Message::~Message()
51 {
52 }
53
54 Message &
operator =(const Message & other)55 Message::operator =( const Message &other )
56 {
57 if ( this != &other )
58 {
59 m_shortDescription = other.m_shortDescription.c_str();
60 m_details.clear();
61 Details::const_iterator it = other.m_details.begin();
62 Details::const_iterator itEnd = other.m_details.end();
63 while ( it != itEnd )
64 m_details.push_back( (*it++).c_str() );
65 }
66
67 return *this;
68 }
69
70
71 const std::string &
shortDescription() const72 Message::shortDescription() const
73 {
74 return m_shortDescription;
75 }
76
77
78 int
detailCount() const79 Message::detailCount() const
80 {
81 return m_details.size();
82 }
83
84
85 std::string
detailAt(int index) const86 Message::detailAt( int index ) const
87 {
88 if ( index < 0 || index >= detailCount() )
89 throw std::invalid_argument( "Message::detailAt() : invalid index" );
90
91 return m_details[ index ];
92 }
93
94
95 std::string
details() const96 Message::details() const
97 {
98 std::string details;
99 for ( Details::const_iterator it = m_details.begin(); it != m_details.end(); ++it )
100 {
101 details += "- ";
102 details += *it;
103 details += '\n';
104 }
105 return details;
106 }
107
108
109 void
clearDetails()110 Message::clearDetails()
111 {
112 m_details.clear();
113 }
114
115
116 void
addDetail(const std::string & detail)117 Message::addDetail( const std::string &detail )
118 {
119 m_details.push_back( detail );
120 }
121
122
123 void
addDetail(const std::string & detail1,const std::string & detail2)124 Message::addDetail( const std::string &detail1,
125 const std::string &detail2 )
126 {
127 addDetail( detail1 );
128 addDetail( detail2 );
129 }
130
131
132 void
addDetail(const std::string & detail1,const std::string & detail2,const std::string & detail3)133 Message::addDetail( const std::string &detail1,
134 const std::string &detail2,
135 const std::string &detail3 )
136 {
137 addDetail( detail1, detail2 );
138 addDetail( detail3 );
139 }
140
141
142 void
addDetail(const Message & message)143 Message::addDetail( const Message &message )
144 {
145 m_details.insert( m_details.end(),
146 message.m_details.begin(),
147 message.m_details.end() );
148 }
149
150
151 void
setShortDescription(const std::string & shortDescription)152 Message::setShortDescription( const std::string &shortDescription )
153 {
154 m_shortDescription = shortDescription;
155 }
156
157
158 bool
operator ==(const Message & other) const159 Message::operator ==( const Message &other ) const
160 {
161 return m_shortDescription == other.m_shortDescription &&
162 m_details == other.m_details;
163 }
164
165
166 bool
operator !=(const Message & other) const167 Message::operator !=( const Message &other ) const
168 {
169 return !( *this == other );
170 }
171
172
173 CPPUNIT_NS_END
174
175