OTest2
A C++ testing framework
assertionsimpl.h
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 #ifndef OTest2__INCLUDE_OTEST2_ASSERTIONSIMPL_H_
20 #define OTest2__INCLUDE_OTEST2_ASSERTIONSIMPL_H_
21 
22 #include <otest2/assertions.h>
23 
24 #include <type_traits>
25 
26 #include <typeinfo>
27 #include <cxxabi.h>
28 
29 #include <otest2/assertstream.h>
30 #include <otest2/printtraits.h>
31 
32 namespace OTest2 {
33 
34 template<typename Compare_, typename A_, typename B_>
36  Compare_&& cmp_,
37  A_&& a_,
38  B_&& b_,
39  bool condition_) {
40  typedef typename AssertionParameter<A_>::Type AType_;
41  typedef typename AssertionParameter<B_>::Type BType_;
42  typedef typename std::remove_reference<Compare_>::type CmpType_;
43 
44  /* -- report the result and the used relation operator */
45  AssertStream report_(enterAssertion(condition_));
46  report_ << "check '";
47  PrintTrait<CmpType_>::print(report_, cmp_);
48  report_ << "' ";
49  if(condition_)
50  report_ << "has passed";
51  else
52  report_ << "has failed";
53  report_ << commitMsg();
54 
55  /* -- print the expression */
56  report_ << " " << assertPar(0) << " ";
57  PrintTrait<CmpType_>::print(report_, cmp_);
58  report_ << " " << assertPar(1) << commitMsg();
59 
60  /* -- print actual values */
61  report_ << "actual values:" << commitMsg();
62  report_ << " ";
63  PrintTrait<AType_>::print(report_, a_);
64  report_ << " ";
65  PrintTrait<CmpType_>::print(report_, cmp_);
66  report_ << " ";
67  PrintTrait<BType_>::print(report_, b_);
68  report_ << commitMsg();
69 
70  return report_.getResult();
71 }
72 
73 template<template<typename, typename> class Compare_, typename A_, typename B_>
75  A_&& a_,
76  B_&& b_) {
77  typedef typename AssertionParameter<A_>::Type AType_;
78  typedef typename AssertionParameter<B_>::Type BType_;
79  typedef Compare_<AType_, BType_> CmpType_;
80 
81  /* -- compare the values */
82  CmpType_ cmp_;
83  const bool condition_(cmp_(a_, b_));
84  return reportAssertion(cmp_, a_, b_, condition_);
85 }
86 
87 template<template<typename, typename, typename> class Compare_, typename A_, typename B_, typename Precision_>
89  A_ a_,
90  B_ b_,
91  Precision_ precision_) {
92  /* -- compare the values */
93  Compare_<A_, B_, Precision_> cmp_;
94  const bool condition_(cmp_(a_, b_, precision_));
95  return reportAssertion(cmp_, a_, b_, condition_);
96 }
97 
98 } /* -- namespace OTest2 */
99 
100 #endif /* OTest2__INCLUDE_OTEST2_ASSERTIONSIMPL_H_ */
OTest2::GenericAssertion::reportAssertion
bool reportAssertion(Compare_ &&cmp_, A_ &&a_, B_ &&b_, bool condition_)
Definition: assertionsimpl.h:35
OTest2::assertPar
Private::Manipulator< int > assertPar(int index_)
Print assertion parameter.
Definition: assertstream.cpp:196
assertstream.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
printtraits.h
OTest2::PrintTrait::print
static std::ostream & print(std::ostream &os_, typename TypeTrait< Type_ >::BestArg value_)
Definition: printtraits.h:39
OTest2
Definition: assertbean.h:25
OTest2::AssertionParameter::Type
NormalizeStringType< typename std::remove_const< typename std::remove_reference< Type_ >::type >::type >::Type Type
Definition: typetraits.h:84
type
TokenType type
Definition: runnerfiltertags.cpp:502
OTest2::AssertStream
Assertion stream.
Definition: assertstream.h:40
OTest2::GenericAssertion::testAssertCompareFP
bool testAssertCompareFP(A_ a_, B_ b_, Precision_ precision_=DEFAULT_FLOAT_PRECISION)
Definition: assertionsimpl.h:88
OTest2::GenericAssertion::testAssertCompare
bool testAssertCompare(A_ &&a_, B_ &&b_)
Definition: assertionsimpl.h:74
assertions.h