OTest2
A C++ testing framework
assertbufferstr.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 <assertbufferstr.h>
21 
22 #include <assert.h>
23 
24 namespace OTest2 {
25 
28 
30  AssertBufferListener* listener_) :
31  state(State::IDLE),
32  type(Type::ASSERTION),
33  listener(listener_) {
34  assert(listener != nullptr);
35  setp(tmp_buffer, tmp_buffer + TMP_SIZE);
36 }
37 
39  /* -- ignore unfinished message */
40 }
41 
43  const AssertBufferAssertData& data_) {
44  assert(state == State::IDLE);
45 
46  state = State::OPENED;
47  type = Type::ASSERTION;
48  data = data_;
49 }
50 
52  assert(state == State::IDLE);
53 
54  state = State::OPENED;
55  type = Type::ERROR;
56 }
57 
58 void AssertBufferStr::finishTmpBuffer() {
59  /* -- write temporary data */
60  buffer.sputn(tmp_buffer, pptr() - tmp_buffer);
61 
62  /* -- set new buffer */
63  setp(tmp_buffer, tmp_buffer + TMP_SIZE);
64 }
65 
67  int c_) {
68  finishTmpBuffer();
69 
70  /* -- insert the passed character */
71  if(c_ != traits_type::eof()) {
72  tmp_buffer[0] = traits_type::to_char_type(c_);
73  pbump(1);
74  }
75 
76  return traits_type::not_eof(c_);
77 }
78 
80  Color color_) {
81  /* -- default implementation is to do nothing */
82 }
83 
85  Color color_) {
86  /* -- default implementation is to do nothing */
87 }
88 
90  Style style_) {
91  /* -- default implementation is to do nothing */
92 }
93 
95  /* -- default implementation is to do nothing */
96 }
97 
99  const Context& context_) {
100  assert(state != State::IDLE);
101 
102  /* -- append temporary buffer to the string */
103  finishTmpBuffer();
104 
105  /* -- get the message */
106  std::string message_(buffer.str());
107  buffer.str("");
108 
109  /* -- emit the event */
110  switch(state) {
111  case State::OPENED:
112  state = State::ADDITIONAL;
113  switch(type) {
114  case Type::ASSERTION:
115  listener->assertionOpeningMessage(context_, data, message_);
116  break;
117  case Type::ERROR:
118  listener->errorOpeningMessage(context_, message_);
119  break;
120  default:
121  assert(false);
122  break;
123  }
124  break;
125  case State::ADDITIONAL:
126  switch(type) {
127  case Type::ASSERTION:
128  listener->assertionAdditionalMessage(context_, data, message_);
129  break;
130  case Type::ERROR:
131  listener->errorAdditionalMessage(context_, message_);
132  break;
133  default:
134  assert(false);
135  break;
136  }
137  break;
138  default:
139  assert(false);
140  break;
141  }
142 }
143 
145  const Context& context_) {
146  /* -- at least one message must have been constructed */
147  assert(state == State::ADDITIONAL);
148 
149  /* -- reset the buffer */
150  buffer.str("");
151  setp(tmp_buffer, tmp_buffer + TMP_SIZE);
152 
153  /* -- switch the state */
154  state = State::IDLE;
155 
156  /* -- emit the event */
157  switch(type) {
158  case Type::ASSERTION:
159  listener->assertionClose(context_, data);
160  break;
161  case Type::ERROR:
162  listener->errorClose(context_);
163  break;
164  default:
165  assert(false);
166  break;
167  }
168 }
169 
170 } /* -- namespace OTest2 */
OTest2::Style
Style
Style of shown text.
Definition: reporterattributes.h:44
OTest2::AssertBufferStr::overflow
virtual int overflow(int c_) override final
Definition: assertbufferstr.cpp:66
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::setForeground
virtual void setForeground(Color color_) override
Set foreground color.
Definition: assertbufferstr.cpp:79
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::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::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::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
type
TokenType type
Definition: runnerfiltertags.cpp:502
OTest2::AssertBufferAssertData
Common data kept for opened assertion.
Definition: assertbufferstr.h:35
assertbufferstr.h
OTest2::AssertBufferListener::errorOpeningMessage
virtual void errorOpeningMessage(const Context &context_, const std::string &message_)=0
First composed message of an internal error.