OTest2
A C++ testing framework
assertstream.cpp
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 #include <assertstream.h>
21 
22 #include <assert.h>
23 #include <iterator>
24 
25 #include <assertbuffer.h>
26 
27 namespace OTest2 {
28 
30  const Context& context_,
31  AssertBufferPtr buffer_,
32  bool result_,
33  std::initializer_list<const char*> parameters_) :
34  std::ostream(buffer_.get()),
35  context(&context_),
36  buffer(buffer_),
37  result(result_),
38  parameters(parameters_) {
39  assert(buffer != nullptr);
40 
41 }
42 
44  AssertStream&& other_) :
45  context(other_.context),
46  buffer(std::move(other_.buffer)),
47  result(other_.result),
48  parameters(other_.parameters) {
49  other_.buffer = nullptr;
50 }
51 
53  if(buffer != nullptr)
54  buffer->commitAssertion(*context);
55 }
56 
58  Color color_) {
59  assert(buffer != nullptr);
60  buffer->setForeground(color_);
61 }
62 
64  Color color_) {
65  assert(buffer != nullptr);
66  buffer->setBackground(color_);
67 }
68 
70  Style style_) {
71  assert(buffer != nullptr);
72  buffer->setTextStyle(style_);
73 }
74 
76  assert(buffer != nullptr);
77  buffer->resetAttributes();
78 }
79 
81  int index_) {
82  assert(index_ >= 0 && index_ < parameters.size());
83  auto iter_(parameters.begin());
84  std::advance(iter_, index_);
85  *this << *iter_;
86 }
87 
89  assert(buffer != nullptr);
90  buffer->commitMessage(*context);
91 }
92 
94  return result;
95 }
96 
97 namespace Private {
98 
102 template<int...>
103 struct NumSequence {
104 
105 };
106 
110 template<int N_, int... Seq_>
111 struct GenSequence : public GenSequence<N_ - 1, N_ - 1, Seq_...> {
112 
113 };
114 
115 template<int... Seq_>
116 struct GenSequence<0, Seq_...> {
117  typedef NumSequence<Seq_...> SeqType;
118 };
119 
120 template<typename... Values_, int... indexes_>
123  AssertStream* stream_,
124  void (AssertStream::* fce_)(Values_...),
125  const std::tuple<Values_...>& args_) {
126  (stream_->*fce_)(std::get<indexes_>(args_)...);
127 }
128 
129 template<typename... Values_>
131  void (AssertStream::* fce_)(Values_...),
132  Values_... args_) :
133  fce(fce_),
134  args(args_...) {
135 
136 }
137 
138 template<typename... Values_>
140  std::ostream& os_) const {
141  AssertStream* stream_(dynamic_cast<AssertStream*>(&os_));
142  assert(stream_ != nullptr);
144  typename GenSequence<sizeof...(Values_)>::SeqType(), stream_, fce, args);
145  return os_;
146 }
147 
148 /* -- explicit instantiations */
149 template class Manipulator<Color>;
150 template class Manipulator<Style>;
151 template class Manipulator<int>;
152 template class Manipulator<>;
153 
154 } /* -- namespace Private */
155 
156 template<typename... Values_>
157 std::ostream& operator << (
158  std::ostream& os_,
159  const Private::Manipulator<Values_...>& manip_) {
160  return manip_.doTheJob(os_);
161 }
162 
163 /* -- explicit instantiations */
164 template std::ostream& operator << (
165  std::ostream& os_,
166  const Private::Manipulator<Color>&);
167 template std::ostream& operator << (
168  std::ostream& os_,
169  const Private::Manipulator<Style>&);
170 template std::ostream& operator << (
171  std::ostream& os_,
172  const Private::Manipulator<int>&);
173 template std::ostream& operator << (
174  std::ostream& os_,
175  const Private::Manipulator<>&);
176 
178  Color color_) {
180 }
181 
183  Color color_) {
185 }
186 
188  Style style_) {
190 }
191 
194 }
195 
197  int index_) {
199 }
200 
203 }
204 
205 } /* -- namespace OTest2 */
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
assertstream.h
OTest2::Private::GenSequence
Compile-time generator of the sequence of integers.
Definition: assertstream.cpp:111
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::Private::invokeFunction
void invokeFunction(NumSequence< indexes_... >, AssertStream *stream_, void(AssertStream::*fce_)(Values_...), const std::tuple< Values_... > &args_)
Definition: assertstream.cpp:121
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
OTest2::Private::GenSequence< 0, Seq_... >::SeqType
NumSequence< Seq_... > SeqType
Definition: assertstream.cpp:117
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::NumSequence
Compile-time sequence of integers.
Definition: assertstream.cpp:103
OTest2::Private::Manipulator
Definition: assertstream.h:132
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::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
assertbuffer.h
OTest2::foreground
Private::Manipulator< Color > foreground(Color color_)
Foreground color manipulator.
Definition: assertstream.cpp:177