AUnit  1.5.5
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
TestRunner.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 <Arduino.h> // 'Serial' or SERIAL_PORT_MONITOR
26 #include <string.h>
27 #include <stdint.h>
28 #include "FCString.h"
29 #include "Compare.h"
30 #include "Printer.h"
31 #include "Verbosity.h"
32 #include "Test.h"
33 #include "TestRunner.h"
34 #include "string_util.h"
35 
36 namespace aunit {
37 
38 // Use a function static singleton to avoid the static initialization ordering
39 // problem. It's probably not an issue right now, since TestRunner is expected
40 // to be called only after all static initialization, but future refactoring
41 // could change that so this is defensive programming.
42 TestRunner* TestRunner::getRunner() {
43  static TestRunner singletonRunner;
44  return &singletonRunner;
45 }
46 
47 void TestRunner::setPrinter(Print* printer) {
48  Printer::setPrinter(printer);
49 }
50 
51 void TestRunner::setLifeCycleMatchingPattern(const char* pattern,
52  uint8_t lifeCycle) {
53  size_t length = strlen(pattern);
54  if (length > 0 && pattern[length - 1] == '*') {
55  // prefix match
56  length--;
57  } else {
58  // exact match
59  length++;
60  }
61 
62  for (Test** p = Test::getRoot(); *p != nullptr; p = (*p)->getNext()) {
63  if ((*p)->getName().compareToN(pattern, length) == 0) {
64  (*p)->setLifeCycle(lifeCycle);
65  }
66  }
67 }
68 
69 void TestRunner::setLifeCycleMatchingPattern(const char* testClass,
70  const char* pattern, uint8_t lifeCycle) {
71  // The effective pattern is the join of testClass and pattern with a '_'
72  // delimiter. This must match the algorithm used by testF() and testingF().
73  // We use string_join() instead of String so that AUnit can avoid a direct
74  // dependency on the String class. AUnit thus avoids allocating any memory on
75  // the heap.
76  char fullPattern[kMaxPatternLength];
77  bool status = internal::string_join(fullPattern, kMaxPatternLength, '_',
78  testClass, pattern);
79  if (!status) return;
80 
81  setLifeCycleMatchingPattern(fullPattern, lifeCycle);
82 }
83 
84 // Count the number of tests in TestRunner instead of Test::insert() to avoid
85 // another C++ static initialization ordering problem.
86 uint16_t TestRunner::countTests() {
87  uint16_t count = 0;
88  for (Test** p = Test::getRoot(); *p != nullptr; p = (*p)->getNext()) {
89  count++;
90  }
91  return count;
92 }
93 
94 namespace {
95 
101 void printSeconds(Print* printer, unsigned long timeMillis) {
102  int s = timeMillis / 1000;
103  int ms = timeMillis % 1000;
104  printer->print(s);
105  printer->print('.');
106  if (ms < 100) printer->print('0');
107  if (ms < 10) printer->print('0');
108  printer->print(ms);
109 }
110 
111 }
112 
113 void TestRunner::printStartRunner() const {
115 
116  Print* printer = Printer::getPrinter();
117  printer->print(F("TestRunner started on "));
118  printer->print(mCount);
119  printer->println(F(" test(s)."));
120 }
121 
122 void TestRunner::resolveRun() const {
124  Print* printer = Printer::getPrinter();
125 
126  unsigned long elapsedTime = mEndTime - mStartTime;
127  printer->print(F("TestRunner duration: "));
128  printSeconds(printer, elapsedTime);
129  printer->println(" seconds.");
130 
131  printer->print(F("TestRunner summary: "));
132  printer->print(mPassedCount);
133  printer->print(F(" passed, "));
134  printer->print(mFailedCount);
135  printer->print(F(" failed, "));
136  printer->print(mSkippedCount);
137  printer->print(F(" skipped, "));
138  printer->print(mExpiredCount);
139  printer->print(F(" timed out, out of "));
140  printer->print(mCount);
141  printer->println(F(" test(s)."));
142 }
143 
144 void TestRunner::setRunnerTimeout(TimeoutType timeout) {
145  mTimeout = timeout;
146 }
147 
148 }
Compare.h
aunit::TestRunner::setPrinter
static void setPrinter(Print *printer)
Set the output printer.
Definition: TestRunner.cpp:47
aunit::Printer::getPrinter
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:48
aunit::Verbosity::kTestRunSummary
static const uint8_t kTestRunSummary
Print TestRunner summary message.
Definition: Verbosity.h:58
aunit::TestRunner::isVerbosity
static bool isVerbosity(uint8_t verbosity)
Returns true if ANY of the bit flags of 'verbosity' is set.
Definition: TestRunner.h:111
aunit::Test::getNext
Test ** getNext()
Return the next pointer as a pointer to the pointer, similar to getRoot().
Definition: Test.h:188
aunit::Test::getRoot
static Test ** getRoot()
Get the pointer to the root pointer.
Definition: Test.cpp:36
aunit::Printer::setPrinter
static void setPrinter(Print *printer)
Set the printer.
Definition: Printer.h:51