OTest2
A C++ testing framework
assertbufferstr.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_ASSERTBUFFERSTR_H_
21 #define OTest2_INCLUDE_OTEST2_ASSERTBUFFERSTR_H_
22 
23 #include <otest2/assertbuffer.h>
25 #include <sstream>
26 #include <string>
27 
28 namespace OTest2 {
29 
30 class Context;
31 
36  bool condition;
37  std::string file;
38  int line;
39 };
40 
45  public:
46  /* -- avoid copying */
48  const AssertBufferListener&) = delete;
50  const AssertBufferListener&) = delete;
51 
56 
60  virtual ~AssertBufferListener();
61 
69  virtual void assertionOpeningMessage(
70  const Context& context_,
71  const AssertBufferAssertData& data_,
72  const std::string& message_) = 0;
73 
77  virtual void assertionAdditionalMessage(
78  const Context& context_,
79  const AssertBufferAssertData& data_,
80  const std::string& message_) = 0;
81 
85  virtual void assertionClose(
86  const Context& context_,
87  const AssertBufferAssertData& data_) = 0;
88 
95  virtual void errorOpeningMessage(
96  const Context& context_,
97  const std::string& message_) = 0;
98 
102  virtual void errorAdditionalMessage(
103  const Context& context_,
104  const std::string& message_) = 0;
105 
109  virtual void errorClose(
110  const Context& context_) = 0;
111 };
112 
118  private:
119  /* -- static buffer for std::streambuf implementation */
120  enum {
121  TMP_SIZE = 1024
122  };
123  char tmp_buffer[TMP_SIZE];
124 
125  /* -- dynamic buffer for the whole message */
126  std::stringbuf buffer;
127 
128  /* -- object status - opening of the assertion */
129  enum class State {
130  IDLE = 0,
131  OPENED,
132  ADDITIONAL,
133  } state;
134  enum class Type {
135  ASSERTION = 0,
136  ERROR,
137  } type;
139 
140  /* -- buffer listener */
141  AssertBufferListener* listener;
142 
143  void finishTmpBuffer();
144 
145  public:
146  /* -- avoid copying */
148  const AssertBufferStr&) = delete;
150  const AssertBufferStr&) = delete;
151 
157  explicit AssertBufferStr(
158  AssertBufferListener* listener_);
159 
163  virtual ~AssertBufferStr();
164 
170  void openAssertion(
171  const AssertBufferAssertData& data_);
172 
176  void openError();
177 
178  protected:
179  /* -- stream buffer */
180  virtual int overflow(
181  int c_) override final;
182 
183  public:
184  /* -- assert buffer */
185  virtual void setForeground(
186  Color color_) override;
187  virtual void setBackground(
188  Color color_) override;
189  virtual void setTextStyle(
190  Style style_) override;
191  virtual void resetAttributes() override;
192  virtual void commitMessage(
193  const Context& context_) override final;
194  virtual void commitAssertion(
195  const Context& context_) override final;
196 };
197 
198 } /* -- namespace OTest2 */
199 
200 #endif /* -- OTest2_INCLUDE_OTEST2_ASSERTBUFFERSTR_H_ */
OTest2::Style
Style
Style of shown text.
Definition: reporterattributes.h:44
assertbufferstrptr.h
OTest2::AssertBufferStr::overflow
virtual int overflow(int c_) override final
Definition: assertbufferstr.cpp:66
OTest2::AssertBufferListener::operator=
AssertBufferListener & operator=(const AssertBufferListener &)=delete
OTest2::AssertBufferListener::assertionClose
virtual void assertionClose(const Context &context_, const AssertBufferAssertData &data_)=0
Closing of the assertion.
OTest2::AssertBufferStr::setBackground
virtual void setBackground(Color color_) override
Set background color.
Definition: assertbufferstr.cpp:84
OTest2::AssertBufferStr::openError
void openError()
Open an internal error.
Definition: assertbufferstr.cpp:51
OTest2::AssertBufferStr::commitMessage
virtual void commitMessage(const Context &context_) override final
Commit (flush) current assertion message.
Definition: assertbufferstr.cpp:98
OTest2::AssertBufferStr
An implementation of the assertion buffer keeping messages in a memory buffer.
Definition: assertbufferstr.h:117
OTest2::AssertBufferStr::setForeground
virtual void setForeground(Color color_) override
Set foreground color.
Definition: assertbufferstr.cpp:79
OTest2::AssertBufferStr::operator=
AssertBufferStr & operator=(const AssertBufferStr &)=delete
OTest2::AssertBufferListener::~AssertBufferListener
virtual ~AssertBufferListener()
Dtor.
OTest2::AssertBufferStr::~AssertBufferStr
virtual ~AssertBufferStr()
Dtor.
Definition: assertbufferstr.cpp:38
OTest2::AssertBufferStr::AssertBufferStr
AssertBufferStr(const AssertBufferStr &)=delete
OTest2::Color
Color
List of colors supported by the reporters.
Definition: reporterattributes.h:30
OTest2
Definition: assertbean.h:25
OTest2::AssertBufferListener
Listener of the string assertion buffer.
Definition: assertbufferstr.h:44
OTest2::AssertBufferStr::setTextStyle
virtual void setTextStyle(Style style_) override
Set style of the shown text.
Definition: assertbufferstr.cpp:89
OTest2::AssertBuffer
Streambuffer interface used in the assertion API.
Definition: assertbuffer.h:34
OTest2::AssertBufferListener::assertionAdditionalMessage
virtual void assertionAdditionalMessage(const Context &context_, const AssertBufferAssertData &data_, const std::string &message_)=0
Additional assertion message.
OTest2::AssertBufferStr::openAssertion
void openAssertion(const AssertBufferAssertData &data_)
Open an assertion.
Definition: assertbufferstr.cpp:42
OTest2::AssertBufferListener::errorClose
virtual void errorClose(const Context &context_)=0
Closing of the internal error.
OTest2::AssertBufferListener::AssertBufferListener
AssertBufferListener()
Ctor.
OTest2::AssertBufferListener::errorAdditionalMessage
virtual void errorAdditionalMessage(const Context &context_, const std::string &message_)=0
Additional error message.
OTest2::AssertBufferAssertData::condition
bool condition
Definition: assertbufferstr.h:36
OTest2::AssertBufferStr::commitAssertion
virtual void commitAssertion(const Context &context_) override final
Finish current assertion.
Definition: assertbufferstr.cpp:144
OTest2::AssertBufferStr::resetAttributes
virtual void resetAttributes() override
Reset currently set text attributes.
Definition: assertbufferstr.cpp:94
OTest2::AssertBufferAssertData::line
int line
Definition: assertbufferstr.h:38
OTest2::AssertBufferListener::assertionOpeningMessage
virtual void assertionOpeningMessage(const Context &context_, const AssertBufferAssertData &data_, const std::string &message_)=0
First composed message of an assertion.
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::AssertBufferAssertData
Common data kept for opened assertion.
Definition: assertbufferstr.h:35
OTest2::AssertBufferListener::errorOpeningMessage
virtual void errorOpeningMessage(const Context &context_, const std::string &message_)=0
First composed message of an internal error.
OTest2::AssertBufferAssertData::file
std::string file
Definition: assertbufferstr.h:37
assertbuffer.h