OTest2
A C++ testing framework
assertions.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 <assertions.h>
21 
22 #include <string>
23 
24 #include <assertbean.h>
25 #include <context.h>
26 #include <exccatcher.h>
27 
28 namespace OTest2 {
29 
31  bool condition_) {
32  if(condition_)
33  return simpleAssertionImpl(condition_, "", true);
34  else
35  return simpleAssertionImpl(condition_, "has failed", true);
36 }
37 
39  const AssertBean& bean_) {
40  if(bean_.getCondition())
41  return simpleAssertionImpl(bean_.getCondition(), "", true);
42  else
43  return simpleAssertionImpl(bean_.getCondition(), bean_.getMessage(), true);
44 }
45 
47  std::function<bool()> ftor_) {
48  std::string message_;
49  bool result_(false);
50  if(otest2Context().exception_catcher->catchException(
51  otest2Context(),
52  [&result_, ftor_](const Context&) {
53  result_ = ftor_();
54  },
55  message_)) {
56  return simpleAssertionImpl(false, message_, false);
57  }
58  if(result_) {
59  return simpleAssertionImpl(
60  true,
61  "the expected exception is caught",
62  false);
63  }
64  else {
65  return simpleAssertionImpl(
66  false,
67  "an exception is expected but no one has occurred",
68  false);
69  }
70 }
71 
72 } /* -- namespace OTest2 */
OTest2::AssertBean::getCondition
bool getCondition() const
Get assertion condition.
Definition: assertbean.cpp:54
OTest2::AssertBean
A helper class packing assertion condition and an assertion message together.
Definition: assertbean.h:31
OTest2::AssertBean::getMessage
const std::string & getMessage() const
Get assertion message.
Definition: assertbean.cpp:58
OTest2::GenericAssertion::testException
bool testException(std::function< bool()> ftor_)
Definition: assertions.cpp:46
OTest2
Definition: assertbean.h:25
OTest2::AssertContext::simpleAssertionImpl
bool simpleAssertionImpl(bool condition_, const std::string &message_, bool print_expression_)
Implementation of a simple assertion.
Definition: assertcontext.cpp:58
OTest2::GenericAssertion::testAssert
bool testAssert(bool condition_)
Definition: assertions.cpp:30
exccatcher.h
context.h
OTest2::Context
OTest2 runtime context.
Definition: context.h:38
assertbean.h
OTest2::AssertContext::otest2Context
const Context & otest2Context() const
Get the OTest2 context.
Definition: assertcontext.cpp:78
assertions.h