AUnit  0.4.0
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 
86  virtual void setup() {}
87 
93  virtual void loop() = 0;
94 
96  void resolve();
97 
99  const FCString& getName() { return mName; }
100 
102  uint8_t getStatus() { return mStatus; }
103 
105  void setStatus(uint8_t status) { mStatus = status; }
106 
108  void setPassOrFail(bool ok);
109 
115  Test** getNext() { return &mNext; }
116 
118  bool isDone() { return mStatus >= kStatusPassed; }
119 
121  bool isNotDone() { return !isDone(); }
122 
124  bool isPassed() { return mStatus == kStatusPassed; }
125 
127  bool isNotPassed() { return !isPassed(); }
128 
130  bool isFailed() { return mStatus == kStatusFailed; }
131 
133  bool isNotFailed() { return !isFailed(); }
134 
136  bool isSkipped() { return mStatus == kStatusSkipped; }
137 
139  bool isNotSkipped() { return !isSkipped(); }
140 
142  bool isExpired() { return mStatus == kStatusExpired; }
143 
145  bool isNotExpired() { return !isExpired(); }
146 
148  void skip() { mStatus = kStatusSkipped; }
149 
151  void expire() { mStatus = kStatusExpired; }
152 
154  void enableVerbosity(uint8_t verbosity) { mVerbosity |= verbosity; }
155 
157  void disableVerbosity(uint8_t verbosity) { mVerbosity &= ~verbosity; }
158 
159  protected:
161  void fail() { mStatus = kStatusFailed; }
162 
164  void pass() { mStatus = kStatusPassed; }
165 
166  void init(const char* name) {
167  mName = FCString(name);
168  mStatus = kStatusNew;
169  mVerbosity = 0;
170  insert();
171  }
172 
173  void init(const __FlashStringHelper* name) {
174  mName = FCString(name);
175  mStatus = kStatusNew;
176  mVerbosity = 0;
177  insert();
178  }
179 
181  bool isVerbosity(uint8_t verbosity) { return mVerbosity & verbosity; }
182 
184  uint8_t getVerbosity() { return mVerbosity; }
185 
186  private:
187  // Disable copy-constructor and assignment operator
188  Test(const Test&) = delete;
189  Test& operator=(const Test&) = delete;
190 
192  void insert();
193 
194  FCString mName;
195  uint8_t mStatus;
196  uint8_t mVerbosity;
197  Test* mNext;
198 };
199 
200 }
201 
202 #endif
void disableVerbosity(uint8_t verbosity)
Disable the given verbosity of the current test.
Definition: Test.h:157
Base class of all test cases.
Definition: Test.h:49
void expire()
Mark the test as expired (i.e.
Definition: Test.h:151
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:161
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:121
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:127
uint8_t getVerbosity()
Get the verbosity.
Definition: Test.h:184
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:115
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:124
virtual void setup()
Optional method that performs any initialization.
Definition: Test.h:86
virtual void loop()=0
The user-provided test case function.
void pass()
Mark the test as passed.
Definition: Test.h:164
void enableVerbosity(uint8_t verbosity)
Enable the given verbosity of the current test.
Definition: Test.h:154
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:142
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:181
void skip()
Mark the test as skipped.
Definition: Test.h:148
bool isDone()
Return true if test is done (passed, failed, skipped, expired).
Definition: Test.h:118
bool isNotFailed()
Return true if test is failed.
Definition: Test.h:133
bool isFailed()
Return true if test is failed.
Definition: Test.h:130
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:139
const FCString & getName()
Get the name of the test.
Definition: Test.h:99
bool isSkipped()
Return true if test isNot skipped.
Definition: Test.h:136
bool isNotExpired()
Return true if test is expired.
Definition: Test.h:145
void setStatus(uint8_t status)
Set the status of the test.
Definition: Test.h:105
uint8_t getStatus()
Get the status of the test.
Definition: Test.h:102