TestBase.h
Go to the documentation of this file.
1 /****
2  * TestBase.h
3  *
4  * Copyright 2019 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the SmingTest Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: 2018 - Mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include <WString.h>
25 #include <debug_progmem.h>
26 
30 class TestBase
31 {
32 public:
36  struct TestParam {
37  const char* expr;
40  bool verbose;
41  };
42 
43  virtual ~TestBase()
44  {
45  }
46 
53  virtual bool testVerify(bool res, const TestParam& param);
54 
55  bool test_verify(bool res, const char* expr, const String& value1, const String& value2, bool verbose)
56  {
57  return testVerify(res, TestParam{expr, value1, value2, verbose});
58  }
59 
60  template <typename V>
61  typename std::enable_if<std::is_arithmetic<V>::value, bool>::type
62  test_verify(bool res, const char* expr, const V& value1, const V& value2, bool verbose)
63  {
64  return testVerify(res, TestParam{expr, String(value1), String(value2), verbose});
65  }
66 
67  template <typename V>
68  typename std::enable_if<!std::is_same<V, String>::value && !std::is_arithmetic<V>::value, bool>::type
69  test_verify(bool res, const char* expr, const V& value1, const V& value2, bool verbose)
70  {
71  return testVerify(res, TestParam{expr, toString(value1), toString(value2), verbose});
72  }
73 
74  virtual void fail(const char* func)
75  {
76  m_printf(_F("FAIL in `%s`\r\n"), func);
77  }
78 };
79 
85 #define INTERNAL_CHECK(expr, verbose) INTERNAL_CHECK2(expr, expr, verbose)
86 
87 #define INTERNAL_CHECK2(res, expr, verbose) \
88  do { \
89  PSTR_ARRAY(tmpExprStr, #expr); \
90  if(!testVerify(bool(res), TestParam{tmpExprStr, nullptr, nullptr, verbose})) { \
91  TEST_ASSERT(false); \
92  } \
93  } while(0)
94 
95 #define INTERNAL_CHECK_EQ(a, b, verbose) \
96  do { \
97  PSTR_ARRAY(tmpExprStr, #a " == " #b); \
98  const auto value_a = a; \
99  const decltype(value_a) value_b = b; \
100  if(!test_verify(value_a == value_b, tmpExprStr, value_a, value_b, verbose)) { \
101  TEST_ASSERT(false); \
102  } \
103  } while(0)
104 
105 #define INTERNAL_CHECK_NEQ(a, b, verbose) \
106  do { \
107  PSTR_ARRAY(tmpExprStr, #a " != " #b); \
108  const auto value_a = a; \
109  const decltype(value_a) value_b = b; \
110  if(!test_verify(value_a != value_b, tmpExprStr, value_a, value_b, verbose)) { \
111  TEST_ASSERT(false); \
112  } \
113  } while(0)
114 
126 #define CHECK(expr) INTERNAL_CHECK(expr, false)
127 
133 #define CHECK2(res, expr) INTERNAL_CHECK2(res, expr, false)
134 
140 #define CHECK_EQ(a, b) INTERNAL_CHECK_EQ(a, b, false)
141 
147 #define CHECK_NEQ(a, b) INTERNAL_CHECK_NEQ(a, b, false)
148 
160 #define REQUIRE(expr) INTERNAL_CHECK(expr, true)
161 
167 #define REQUIRE2(res, expr) INTERNAL_CHECK2(res, expr, true)
168 
174 #define REQUIRE_EQ(a, b) INTERNAL_CHECK_EQ(a, b, true)
175 
181 #define REQUIRE_NEQ(a, b) INTERNAL_CHECK_NEQ(a, b, true)
182 
190 #define TEST_ASSERT(result) \
191  if(!(result)) { \
192  fail(__PRETTY_FUNCTION__); \
193  assert(false); \
194  }
std::enable_if< std::is_integral< T >::value, String >::type toString(T value)
Definition: BitSet.h:481
The String class.
Definition: WString.h:137
Base class supporting verification for test assertions.
Definition: TestBase.h:31
virtual ~TestBase()
Definition: TestBase.h:43
std::enable_if< std::is_arithmetic< V >::value, bool >::type test_verify(bool res, const char *expr, const V &value1, const V &value2, bool verbose)
Definition: TestBase.h:62
std::enable_if<!std::is_same< V, String >::value &&!std::is_arithmetic< V >::value, bool >::type test_verify(bool res, const char *expr, const V &value1, const V &value2, bool verbose)
Definition: TestBase.h:69
bool test_verify(bool res, const char *expr, const String &value1, const String &value2, bool verbose)
Definition: TestBase.h:55
virtual bool testVerify(bool res, const TestParam &param)
Print result of a test.
virtual void fail(const char *func)
Definition: TestBase.h:74
#define _F(str)
Definition: FakePgmSpace.h:97
int m_printf(char const *,...)
Contains details for test verification.
Definition: TestBase.h:36
bool verbose
true to always emit message, false only for errors
Definition: TestBase.h:40
String value2
Second value in comparison (optional)
Definition: TestBase.h:39
const char * expr
Text of expression generated by the macro.
Definition: TestBase.h:37
String value1
First value in comparison, or expected result.
Definition: TestBase.h:38