28 #include "Assertion.h" 41 template <
typename A,
typename B>
42 void printAssertionMessage(
bool ok,
const char* file, uint16_t line,
43 const A& lhs,
const char *opName,
const B& rhs) {
51 printer->print(
"Assertion ");
52 printer->print(ok ?
"passed" :
"failed");
53 printer->print(
": (");
56 printer->print(opName);
61 printer->print(
", file ");
63 printer->print(
", line ");
65 printer->println(
'.');
71 void printAssertionMessage(
bool ok,
const char* file, uint16_t line,
72 bool lhs,
const char *opName,
bool rhs) {
76 printer->print(
"Assertion ");
77 printer->print(ok ?
"passed" :
"failed");
78 printer->print(
": (");
79 printer->print(lhs ?
"true" :
"false");
81 printer->print(opName);
83 printer->print(rhs ?
"true" :
"false");
85 printer->print(
", file ");
87 printer->print(
", line ");
89 printer->println(
'.');
96 void printAssertionBoolMessage(
bool ok,
const char* file, uint16_t line,
97 bool arg,
bool value) {
101 printer->print(
"Assertion ");
102 printer->print(ok ?
"passed" :
"failed");
103 printer->print(
": (");
104 printer->print(arg ?
"true" :
"false");
105 printer->print(
") is ");
106 printer->print(value ?
"true" :
"false");
107 printer->print(
", file ");
108 printer->print(file);
109 printer->print(
", line ");
110 printer->print(line);
111 printer->println(
'.');
121 bool Assertion::assertionBool(
const char* file, uint16_t line,
bool arg,
123 if (
isDone())
return false;
124 bool ok = (arg == value);
126 printAssertionBoolMessage(ok, file, line, arg, value);
132 bool Assertion::assertion(
const char* file, uint16_t line,
bool lhs,
133 const char* opName,
bool (*op)(
bool lhs,
bool rhs),
135 if (
isDone())
return false;
136 bool ok = op(lhs, rhs);
138 printAssertionMessage(ok, file, line, lhs, opName, rhs);
144 bool Assertion::assertion(
const char* file, uint16_t line,
char lhs,
145 const char* opName,
bool (*op)(
char lhs,
char rhs),
147 if (
isDone())
return false;
148 bool ok = op(lhs, rhs);
150 printAssertionMessage(ok, file, line, lhs, opName, rhs);
156 bool Assertion::assertion(
const char* file, uint16_t line,
int lhs,
157 const char* opName,
bool (*op)(
int lhs,
int rhs),
159 if (
isDone())
return false;
160 bool ok = op(lhs, rhs);
162 printAssertionMessage(ok, file, line, lhs, opName, rhs);
168 bool Assertion::assertion(
const char* file, uint16_t line,
unsigned int lhs,
169 const char* opName,
bool (*op)(
unsigned int lhs,
unsigned int rhs),
171 if (
isDone())
return false;
172 bool ok = op(lhs, rhs);
174 printAssertionMessage(ok, file, line, lhs, opName, rhs);
180 bool Assertion::assertion(
const char* file, uint16_t line,
long lhs,
181 const char* opName,
bool (*op)(
long lhs,
long rhs),
183 if (
isDone())
return false;
184 bool ok = op(lhs, rhs);
186 printAssertionMessage(ok, file, line, lhs, opName, rhs);
192 bool Assertion::assertion(
const char* file, uint16_t line,
unsigned long lhs,
193 const char* opName,
bool (*op)(
unsigned long lhs,
unsigned long rhs),
195 if (
isDone())
return false;
196 bool ok = op(lhs, rhs);
198 printAssertionMessage(ok, file, line, lhs, opName, rhs);
204 bool Assertion::assertion(
const char* file, uint16_t line,
double lhs,
205 const char* opName,
bool (*op)(
double lhs,
double rhs),
207 if (
isDone())
return false;
208 bool ok = op(lhs, rhs);
210 printAssertionMessage(ok, file, line, lhs, opName, rhs);
216 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
217 const char* opName,
bool (*op)(
const char* lhs,
const char* rhs),
219 if (
isDone())
return false;
220 bool ok = op(lhs, rhs);
222 printAssertionMessage(ok, file, line, lhs, opName, rhs);
228 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
229 const char *opName,
bool (*op)(
const char* lhs,
const String& rhs),
231 if (
isDone())
return false;
232 bool ok = op(lhs, rhs);
234 printAssertionMessage(ok, file, line, lhs, opName, rhs);
240 bool Assertion::assertion(
const char* file, uint16_t line,
const char* lhs,
242 bool (*op)(
const char* lhs,
const __FlashStringHelper* rhs),
243 const __FlashStringHelper* rhs) {
244 if (
isDone())
return false;
245 bool ok = op(lhs, rhs);
247 printAssertionMessage(ok, file, line, lhs, opName, rhs);
253 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
254 const char *opName,
bool (*op)(
const String& lhs,
const char* rhs),
256 if (
isDone())
return false;
257 bool ok = op(lhs, rhs);
259 printAssertionMessage(ok, file, line, lhs, opName, rhs);
265 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
266 const char *opName,
bool (*op)(
const String& lhs,
const String& rhs),
268 if (
isDone())
return false;
269 bool ok = op(lhs, rhs);
271 printAssertionMessage(ok, file, line, lhs, opName, rhs);
277 bool Assertion::assertion(
const char* file, uint16_t line,
const String& lhs,
279 bool (*op)(
const String& lhs,
const __FlashStringHelper* rhs),
280 const __FlashStringHelper* rhs) {
281 if (
isDone())
return false;
282 bool ok = op(lhs, rhs);
284 printAssertionMessage(ok, file, line, lhs, opName, rhs);
290 bool Assertion::assertion(
const char* file, uint16_t line,
291 const __FlashStringHelper* lhs,
const char *opName,
292 bool (*op)(
const __FlashStringHelper* lhs,
const char* rhs),
294 if (
isDone())
return false;
295 bool ok = op(lhs, rhs);
297 printAssertionMessage(ok, file, line, lhs, opName, rhs);
303 bool Assertion::assertion(
const char* file, uint16_t line,
304 const __FlashStringHelper* lhs,
const char *opName,
305 bool (*op)(
const __FlashStringHelper* lhs,
const String& rhs),
307 if (
isDone())
return false;
308 bool ok = op(lhs, rhs);
310 printAssertionMessage(ok, file, line, lhs, opName, rhs);
316 bool Assertion::assertion(
const char* file, uint16_t line,
317 const __FlashStringHelper* lhs,
const char *opName,
318 bool (*op)(
const __FlashStringHelper* lhs,
const __FlashStringHelper* rhs),
319 const __FlashStringHelper* rhs) {
320 if (
isDone())
return false;
321 bool ok = op(lhs, rhs);
323 printAssertionMessage(ok, file, line, lhs, opName, rhs);
337 template <
typename A,
typename B>
338 void printAssertionMessageVerbose(
bool ok,
const char* file,
339 uint16_t line,
const A& lhs, internal::FlashStringType lhsString,
340 const char *opName,
const B& rhs, internal::FlashStringType rhsString) {
348 printer->print(
"Assertion ");
349 printer->print(ok ?
"passed" :
"failed");
350 printer->print(
": (");
351 printer->print(lhsString);
354 printer->print(
") ");
355 printer->print(opName);
356 printer->print(
" (");
357 printer->print(rhsString);
362 printer->print(
", file ");
363 printer->print(file);
364 printer->print(
", line ");
365 printer->print(line);
366 printer->println(
'.');
372 void printAssertionMessageVerbose(
bool ok,
const char* file,
373 uint16_t line,
bool lhs, internal::FlashStringType lhsString,
374 const char *opName,
bool rhs, internal::FlashStringType rhsString) {
378 printer->print(
"Assertion ");
379 printer->print(ok ?
"passed" :
"failed");
380 printer->print(
": (");
381 printer->print(lhsString);
383 printer->print(lhs ?
"true" :
"false");
384 printer->print(
") ");
385 printer->print(opName);
386 printer->print(
" (");
387 printer->print(rhsString);
389 printer->print(rhs ?
"true" :
"false");
391 printer->print(
", file ");
392 printer->print(file);
393 printer->print(
", line ");
394 printer->print(line);
395 printer->println(
'.');
402 void printAssertionBoolMessageVerbose(
bool ok,
const char* file,
403 uint16_t line,
bool arg, internal::FlashStringType argString,
bool value) {
407 printer->print(
"Assertion ");
408 printer->print(ok ?
"passed" :
"failed");
409 printer->print(
": (");
410 printer->print(argString);
412 printer->print(arg ?
"true" :
"false");
413 printer->print(
") is ");
414 printer->print(value ?
"true" :
"false");
415 printer->print(
", file ");
416 printer->print(file);
417 printer->print(
", line ");
418 printer->print(line);
419 printer->println(
'.');
424 bool Assertion::assertionBoolVerbose(
const char* file, uint16_t line,
bool arg,
425 internal::FlashStringType argString,
bool value) {
426 if (
isDone())
return false;
427 bool ok = (arg == value);
429 printAssertionBoolMessageVerbose(ok, file, line, arg, argString, value);
435 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
bool lhs,
436 internal::FlashStringType lhsString,
const char* opName,
437 bool (*op)(
bool lhs,
bool rhs),
bool rhs,
438 internal::FlashStringType rhsString) {
439 if (
isDone())
return false;
440 bool ok = op(lhs, rhs);
442 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
449 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
char lhs,
450 internal::FlashStringType lhsString,
const char* opName,
451 bool (*op)(
char lhs,
char rhs),
char rhs,
452 internal::FlashStringType rhsString) {
453 if (
isDone())
return false;
454 bool ok = op(lhs, rhs);
456 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
463 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
int lhs,
464 internal::FlashStringType lhsString,
const char* opName,
465 bool (*op)(
int lhs,
int rhs),
int rhs,
466 internal::FlashStringType rhsString) {
467 if (
isDone())
return false;
468 bool ok = op(lhs, rhs);
470 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
477 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
478 unsigned int lhs, internal::FlashStringType lhsString,
const char* opName,
479 bool (*op)(
unsigned int lhs,
unsigned int rhs),
480 unsigned int rhs, internal::FlashStringType rhsString) {
481 if (
isDone())
return false;
482 bool ok = op(lhs, rhs);
484 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
491 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
long lhs,
492 internal::FlashStringType lhsString,
const char* opName,
493 bool (*op)(
long lhs,
long rhs),
long rhs,
494 internal::FlashStringType rhsString) {
495 if (
isDone())
return false;
496 bool ok = op(lhs, rhs);
498 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
505 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
506 unsigned long lhs, internal::FlashStringType lhsString,
const char* opName,
507 bool (*op)(
unsigned long lhs,
unsigned long rhs),
508 unsigned long rhs, internal::FlashStringType rhsString) {
509 if (
isDone())
return false;
510 bool ok = op(lhs, rhs);
512 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
519 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
double lhs,
520 internal::FlashStringType lhsString,
const char* opName,
521 bool (*op)(
double lhs,
double rhs),
double rhs,
522 internal::FlashStringType rhsString) {
523 if (
isDone())
return false;
524 bool ok = op(lhs, rhs);
526 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
533 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
534 const char* lhs, internal::FlashStringType lhsString,
const char* opName,
535 bool (*op)(
const char* lhs,
const char* rhs),
536 const char* rhs, internal::FlashStringType rhsString) {
537 if (
isDone())
return false;
538 bool ok = op(lhs, rhs);
540 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
547 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
548 const char* lhs, internal::FlashStringType lhsString,
549 const char *opName,
bool (*op)(
const char* lhs,
const String& rhs),
550 const String& rhs, internal::FlashStringType rhsString) {
551 if (
isDone())
return false;
552 bool ok = op(lhs, rhs);
554 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
561 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
562 const char* lhs, internal::FlashStringType lhsString,
const char *opName,
563 bool (*op)(
const char* lhs,
const __FlashStringHelper* rhs),
564 const __FlashStringHelper* rhs, internal::FlashStringType rhsString) {
565 if (
isDone())
return false;
566 bool ok = op(lhs, rhs);
568 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
575 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
576 const String& lhs, internal::FlashStringType lhsString,
const char *opName,
577 bool (*op)(
const String& lhs,
const char* rhs),
578 const char* rhs, internal::FlashStringType rhsString) {
579 if (
isDone())
return false;
580 bool ok = op(lhs, rhs);
582 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
589 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
590 const String& lhs, internal::FlashStringType lhsString,
const char *opName,
591 bool (*op)(
const String& lhs,
const String& rhs),
592 const String& rhs, internal::FlashStringType rhsString) {
593 if (
isDone())
return false;
594 bool ok = op(lhs, rhs);
596 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
603 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
604 const String& lhs, internal::FlashStringType lhsString,
const char *opName,
605 bool (*op)(
const String& lhs,
const __FlashStringHelper* rhs),
606 const __FlashStringHelper* rhs, internal::FlashStringType rhsString) {
607 if (
isDone())
return false;
608 bool ok = op(lhs, rhs);
610 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
617 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
618 const __FlashStringHelper* lhs, internal::FlashStringType lhsString,
620 bool (*op)(
const __FlashStringHelper* lhs,
const char* rhs),
621 const char* rhs, internal::FlashStringType rhsString) {
622 if (
isDone())
return false;
623 bool ok = op(lhs, rhs);
625 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
632 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
633 const __FlashStringHelper* lhs, internal::FlashStringType lhsString,
635 bool (*op)(
const __FlashStringHelper* lhs,
const String& rhs),
636 const String& rhs, internal::FlashStringType rhsString) {
637 if (
isDone())
return false;
638 bool ok = op(lhs, rhs);
640 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
647 bool Assertion::assertionVerbose(
const char* file, uint16_t line,
648 const __FlashStringHelper* lhs, internal::FlashStringType lhsString,
650 bool (*op)(
const __FlashStringHelper* lhs,
const __FlashStringHelper* rhs),
651 const __FlashStringHelper* rhs, internal::FlashStringType rhsString) {
652 if (
isDone())
return false;
653 bool ok = op(lhs, rhs);
655 printAssertionMessageVerbose(ok, file, line, lhs, lhsString, opName, rhs,
void setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
bool isVerbosity(uint8_t verbosity)
Determine if any of the given verbosity is enabled.
static const uint8_t kAssertionPassed
Print assertXxx() passed message.
bool isDone()
Return true if test has been asserted.
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
static const uint8_t kAssertionFailed
Print assertXxx() failed message.
Flash strings (using F() macro) on the ESP8266 platform cannot be placed in an inline context...