AUnit  0.3.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit.
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 #include <Arduino.h> // for declaration of 'Serial' on Teensy and others
26 #include "Test.h"
27 #include "Compare.h"
28 
29 namespace aunit {
30 
31 // Use a static variable inside a function to solve the static initialization
32 // ordering problem.
34  static Test* root;
35  return &root;
36 }
37 
38 Test::Test(const char* name):
39  mName(name),
40  mStatus(kStatusNew),
41  mNext(nullptr) {
42  insert();
43 }
44 
45 Test::Test(const __FlashStringHelper* name):
46  mName(name),
47  mStatus(kStatusNew),
48  mNext(nullptr) {
49  insert();
50 }
51 
52 // Resolve the status as kStatusFailed only if ok == false. Otherwise, keep the
53 // status as kStatusSetup to allow testing() test cases to continue.
54 void Test::setPassOrFail(bool ok) {
55  if (!ok) {
56  mStatus = kStatusFailed;
57  }
58 }
59 
60 // Insert the current test case into the singly linked list, sorted by
61 // getName(). This is an O(N^2) algorithm, but should be good enough for
62 // small N ~= 100. If N becomes bigger than that, it's probably better to insert
63 // using an O(N) algorithm, then sort the elements later in TestRunner::run().
64 // Also, we don't increment a static counter here, because that would introduce
65 // another static initialization ordering problem.
66 void Test::insert() {
67  // Find the element p whose p->next sorts after the current test
68  Test** p = getRoot();
69  while (*p != nullptr) {
70  if (compareString(getName(), (*p)->getName()) < 0) break;
71  p = &(*p)->mNext;
72  }
73  mNext = *p;
74  *p = this;
75 }
76 
78  once();
79  if (getStatus() == kStatusSetup) {
80  pass();
81  }
82 }
83 
84 }
virtual void once()=0
User-provided test case.
Base class of all test cases.
Definition: Test.h:100
static const uint8_t kStatusFailed
Test has failed, or failed() was called.
Definition: Test.h:115
Test(const char *name)
Constructor taking the name of the given test case.
Definition: Test.cpp:38
Various macros (test(), testing(), externTest(), externTesting()) are defined in this header...
void setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
Definition: Test.cpp:54
void pass()
Mark the test as passed.
Definition: Test.h:217
static Test ** getRoot()
Get the pointer to the root pointer.
Definition: Test.cpp:33
virtual void loop() override
Calls the user-provided once() method.
Definition: Test.cpp:77
static const uint8_t kStatusSetup
Test is set up.
Definition: Test.h:109
const FCString & getName()
Get the name of the test.
Definition: Test.h:158
uint8_t getStatus()
Get the status of the test.
Definition: Test.h:161