AUnit  0.3.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit.
MetaAssertion.cpp
1 #include "Printer.h"
2 #include "Verbosity.h"
3 #include "Compare.h"
4 #include "MetaAssertion.h"
5 
6 namespace aunit {
7 
8 // Print a human readable string fragment from the method name:
9 // "isDone" -> "is done"
10 // "isNotDone" ->"is not done"
11 // ...
12 // "isExpired" ->"is timed out" (*)
13 // "isNotExpired" ->"is not timed out" (*)
14 //
15 // (*) - The internal "expired" state is consistently known as "timed out"
16 // externally. Doing a string compare is a bit inefficient in terms of CPU
17 // cycle, but these are unit tests, we don't need to be super fast.
18 //
19 // I can think of 2 alternatives to do this internal to external mapping:
20 // 1) provide the human-readable mapping in the various assertTestXxx()
21 // macros,
22 // 2) maybe use a pointer to member functions and do a switch on that here.
23 // Either of those seems more complex than this little hack.
24 void printStatusString(const char* statusName) {
25  const char* p;
26  if (compareEqual(statusName, F("isExpired"))) {
27  p = "isTimedOut"; // can't use F() here without a lot of work
28  } else if (compareEqual(statusName, F("isNotExpired"))) {
29  p = "isNotTimedOut"; // can't use F() here without a lot of work
30  } else {
31  p = statusName;
32  }
33 
34  for (; *p != '\0'; p++) {
35  char c = *p;
36  if (!isupper(c)) {
37  Printer::getPrinter()->print(c);
38  } else {
39  Printer::getPrinter()->print(' ');
40  c = tolower(c);
41  Printer::getPrinter()->print(c);
42  }
43  }
44 }
45 
46 void printAssertionTestStatusMessage(bool ok, const char* file, uint16_t line,
47  const char* testName, const char* statusName) {
48  bool isOutput =
51  if (!isOutput) return;
52 
53  // TODO: Merge the common strings between this and printAssertionMesssage()
54  // into PROGMEM strings manually and reused them. It's not too bad even with
55  // these c-strings, because the compiler will dedupe them.
56  Print* printer = Printer::getPrinter();
57  printer->print("Assertion ");
58  printer->print(ok ? "passed" : "failed");
59  printer->print(": Test ");
60  printer->print(testName);
61  printer->print(' ');
62  printStatusString(statusName);
63  printer->print(", file ");
64  printer->print(file);
65  printer->print(", line ");
66  printer->print(line);
67  printer->println('.');
68 }
69 
70 bool assertionTestStatus(const char* file, uint16_t line,
71  const char* testName, const char* statusName, bool ok) {
72  printAssertionTestStatusMessage(ok, file, line, testName, statusName);
74  return ok;
75 }
76 
77 }
Various assertTestXxx() and checkTestXxx() macros are defined in this header.
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
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 'verbosity' is set.
Definition: TestRunner.h:71