OTest2
A C++ testing framework
assertstream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 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 #ifndef OTest2_INCLUDE_OTEST2_ASSERTSTREAM_H_
21 #define OTest2_INCLUDE_OTEST2_ASSERTSTREAM_H_
22 
23 #include <assert.h>
24 #include <initializer_list>
25 #include <otest2/assertbufferptr.h>
27 #include <ostream>
28 #include <tuple>
29 
30 namespace OTest2 {
31 
32 class Context;
33 
40 class AssertStream : public std::ostream {
41  private:
42  const Context* context;
43  AssertBufferPtr buffer;
44  bool result;
45  std::initializer_list<const char*> parameters;
46 
47  public:
48  /* -- avoid copying */
50  const AssertStream&) = delete;
52  const AssertStream&) = delete;
53 
62  explicit AssertStream(
63  const Context& context_,
64  AssertBufferPtr buffer_,
65  bool result_,
66  std::initializer_list<const char*> parameters_);
67 
72  AssertStream&& other_);
73 
77  virtual ~AssertStream();
78 
84  void setForeground(
85  Color color_);
86 
92  void setBackground(
93  Color color_);
94 
100  void setTextStyle(
101  Style style_);
102 
106  void resetAttributes();
107 
111  void printParameter(
112  int index_);
113 
117  void commitMessage();
118 
126  bool getResult();
127 };
128 
129 namespace Private {
130 
131 template<typename... Values_>
132 class Manipulator {
133  private:
134  void (AssertStream::* fce)(Values_...);
135  std::tuple<Values_...> args;
136 
137  public:
138  explicit Manipulator(
139  void (AssertStream::* fce_)(Values_...),
140  Values_... args_);
141  std::ostream& doTheJob(
142  std::ostream& os_) const;
143 };
144 
145 } /* -- namespace Private */
146 
150 template<typename... Values_>
151 std::ostream& operator << (
152  std::ostream& os_,
153  const Private::Manipulator<Values_...>& manip_);
154 
161  Color color_);
162 
169  Color color_);
170 
177  Style style_);
178 
183 
190  int index_);
191 
196 
197 } /* -- namespace OTest2 */
198 
199 #endif /* -- OTest2_INCLUDE_OTEST2_ASSERTSTREAM_H_ */
OTest2::resetAttrs
Private::Manipulator resetAttrs()
Reset color and style attributes - manipulator.
Definition: assertstream.cpp:192
OTest2::AssertStream::printParameter
void printParameter(int index_)
Print assertion parameter at the zero based index_.
Definition: assertstream.cpp:80
OTest2::textStyle
Private::Manipulator< Style > textStyle(Style style_)
Text style manipulator.
Definition: assertstream.cpp:187
OTest2::AssertStream::resetAttributes
void resetAttributes()
Reset the color and style attributes.
Definition: assertstream.cpp:75
OTest2::Style
Style
Style of shown text.
Definition: reporterattributes.h:44
OTest2::assertPar
Private::Manipulator< int > assertPar(int index_)
Print assertion parameter.
Definition: assertstream.cpp:196
OTest2::AssertStream::~AssertStream
virtual ~AssertStream()
Dtor.
Definition: assertstream.cpp:52
OTest2::AssertStream::getResult
bool getResult()
Return the assertion result.
Definition: assertstream.cpp:93
OTest2::commitMsg
Private::Manipulator commitMsg()
Commit current assertion message.
Definition: assertstream.cpp:201
OTest2::AssertStream::setTextStyle
void setTextStyle(Style style_)
Set text style.
Definition: assertstream.cpp:69
OTest2::AssertStream::AssertStream
AssertStream(const AssertStream &)=delete
OTest2::AssertStream::setForeground
void setForeground(Color color_)
Set foreground color.
Definition: assertstream.cpp:57
OTest2::Color
Color
List of colors supported by the reporters.
Definition: reporterattributes.h:30
OTest2
Definition: assertbean.h:25
OTest2::AssertStream::commitMessage
void commitMessage()
Commit the assertion message.
Definition: assertstream.cpp:88
OTest2::AssertStream::setBackground
void setBackground(Color color_)
Set background color.
Definition: assertstream.cpp:63
reporterattributes.h
OTest2::operator<<
std::ostream & operator<<(std::ostream &os_, const Private::Manipulator< Values_... > &manip_)
Application of manipulators.
Definition: assertstream.cpp:157
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::AssertStream
Assertion stream.
Definition: assertstream.h:40
OTest2::Private::Manipulator
Definition: assertstream.h:132
assertbufferptr.h
OTest2::AssertBufferPtr
std::shared_ptr< AssertBuffer > AssertBufferPtr
Definition: assertbufferptr.h:27
OTest2::Private::Manipulator::Manipulator
Manipulator(void(AssertStream::*fce_)(Values_...), Values_... args_)
Definition: assertstream.cpp:130
OTest2::AssertStream::operator=
AssertStream & operator=(const AssertStream &)=delete
OTest2::background
Private::Manipulator< Color > background(Color color_)
Background color manipulator.
Definition: assertstream.cpp:182
OTest2::Private::Manipulator::doTheJob
std::ostream & doTheJob(std::ostream &os_) const
Definition: assertstream.cpp:139
OTest2::foreground
Private::Manipulator< Color > foreground(Color color_)
Foreground color manipulator.
Definition: assertstream.cpp:177