AUnit  0.4.0
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
Assertion.h
Go to the documentation of this file.
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 // Significant portions of the design and implementation of this file came from
26 // https://github.com/mmurdoch/arduinounit/blob/master/src/ArduinoUnit.h
27 
36 #ifndef AUNIT_ASSERTION_H
37 #define AUNIT_ASSERTION_H
38 
39 #include "Test.h"
40 
42 #define assertEqual(arg1,arg2) assertOp(arg1,aunit::compareEqual,"==",arg2)
43 
45 #define assertNotEqual(arg1,arg2) \
46  assertOp(arg1,aunit::compareNotEqual,"!=",arg2)
47 
49 #define assertLess(arg1,arg2) assertOp(arg1,aunit::compareLess,"<",arg2)
50 
52 #define assertMore(arg1,arg2) assertOp(arg1,aunit::compareMore,">",arg2)
53 
55 #define assertLessOrEqual(arg1,arg2) \
56  assertOp(arg1,aunit::compareLessOrEqual,"<=",arg2)
57 
59 #define assertMoreOrEqual(arg1,arg2) \
60  assertOp(arg1,aunit::compareMoreOrEqual,">=",arg2)
61 
63 #define assertTrue(arg) assertEqual(arg,true)
64 
66 #define assertFalse(arg) assertEqual(arg,false)
67 
69 #define assertOp(arg1,op,opName,arg2) do {\
70  if (!assertion(__FILE__,__LINE__,(arg1),opName,op,(arg2)))\
71  return;\
72 } while (false)
73 
74 class __FlashStringHelper;
75 class String;
76 
77 namespace aunit {
78 
98 class Assertion: public Test {
99  protected:
102 
104  bool isOutputEnabled(bool ok);
105 
106  // NOTE: Don't create a virtual destructor. That's the normal best practice
107  // for classes that will be used polymorphically. However, this class will
108  // never be deleted polymorphically (i.e. through its pointer) so it
109  // doesn't need a virtual destructor. In fact, adding it causes flash and
110  // static memory to increase dramatically because each test() and testing()
111  // macro creates a new subclass. AceButtonTest flash memory increases from
112  // 18928 to 20064 bytes, and static memory increases from 917 to 1055
113  // bytes.
114 
115  bool assertion(const char* file, uint16_t line, bool lhs,
116  const char* opName, bool (*op)(bool lhs, bool rhs),
117  bool rhs);
118 
119  bool assertion(const char* file, uint16_t line, char lhs,
120  const char* opName, bool (*op)(char lhs, char rhs),
121  char rhs);
122 
123  bool assertion(const char* file, uint16_t line, int lhs,
124  const char* opName, bool (*op)(int lhs, int rhs),
125  int rhs);
126 
127  bool assertion(const char* file, uint16_t line, unsigned int lhs,
128  const char* opName, bool (*op)(unsigned int lhs, unsigned int rhs),
129  unsigned int rhs);
130 
131  bool assertion(const char* file, uint16_t line, long lhs,
132  const char* opName, bool (*op)(long lhs, long rhs),
133  long rhs);
134 
135  bool assertion(const char* file, uint16_t line, unsigned long lhs,
136  const char* opName, bool (*op)(unsigned long lhs, unsigned long rhs),
137  unsigned long rhs);
138 
139  bool assertion(const char* file, uint16_t line, double lhs,
140  const char* opName, bool (*op)(double lhs, double rhs),
141  double rhs);
142 
143  bool assertion(const char* file, uint16_t line, const char* lhs,
144  const char* opName, bool (*op)(const char* lhs, const char* rhs),
145  const char* rhs);
146 
147  bool assertion(const char* file, uint16_t line, const char* lhs,
148  const char *opName, bool (*op)(const char* lhs, const String& rhs),
149  const String& rhs);
150 
151  bool assertion(const char* file, uint16_t line, const char* lhs,
152  const char *opName,
153  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
154  const __FlashStringHelper* rhs);
155 
156  bool assertion(const char* file, uint16_t line, const String& lhs,
157  const char *opName, bool (*op)(const String& lhs, const char* rhs),
158  const char* rhs);
159 
160  bool assertion(const char* file, uint16_t line, const String& lhs,
161  const char *opName, bool (*op)(const String& lhs, const String& rhs),
162  const String& rhs);
163 
164  bool assertion(const char* file, uint16_t line, const String& lhs,
165  const char *opName,
166  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
167  const __FlashStringHelper* rhs);
168 
169  bool assertion(const char* file, uint16_t line,
170  const __FlashStringHelper* lhs, const char *opName,
171  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
172  const char* rhs);
173 
174  bool assertion(const char* file, uint16_t line,
175  const __FlashStringHelper* lhs, const char *opName,
176  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
177  const String& rhs);
178 
179  bool assertion(const char* file, uint16_t line,
180  const __FlashStringHelper* lhs, const char *opName,
181  bool (*op)(const __FlashStringHelper* lhs,
182  const __FlashStringHelper* rhs),
183  const __FlashStringHelper* rhs);
184 
185  private:
186  // Disable copy-constructor and assignment operator
187  Assertion(const Assertion&) = delete;
188  Assertion& operator=(const Assertion&) = delete;
189 };
190 
191 }
192 
193 #endif
Base class of all test cases.
Definition: Test.h:49
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:67
An Assertion class is a subclass of Test and provides various overloaded assertion() functions...
Definition: Assertion.h:98
Assertion()
Empty constructor.
Definition: Assertion.h:101