AUnit  0.4.0
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
MetaAssertion.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 
34 #ifndef AUNIT_META_ASSERTION_H
35 #define AUNIT_META_ASSERTION_H
36 
37 #include "Printer.h"
38 #include "Assertion.h"
39 
40 class __FlashStringHelper;
41 
42 // Meta tests, same syntax as ArduinoUnit for compatibility.
43 // The checkTestXxx() macros return a boolean, and execution continues.
44 
46 #define checkTestDone(name) (test_##name##_instance.isDone())
47 
49 #define checkTestNotDone(name) (test_##name##_instance.isNotDone())
50 
52 #define checkTestPass(name) (test_##name##_instance.isPassed())
53 
55 #define checkTestNotPass(name) (test_##name##_instance.isNotPassed())
56 
58 #define checkTestFail(name) (test_##name##_instance.isFailed())
59 
61 #define checkTestNotFail(name) (test_##name##_instance.isNotFailed())
62 
64 #define checkTestSkip(name) (test_##name##_instance.isSkipped())
65 
67 #define checkTestNotSkip(name) (test_##name##_instance.isNotSkipped())
68 
70 #define checkTestExpire(name) (test_##name##_instance.isExpired())
71 
73 #define checkTestNotExpire(name) (test_##name##_instance.isNotExpired())
74 
75 // If the assertTestXxx() macros fail, they generate an optional output, calls
76 // fail(), and returns from the current test case.
77 
79 #define assertTestDone(name) \
80  assertTestStatus(name, isDone, kMessageDone)
81 
83 #define assertTestNotDone(name) \
84  assertTestStatus(name, isNotDone, kMessageNotDone)
85 
87 #define assertTestPass(name) \
88  assertTestStatus(name, isPassed, kMessagePassed)
89 
91 #define assertTestNotPass(name) \
92  assertTestStatus(name, isNotPassed, kMessageNotPassed)
93 
95 #define assertTestFail(name) \
96  assertTestStatus(name, isFailed, kMessageFailed)
97 
99 #define assertTestNotFail(name) \
100  assertTestStatus(name, isNotFailed, kMessageNotFailed)
101 
103 #define assertTestSkip(name) \
104  assertTestStatus(name, isSkipped, kMessageSkipped)
105 
107 #define assertTestNotSkip(name) \
108  assertTestStatus(name, isNotSkipped, kMessageNotSkipped)
109 
111 #define assertTestExpire(name) \
112  assertTestStatus(name, isExpired, kMessageExpired)
113 
115 #define assertTestNotExpire(name) \
116  assertTestStatus(name, isNotExpired, kMessageNotExpired)
117 
119 #define assertTestStatus(name,method,message) do {\
120  if (!assertionTestStatus(\
121  __FILE__,__LINE__,#name,FPSTR(message),test_##name##_instance.method()))\
122  return;\
123 } while (false)
124 
125 // Meta tests for testF() and testingF() are slightly different because
126 // the name of the fixture class is appended to the instance name.
127 
129 #define checkTestDoneF(test_class,name) \
130  (test_class##_##name##_instance.isDone())
131 
133 #define checkTestNotDoneF(test_class,name) \
134  (test_class##_##name##_instance.isNotDone())
135 
137 #define checkTestPassF(test_class,name) \
138  (test_class##_##name##_instance.isPassed())
139 
141 #define checkTestNotPassF(test_class,name) \
142  (test_class##_##name##_instance.isNotPassed())
143 
145 #define checkTestFailF(test_class,name) \
146  (test_class##_##name##_instance.isFailed())
147 
149 #define checkTestNotFailF(test_class,name) \
150  (test_class##_##name##_instance.isNotFailed())
151 
153 #define checkTestSkipF(test_class,name) \
154  (test_class##_##name##_instance.isSkipped())
155 
157 #define checkTestNotSkipF(test_class,name) \
158  (test_class##_##name##_instance.isNotSkipped())
159 
161 #define checkTestExpireF(test_class,name) \
162  (test_class##_##name##_instance.isExpired())
163 
165 #define checkTestNotExpireF(test_class,name) \
166  (test_class##_##name##_instance.isNotExpired())
167 
168 // If the assertTestXxx() macros fail, they generate an optional output, calls
169 // fail(), and returns from the current test case.
170 
172 #define assertTestDoneF(test_class,name) \
173  assertTestStatusF(test_class, name, isDone, kMessageDone)
174 
176 #define assertTestNotDoneF(test_class,name) \
177  assertTestStatusF(test_class, name, isNotDone, kMessageNotDone)
178 
180 #define assertTestPassF(test_class,name) \
181  assertTestStatusF(test_class, name, isPassed, kMessagePassed)
182 
184 #define assertTestNotPassF(test_class,name) \
185  assertTestStatusF(test_class, name, isNotPassed, kMessageNotPassed)
186 
188 #define assertTestFailF(test_class,name) \
189  assertTestStatusF(test_class, name, isFailed, kMessageFailed)
190 
192 #define assertTestNotFailF(test_class,name) \
193  assertTestStatusF(test_class, name, isNotFailed, kMessageNotFailed)
194 
196 #define assertTestSkipF(test_class,name) \
197  assertTestStatusF(test_class, name, isSkipped, kMessageSkipped)
198 
200 #define assertTestNotSkipF(test_class,name) \
201  assertTestStatusF(test_class, name, isNotSkipped, kMessageNotSkipped)
202 
204 #define assertTestExpireF(test_class,name) \
205  assertTestStatusF(test_class, name, isExpired, kMessageExpired)
206 
208 #define assertTestNotExpireF(test_class,name) \
209  assertTestStatusF(test_class, name, isNotExpired, kMessageNotExpired)
210 
212 #define assertTestStatusF(test_class,name,method,message) do {\
213  if (!assertionTestStatus(\
214  __FILE__,__LINE__,#name,FPSTR(message),\
215  test_class##_##name##_instance.method()))\
216  return;\
217 } while (false)
218 
219 namespace aunit {
220 
225 class MetaAssertion: public Assertion {
226  protected:
227  // Human-readable strings for various meta-asssertion messages.
228  // They need to be protected, not private, because they are used by
229  // subclasses through the test() and testing() macros.
230  static const char kMessageDone[];
231  static const char kMessageNotDone[];
232  static const char kMessagePassed[];
233  static const char kMessageNotPassed[];
234  static const char kMessageFailed[];
235  static const char kMessageNotFailed[];
236  static const char kMessageSkipped[];
237  static const char kMessageNotSkipped[];
238  static const char kMessageExpired[];
239  static const char kMessageNotExpired[];
240 
243 
248  bool assertionTestStatus(const char* file, uint16_t line,
249  const char* testName, const __FlashStringHelper* statusMessage,
250  bool ok);
251 
254  bool ok, const char* file, uint16_t line,
255  const char* testName, const __FlashStringHelper* statusMessage);
256 
257  private:
258  // Disable copy-constructor and assignment operator
259  MetaAssertion(const MetaAssertion&) = delete;
260  MetaAssertion& operator=(const MetaAssertion&) = delete;
261 
262 };
263 
264 }
265 
266 #endif
void printAssertionTestStatusMessage(bool ok, const char *file, uint16_t line, const char *testName, const __FlashStringHelper *statusMessage)
Print the meta assertion passed or failed message.
Class that extends the Assertion class to support the checkTestXxx() and assertTestXxx() macros that ...
bool assertionTestStatus(const char *file, uint16_t line, const char *testName, const __FlashStringHelper *statusMessage, bool ok)
Set the status of the current test based on 'ok, and print assertion message if requested.
MetaAssertion()
Empty constructor.
An Assertion class is a subclass of Test and provides various overloaded assertion() functions...
Definition: Assertion.h:98
Various assertXxx() macros are defined in this header.