OTest2
A C++ testing framework
assertcontext.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 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 <assertcontext.h>
21 
22 #include <assert.h>
23 
24 #include <assertstream.h>
25 #include <context.h>
26 #include <reporter.h>
27 #include <semanticstack.h>
28 
29 namespace OTest2 {
30 
32  const Context& context_,
33  const std::string& file_,
34  int lineno_,
35  std::initializer_list<const char*> parameters_) :
36  context(&context_),
37  file(file_),
38  lineno(lineno_),
39  parameters(parameters_) {
40 
41 }
42 
44 
46  bool result_) {
47  /* -- change the result of current object */
48  if(!result_)
49  context->semantic_stack->setTop(false);
50 
51  /* -- open the assertion for additional messages */
52  auto assert_buffer_(context->reporter->enterAssert(
53  *context, result_, file, lineno));
54 
55  return AssertStream(*context, assert_buffer_, result_, parameters);
56 }
57 
59  bool condition_,
60  const std::string& message_,
61  bool print_expression_) {
62  /* -- open the assertion */
63  AssertStream report_(enterAssertion(condition_));
64 
65  /* -- report the assertion */
66  if(print_expression_)
67  report_ << '\'' << assertPar(0) << '\'';
68  if(!message_.empty()) {
69  if(print_expression_)
70  report_ << ' ';
71  report_ << message_;
72  }
73  report_ << commitMsg();
74 
75  return report_.getResult();
76 }
77 
79  return *context;
80 }
81 
82 } /* namespace OTest2 */
OTest2::AssertContext::AssertContext
AssertContext(const AssertContext &)=delete
OTest2::assertPar
Private::Manipulator< int > assertPar(int index_)
Print assertion parameter.
Definition: assertstream.cpp:196
OTest2::AssertContext::~AssertContext
~AssertContext()
Dtor.
assertstream.h
assertcontext.h
OTest2::Context::reporter
Reporter *const reporter
Definition: context.h:45
reporter.h
OTest2::AssertStream::getResult
bool getResult()
Return the assertion result.
Definition: assertstream.cpp:93
OTest2::AssertContext::enterAssertion
AssertStream enterAssertion(bool result_)
Enter an assertion.
Definition: assertcontext.cpp:45
OTest2::commitMsg
Private::Manipulator commitMsg()
Commit current assertion message.
Definition: assertstream.cpp:201
semanticstack.h
OTest2
Definition: assertbean.h:25
OTest2::Reporter::enterAssert
virtual AssertBufferPtr enterAssert(const Context &context_, bool condition_, const std::string &file_, int lineno_)=0
Enter an assertion.
OTest2::AssertContext::simpleAssertionImpl
bool simpleAssertionImpl(bool condition_, const std::string &message_, bool print_expression_)
Implementation of a simple assertion.
Definition: assertcontext.cpp:58
OTest2::Context::semantic_stack
SemanticStack *const semantic_stack
Definition: context.h:41
context.h
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
OTest2::SemanticStack::setTop
void setTop(bool value_)
Set top value.
Definition: semanticstack.cpp:75
OTest2::AssertStream
Assertion stream.
Definition: assertstream.h:40
OTest2::AssertContext::otest2Context
const Context & otest2Context() const
Get the OTest2 context.
Definition: assertcontext.cpp:78