AUnit  0.3.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit.
Assertion.cpp
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 #include "Assertion.h"
26 
27 namespace aunit {
28 
29 // This can be a template function because it is accessed only through the
30 // various assertXxx() methods. Those assertXxx() methods are explicitly
31 // overloaded for the various types that we want to support.
32 //
33 // Prints something like the following:
34 // Assertion failed: (5) == (6), file Test.ino, line 820.
35 // Assertion passed: (6) == (6), file Test.ino, line 820.
36 template <typename A, typename B>
37 void printAssertionMessage(bool ok, const char* file, uint16_t line,
38  const A& lhs, const char *opName, const B& rhs) {
39 
40  bool isOutput =
43  if (!isOutput) return;
44 
45  // Don't use F() strings here because flash memory strings are not deduped by
46  // the compiler, so each template instantiation of this method causes a
47  // duplication of all the strings below. See
48  // https://github.com/mmurdoch/arduinounit/issues/70
49  // for more info.
50  Print* printer = Printer::getPrinter();
51  printer->print("Assertion ");
52  printer->print(ok ? "passed" : "failed");
53  printer->print(": (");
54  printer->print(lhs);
55  printer->print(") ");
56  printer->print(opName);
57  printer->print(" (");
58  printer->print(rhs);
59  printer->print("), file ");
60  printer->print(file);
61  printer->print(", line ");
62  printer->print(line);
63  printer->println('.');
64 }
65 
66 bool assertion(const char* file, uint16_t line, bool lhs,
67  const char* opName, bool (*op)(bool lhs, bool rhs),
68  bool rhs) {
69  bool ok = op(lhs, rhs);
70  printAssertionMessage(ok, file, line, lhs, opName, rhs);
72  return ok;
73 }
74 
75 bool assertion(const char* file, uint16_t line, char lhs,
76  const char* opName, bool (*op)(char lhs, char rhs),
77  char rhs) {
78  bool ok = op(lhs, rhs);
79  printAssertionMessage(ok, file, line, lhs, opName, rhs);
81  return ok;
82 }
83 
84 bool assertion(const char* file, uint16_t line, int lhs,
85  const char* opName, bool (*op)(int lhs, int rhs),
86  int rhs) {
87  bool ok = op(lhs, rhs);
88  printAssertionMessage(ok, file, line, lhs, opName, rhs);
90  return ok;
91 }
92 
93 bool assertion(const char* file, uint16_t line, unsigned int lhs,
94  const char* opName, bool (*op)(unsigned int lhs, unsigned int rhs),
95  unsigned int rhs) {
96  bool ok = op(lhs, rhs);
97  printAssertionMessage(ok, file, line, lhs, opName, rhs);
99  return ok;
100 }
101 
102 bool assertion(const char* file, uint16_t line, long lhs,
103  const char* opName, bool (*op)(long lhs, long rhs),
104  long rhs) {
105  bool ok = op(lhs, rhs);
106  printAssertionMessage(ok, file, line, lhs, opName, rhs);
108  return ok;
109 }
110 
111 bool assertion(const char* file, uint16_t line, unsigned long lhs,
112  const char* opName, bool (*op)(unsigned long lhs, unsigned long rhs),
113  unsigned long rhs) {
114  bool ok = op(lhs, rhs);
115  printAssertionMessage(ok, file, line, lhs, opName, rhs);
117  return ok;
118 }
119 
120 bool assertion(const char* file, uint16_t line, double lhs,
121  const char* opName, bool (*op)(double lhs, double rhs),
122  double rhs) {
123  bool ok = op(lhs, rhs);
124  printAssertionMessage(ok, file, line, lhs, opName, rhs);
126  return ok;
127 }
128 
129 bool assertion(const char* file, uint16_t line, const char* lhs,
130  const char* opName, bool (*op)(const char* lhs, const char* rhs),
131  const char* rhs) {
132  bool ok = op(lhs, rhs);
133  printAssertionMessage(ok, file, line, lhs, opName, rhs);
135  return ok;
136 }
137 
138 bool assertion(const char* file, uint16_t line, const char* lhs,
139  const char *opName, bool (*op)(const char* lhs, const String& rhs),
140  const String& rhs) {
141  bool ok = op(lhs, rhs);
142  printAssertionMessage(ok, file, line, lhs, opName, rhs);
144  return ok;
145 }
146 
147 bool assertion(const char* file, uint16_t line, const char* lhs,
148  const char *opName,
149  bool (*op)(const char* lhs, const __FlashStringHelper* rhs),
150  const __FlashStringHelper* rhs) {
151  bool ok = op(lhs, rhs);
152  printAssertionMessage(ok, file, line, lhs, opName, rhs);
154  return ok;
155 }
156 
157 bool assertion(const char* file, uint16_t line, const String& lhs,
158  const char *opName, bool (*op)(const String& lhs, const char* rhs),
159  const char* rhs) {
160  bool ok = op(lhs, rhs);
161  printAssertionMessage(ok, file, line, lhs, opName, rhs);
163  return ok;
164 }
165 
166 bool assertion(const char* file, uint16_t line, const String& lhs,
167  const char *opName, bool (*op)(const String& lhs, const String& rhs),
168  const String& rhs) {
169  bool ok = op(lhs, rhs);
170  printAssertionMessage(ok, file, line, lhs, opName, rhs);
172  return ok;
173 }
174 
175 bool assertion(const char* file, uint16_t line, const String& lhs,
176  const char *opName,
177  bool (*op)(const String& lhs, const __FlashStringHelper* rhs),
178  const __FlashStringHelper* rhs) {
179  bool ok = op(lhs, rhs);
180  printAssertionMessage(ok, file, line, lhs, opName, rhs);
182  return ok;
183 }
184 
185 bool assertion(const char* file, uint16_t line,
186  const __FlashStringHelper* lhs, const char *opName,
187  bool (*op)(const __FlashStringHelper* lhs, const char* rhs),
188  const char* rhs) {
189  bool ok = op(lhs, rhs);
190  printAssertionMessage(ok, file, line, lhs, opName, rhs);
192  return ok;
193 }
194 
195 bool assertion(const char* file, uint16_t line,
196  const __FlashStringHelper* lhs, const char *opName,
197  bool (*op)(const __FlashStringHelper* lhs, const String& rhs),
198  const String& rhs) {
199  bool ok = op(lhs, rhs);
200  printAssertionMessage(ok, file, line, lhs, opName, rhs);
202  return ok;
203 }
204 
205 bool assertion(const char* file, uint16_t line,
206  const __FlashStringHelper* lhs, const char *opName,
207  bool (*op)(const __FlashStringHelper* lhs, const __FlashStringHelper* rhs),
208  const __FlashStringHelper* rhs) {
209  bool ok = op(lhs, rhs);
210  printAssertionMessage(ok, file, line, lhs, opName, rhs);
212  return ok;
213 }
214 
215 }
static const uint8_t kAssertionPassed
Print assertXxx() passed message.
Definition: Verbosity.h:40
static void setPassOrFail(bool ok)
Set the pass/fail status of the current test.
Definition: TestRunner.h:79
Various assertXxx() macros are defined in this header.
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:50
static const uint8_t kAssertionFailed
Print assertXxx() failed message.
Definition: Verbosity.h:43
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of &#39;verbosity&#39; is set.
Definition: TestRunner.h:71