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