AUnit  0.4.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
Test.h
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 // Significant portions of the design and implementation of this file came from
26 // https://github.com/mmurdoch/arduinounit/blob/master/src/ArduinoUnit.h
27 
28 #ifndef AUNIT_TEST_H
29 #define AUNIT_TEST_H
30 
31 #include <stdint.h>
32 #include "FCString.h"
33 #include "Verbosity.h"
34 
35 // Defined in ESP8266, not defined in AVR or Teensy
36 #ifndef FPSTR
37 #define FPSTR(pstr_pointer) \
38  (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
39 #endif
40 
41 namespace aunit {
42 
49 class Test {
50  public:
51  // Don't change the order of Passed, Failed, Skipped or Expired without
52  // looking at the isDone() method.
53 
55  static const uint8_t kStatusNew = 0;
56 
58  static const uint8_t kStatusSetup = 1;
59 
61  static const uint8_t kStatusPassed = 2;
62 
64  static const uint8_t kStatusFailed = 3;
65 
67  static const uint8_t kStatusSkipped = 4;
68 
70  static const uint8_t kStatusExpired = 5;
71 
77  static Test** getRoot();
78 
80  Test();
81 
89  virtual void setup() {}
90 
98  virtual void teardown() {}
99 
105  virtual void loop() = 0;
106 
108  void resolve();
109 
111  const FCString& getName() { return mName; }
112 
114  uint8_t getStatus() { return mStatus; }
115 
117  void setStatus(uint8_t status) { mStatus = status; }
118 
120  void setPassOrFail(bool ok);
121 
127  Test** getNext() { return &mNext; }
128 
130  bool isDone() { return mStatus >= kStatusPassed; }
131 
133  bool isNotDone() { return !isDone(); }
134 
136  bool isPassed() { return mStatus == kStatusPassed; }
137 
139  bool isNotPassed() { return !isPassed(); }
140 
142  bool isFailed() { return mStatus == kStatusFailed; }
143 
145  bool isNotFailed() { return !isFailed(); }
146 
148  bool isSkipped() { return mStatus == kStatusSkipped; }
149 
151  bool isNotSkipped() { return !isSkipped(); }
152 
154  bool isExpired() { return mStatus == kStatusExpired; }
155 
157  bool isNotExpired() { return !isExpired(); }
158 
160  void skip() { mStatus = kStatusSkipped; }
161 
163  void expire() { mStatus = kStatusExpired; }
164 
166  void enableVerbosity(uint8_t verbosity) { mVerbosity |= verbosity; }
167 
169  void disableVerbosity(uint8_t verbosity) { mVerbosity &= ~verbosity; }
170 
171  protected:
173  void fail() { mStatus = kStatusFailed; }
174 
176  void pass() { mStatus = kStatusPassed; }
177 
178  void init(const char* name) {
179  mName = FCString(name);
180  mStatus = kStatusNew;
181  mVerbosity = 0;
182  insert();
183  }
184 
185  void init(const __FlashStringHelper* name) {
186  mName = FCString(name);
187  mStatus = kStatusNew;
188  mVerbosity = 0;
189  insert();
190  }
191 
193  bool isVerbosity(uint8_t verbosity) { return mVerbosity & verbosity; }
194 
196  uint8_t getVerbosity() { return mVerbosity; }
197 
198  private:
199  // Disable copy-constructor and assignment operator
200  Test(const Test&) = delete;
201  Test& operator=(const Test&) = delete;
202 
204  void insert();
205 
206  FCString mName;
207  uint8_t mStatus;
208  uint8_t mVerbosity;
209  Test* mNext;
210 };
211 
212 }
213 
214 #endif
void disableVerbosity(uint8_t verbosity)
Disable the given verbosity of the current test.
Definition: Test.h:169
Base class of all test cases.
Definition: Test.h:49
void expire()
Mark the test as expired (i.e.
Definition: Test.h:163
static const uint8_t kStatusFailed
Test has failed, or failed() was called.
Definition: Test.h:64
static const uint8_t kStatusNew
Test is new, needs to be setup.
Definition: Test.h:55
void fail()
Mark the test as failed.
Definition: Test.h:173
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
bool isNotDone()
Return true if test is done (passed, failed, skipped, expired).
Definition: Test.h:133
static const uint8_t kStatusPassed
Test has passed, or pass() was called.
Definition: Test.h:61
bool isNotPassed()
Return true if test is passed.
Definition: Test.h:139
uint8_t getVerbosity()
Get the verbosity.
Definition: Test.h:196
Test()
Empty constructor.
Definition: Test.cpp:49
Test ** getNext()
Return the next pointer as a pointer to the pointer, similar to getRoot().
Definition: Test.h:127
static const uint8_t kStatusSkipped
Test is skipped, through the exclude() method or skip() was called.
Definition: Test.h:67
bool isPassed()
Return true if test is passed.
Definition: Test.h:136
virtual void setup()
Optional method that performs any initialization.
Definition: Test.h:89
virtual void loop()=0
The user-provided test case function.
void pass()
Mark the test as passed.
Definition: Test.h:176
void enableVerbosity(uint8_t verbosity)
Enable the given verbosity of the current test.
Definition: Test.h:166
static Test ** getRoot()
Get the pointer to the root pointer.
Definition: Test.cpp:44
bool isExpired()
Return true if test is expired.
Definition: Test.h:154
A union of (const char*) and (const __FlashStringHelper*) with a discriminator.
Definition: FCString.h:50
bool isVerbosity(uint8_t verbosity)
Determine if any of the given verbosity is enabled.
Definition: Test.h:193
void skip()
Mark the test as skipped.
Definition: Test.h:160
bool isDone()
Return true if test is done (passed, failed, skipped, expired).
Definition: Test.h:130
bool isNotFailed()
Return true if test is failed.
Definition: Test.h:145
bool isFailed()
Return true if test is failed.
Definition: Test.h:142
static const uint8_t kStatusSetup
Test is set up.
Definition: Test.h:58
static const uint8_t kStatusExpired
Test has timed out, or expire() called.
Definition: Test.h:70
bool isNotSkipped()
Return true if test isNot skipped.
Definition: Test.h:151
const FCString & getName()
Get the name of the test.
Definition: Test.h:111
virtual void teardown()
Optional method that performs any clean up after the test ends for any reasons, either passing or oth...
Definition: Test.h:98
bool isSkipped()
Return true if test isNot skipped.
Definition: Test.h:148
bool isNotExpired()
Return true if test is expired.
Definition: Test.h:157
void setStatus(uint8_t status)
Set the status of the test.
Definition: Test.h:117
uint8_t getStatus()
Get the status of the test.
Definition: Test.h:114