AUnit  0.4.0
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
Test.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 #ifdef ESP8266
26 #include <pgmspace.h>
27 #else
28 #include <avr/pgmspace.h>
29 #endif
30 
31 #include <Arduino.h> // for declaration of 'Serial' on Teensy and others
32 #include "Verbosity.h"
33 #include "Printer.h"
34 #include "Compare.h"
35 #include "Test.h"
36 
37 namespace aunit {
38 
39 static const char TEST_STRING[] PROGMEM = "Test ";
40 static const __FlashStringHelper* TEST_STRING_F = FPSTR(TEST_STRING);
41 
42 // Use a static variable inside a function to solve the static initialization
43 // ordering problem.
45  static Test* root;
46  return &root;
47 }
48 
50  mStatus(kStatusNew),
51  mVerbosity(Verbosity::kNone),
52  mNext(nullptr) {
53 }
54 
55 // Resolve the status as kStatusFailed only if ok == false. Otherwise, keep the
56 // status as kStatusSetup to allow testing() test cases to continue.
57 void Test::setPassOrFail(bool ok) {
58  if (!ok) {
59  mStatus = kStatusFailed;
60  }
61 }
62 
63 // Insert the current test case into the singly linked list, sorted by
64 // getName(). This is an O(N^2) algorithm, but should be good enough for
65 // small N ~= 100. If N becomes bigger than that, it's probably better to insert
66 // using an O(N) algorithm, then sort the elements later in TestRunner::run().
67 // Also, we don't increment a static counter here, because that would introduce
68 // another static initialization ordering problem.
69 void Test::insert() {
70  // Find the element p whose p->next sorts after the current test
71  Test** p = getRoot();
72  while (*p != nullptr) {
73  if (compareString(getName(), (*p)->getName()) < 0) break;
74  p = &(*p)->mNext;
75  }
76  mNext = *p;
77  *p = this;
78 }
79 
80 void Test::resolve() {
81  if (!isVerbosity(Verbosity::kTestAll)) return;
82 
83  Print* printer = Printer::getPrinter();
84  if (mStatus == Test::kStatusPassed
86  printer->print(TEST_STRING_F);
87  Printer::print(mName);
88  printer->println(F(" passed."));
89  } else if (mStatus == Test::kStatusFailed
91  printer->print(TEST_STRING_F);
92  Printer::print(mName);
93  printer->println(F(" failed."));
94  } else if (mStatus == Test::kStatusSkipped
96  printer->print(TEST_STRING_F);
97  Printer::print(mName);
98  printer->println(F(" skipped."));
99  } else if (mStatus == Test::kStatusExpired
101  printer->print(TEST_STRING_F);
102  Printer::print(mName);
103  printer->println(F(" timed out."));
104  }
105 }
106 
107 }
Base class of all test cases.
Definition: Test.h:49
static const uint8_t kTestExpired
Print test timed out message.
Definition: Verbosity.h:55
static const uint8_t kStatusFailed
Test has failed, or failed() was called.
Definition: Test.h:64
static void print(const FCString &s)
Convenience method for printing an FCString.
Definition: Printer.cpp:33
Utility class to hold the Verbosity constants.
Definition: Verbosity.h:37
void setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
Definition: Test.cpp:57
void resolve()
Print out the summary of the current test.
Definition: Test.cpp:80
static const uint8_t kTestPassed
Print test passed message.
Definition: Verbosity.h:46
static const uint8_t kStatusPassed
Test has passed, or pass() was called.
Definition: Test.h:61
Test()
Empty constructor.
Definition: Test.cpp:49
static const uint8_t kStatusSkipped
Test is skipped, through the exclude() method or skip() was called.
Definition: Test.h:67
static Test ** getRoot()
Get the pointer to the root pointer.
Definition: Test.cpp:44
bool isVerbosity(uint8_t verbosity)
Determine if any of the given verbosity is enabled.
Definition: Test.h:181
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:50
static const uint8_t kStatusExpired
Test has timed out, or expire() called.
Definition: Test.h:70
static const uint8_t kTestSkipped
Print test skipped message.
Definition: Verbosity.h:52
const FCString & getName()
Get the name of the test.
Definition: Test.h:99
static const uint8_t kTestAll
Print all test status messages.
Definition: Verbosity.h:65
static const uint8_t kTestFailed
Print test failed message.
Definition: Verbosity.h:49