OTest2
A C++ testing framework
reportertee.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Ondrej Starek
3  *
4  * This file is part of OTest2.
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <reportertee.h>
21 
22 #include <algorithm>
23 #include <assert.h>
24 #include <functional>
25 #include <vector>
26 
27 #include <assertbuffer.h>
28 
29 namespace OTest2 {
30 
31 using namespace std::placeholders;
32 
33 namespace {
34 
35 class Buffer : public AssertBuffer {
36  private:
37  std::vector<AssertBufferPtr> buffers;
38 
39  public:
40  /* -- avoid copying */
41  Buffer(
42  const Buffer&) = delete;
43  Buffer& operator = (
44  const Buffer&) = delete;
45 
46  public:
47  Buffer();
48  virtual ~Buffer();
49 
50  void appendBuffer(
51  AssertBufferPtr buffer_);
52 
53  protected:
54  /* -- stream buffer */
55  virtual int overflow(
56  int c_) override final;
57 
58  public:
59  /* -- assertion buffer */
60  virtual void setForeground(
61  Color color_) override;
62  virtual void setBackground(
63  Color color_) override;
64  virtual void setTextStyle(
65  Style style_) override;
66  virtual void resetAttributes() override;
67  virtual void commitMessage(
68  const Context& context_) override;
69  virtual void commitAssertion(
70  const Context& context_) override;
71 };
72 
73 Buffer::Buffer() = default;
74 Buffer::~Buffer() = default;
75 
76 void Buffer::appendBuffer(
77  AssertBufferPtr buffer_) {
78  buffers.push_back(buffer_);
79 }
80 
81 int Buffer::overflow(
82  int c_) {
83  if(c_ != traits_type::eof()) {
84  std::for_each(
85  buffers.begin(),
86  buffers.end(),
87  std::bind(&AssertBuffer::sputc, _1, traits_type::to_char_type(c_)));
88  }
89  return traits_type::not_eof(c_);
90 }
91 
92 void Buffer::setForeground(
93  Color color_) {
94  std::for_each(
95  buffers.begin(),
96  buffers.end(),
97  std::bind(&AssertBuffer::setForeground, _1, color_));
98 }
99 
100 void Buffer::setBackground(
101  Color color_) {
102  std::for_each(
103  buffers.begin(),
104  buffers.end(),
105  std::bind(&AssertBuffer::setBackground, _1, color_));
106 }
107 
108 void Buffer::setTextStyle(
109  Style style_) {
110  std::for_each(
111  buffers.begin(),
112  buffers.end(),
113  std::bind(&AssertBuffer::setTextStyle, _1, style_));
114 }
115 
116 void Buffer::resetAttributes() {
117  std::for_each(
118  buffers.begin(),
119  buffers.end(),
120  std::bind(&AssertBuffer::resetAttributes, _1));
121 }
122 
123 void Buffer::commitMessage(
124  const Context& context_) {
125  std::for_each(
126  buffers.begin(),
127  buffers.end(),
128  std::bind(&AssertBuffer::commitMessage, _1, std::cref(context_)));
129 }
130 
131 void Buffer::commitAssertion(
132  const Context& context_) {
133  std::for_each(
134  buffers.begin(),
135  buffers.end(),
136  std::bind(&AssertBuffer::commitAssertion, _1, std::cref(context_)));
137 }
138 
139 } /* -- namespace */
140 
142 
143 }
144 
146 
147 }
148 
150  Reporter* reporter_) {
151  assert(reporter_ != nullptr);
152 
153  reporters.push_back(reporter_);
154 }
155 
157  const Context& context_,
158  const std::string& name_,
159  const Parameters& params_) {
160  std::for_each(
161  reporters.begin(),
162  reporters.end(),
163  std::bind(
165  _1,
166  std::cref(context_),
167  std::cref(name_),
168  std::cref(params_)));
169 }
170 
172  const Context& context_,
173  const std::string& name_,
174  const Parameters& params_) {
175  std::for_each(
176  reporters.begin(),
177  reporters.end(),
178  std::bind(
180  _1,
181  std::cref(context_),
182  std::cref(name_),
183  std::cref(params_)));
184 }
185 
187  const Context& context_,
188  const std::string& name_,
189  const Parameters& params_) {
190  std::for_each(
191  reporters.begin(),
192  reporters.end(),
193  std::bind(
195  _1,
196  std::cref(context_),
197  std::cref(name_),
198  std::cref(params_)));
199 }
200 
202  const Context& context_,
203  const std::string& name_) {
204  std::for_each(
205  reporters.begin(),
206  reporters.end(),
207  std::bind(
209  _1,
210  std::cref(context_),
211  std::cref(name_)));
212 }
213 
215  const Context& context_,
216  bool condition_,
217  const std::string& file_,
218  int lineno_) {
219  auto buffer_(std::make_shared<Buffer>());
220  std::for_each(
221  reporters.begin(),
222  reporters.end(),
223  [buffer_, &context_, condition_, &file_, lineno_](Reporter* reporter_) {
224  buffer_->appendBuffer(
225  reporter_->enterAssert(context_, condition_, file_, lineno_));
226  }
227  );
228  return buffer_;
229 }
230 
232  const Context& context_) {
233  auto buffer_(std::make_shared<Buffer>());
234  std::for_each(
235  reporters.begin(),
236  reporters.end(),
237  [buffer_, &context_](Reporter* reporter_) {
238  buffer_->appendBuffer(
239  reporter_->enterError(context_));
240  }
241  );
242  return buffer_;
243 }
244 
246  const Context& context_,
247  const std::string& name_,
248  bool result_) {
249  std::for_each(
250  reporters.begin(),
251  reporters.end(),
252  std::bind(
254  _1,
255  std::cref(context_),
256  std::cref(name_),
257  result_));
258 }
259 
261  const Context& context_,
262  const std::string& name_,
263  const Parameters& params_,
264  bool result_) {
265  std::for_each(
266  reporters.begin(),
267  reporters.end(),
268  std::bind(
270  _1,
271  std::cref(context_),
272  std::cref(name_),
273  std::cref(params_),
274  result_));
275 }
276 
278  const Context& context_,
279  const std::string& name_,
280  const Parameters& params_,
281  bool result_) {
282  std::for_each(
283  reporters.begin(),
284  reporters.end(),
285  std::bind(
287  _1,
288  std::cref(context_),
289  std::cref(name_),
290  std::cref(params_),
291  result_));
292 }
293 
295  const Context& context_,
296  const std::string& name_,
297  const Parameters& params_,
298  bool result_) {
299  std::for_each(
300  reporters.begin(),
301  reporters.end(),
302  std::bind(
304  _1,
305  std::cref(context_),
306  std::cref(name_),
307  std::cref(params_),
308  result_));
309 }
310 
311 } /* -- namespace OTest2 */
OTest2::Reporter::leaveTest
virtual void leaveTest(const Context &context_, const std::string &name_, const Parameters &params_, bool result_)=0
Leave entire test.
reportertee.h
OTest2::Style
Style
Style of shown text.
Definition: reporterattributes.h:44
OTest2::Reporter::leaveSuite
virtual void leaveSuite(const Context &context_, const std::string &name_, const Parameters &params_, bool result_)=0
Leave a suite.
OTest2::Reporter
Generic test reporter.
Definition: reporter.h:34
OTest2::ReporterTee::ReporterTee
ReporterTee()
Ctor.
Definition: reportertee.cpp:141
OTest2::Reporter::enterState
virtual void enterState(const Context &context_, const std::string &name_)=0
Enter a state.
OTest2::AssertBuffer::commitMessage
virtual void commitMessage(const Context &context_)=0
Commit (flush) current assertion message.
OTest2::Reporter::enterTest
virtual void enterTest(const Context &context_, const std::string &name_, const Parameters &params_)=0
Enter entire test.
OTest2::ReporterTee::enterSuite
virtual void enterSuite(const Context &context_, const std::string &name_, const Parameters &params_) override
Enter a suite.
Definition: reportertee.cpp:171
OTest2::ReporterTee::leaveState
virtual void leaveState(const Context &context_, const std::string &name_, bool result_) override
Leave a state.
Definition: reportertee.cpp:245
OTest2::ReporterTee::leaveSuite
virtual void leaveSuite(const Context &context_, const std::string &name_, const Parameters &params_, bool result_) override
Leave a suite.
Definition: reportertee.cpp:277
OTest2::ReporterTee::leaveCase
virtual void leaveCase(const Context &context_, const std::string &name_, const Parameters &params_, bool result_) override
Leave a case.
Definition: reportertee.cpp:260
OTest2::ReporterTee::enterTest
virtual void enterTest(const Context &context_, const std::string &name_, const Parameters &params_) override
Enter entire test.
Definition: reportertee.cpp:156
OTest2::ReporterTee::~ReporterTee
virtual ~ReporterTee()
Dtor.
Definition: reportertee.cpp:145
OTest2::Reporter::enterCase
virtual void enterCase(const Context &context_, const std::string &name_, const Parameters &params_)=0
Enter a case.
OTest2::AssertBuffer::resetAttributes
virtual void resetAttributes()=0
Reset currently set text attributes.
OTest2::ReporterTee::enterCase
virtual void enterCase(const Context &context_, const std::string &name_, const Parameters &params_) override
Enter a case.
Definition: reportertee.cpp:186
OTest2::ReporterTee::enterError
virtual AssertBufferPtr enterError(const Context &context_) override
Enter an error report.
Definition: reportertee.cpp:231
OTest2::AssertBuffer::setTextStyle
virtual void setTextStyle(Style style_)=0
Set style of the shown text.
OTest2::AssertBuffer::setBackground
virtual void setBackground(Color color_)=0
Set background color.
OTest2::Parameters
Generic parameters of a run of an testing object.
Definition: parameters.h:30
OTest2::ReporterTee::leaveTest
virtual void leaveTest(const Context &context_, const std::string &name_, const Parameters &params_, bool result_) override
Leave entire test.
Definition: reportertee.cpp:294
OTest2::Color
Color
List of colors supported by the reporters.
Definition: reporterattributes.h:30
OTest2
Definition: assertbean.h:25
OTest2::Reporter::enterSuite
virtual void enterSuite(const Context &context_, const std::string &name_, const Parameters &params_)=0
Enter a suite.
OTest2::Reporter::leaveState
virtual void leaveState(const Context &context_, const std::string &name_, bool result_)=0
Leave a state.
OTest2::ReporterTee::enterState
virtual void enterState(const Context &context_, const std::string &name_) override
Enter a state.
Definition: reportertee.cpp:201
OTest2::AssertBuffer::setForeground
virtual void setForeground(Color color_)=0
Set foreground color.
OTest2::AssertBuffer::commitAssertion
virtual void commitAssertion(const Context &context_)=0
Finish current assertion.
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::ReporterTee::appendReporter
void appendReporter(Reporter *reporter_)
Append a reporter.
Definition: reportertee.cpp:149
OTest2::Reporter::leaveCase
virtual void leaveCase(const Context &context_, const std::string &name_, const Parameters &params_, bool result_)=0
Leave a case.
OTest2::AssertBufferPtr
std::shared_ptr< AssertBuffer > AssertBufferPtr
Definition: assertbufferptr.h:27
OTest2::ReporterTee::enterAssert
virtual AssertBufferPtr enterAssert(const Context &context_, bool condition_, const std::string &file_, int lineno_) override
Enter an assertion.
Definition: reportertee.cpp:214
assertbuffer.h