AUnit  1.7.1
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
MetaAssertion.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> // definition of Print
26 #include "Flash.h"
27 #include "Printer.h"
28 #include "Verbosity.h"
29 #include "Compare.h"
30 #include "TestRunner.h"
31 #include "MetaAssertion.h"
32 
33 namespace aunit {
34 
35 // Moving these strings into PROGMEM saves 162 bytes of flash memory (from
36 // elimination of a function) and 130 bytes of static memory,
37 const char MetaAssertion::kMessageDone[] PROGMEM = "done";
38 const char MetaAssertion::kMessageNotDone[] PROGMEM = "not done";
39 const char MetaAssertion::kMessagePassed[] PROGMEM = "passed";
40 const char MetaAssertion::kMessageNotPassed[] PROGMEM = "not passed";
41 const char MetaAssertion::kMessageFailed[] PROGMEM = "failed";
42 const char MetaAssertion::kMessageNotFailed[] PROGMEM = "not failed";
43 const char MetaAssertion::kMessageSkipped[] PROGMEM = "skipped";
44 const char MetaAssertion::kMessageNotSkipped[] PROGMEM = "not skipped";
45 const char MetaAssertion::kMessageExpired[] PROGMEM = "timed out";
46 const char MetaAssertion::kMessageNotExpired[] PROGMEM = "not timed out";
47 
48 namespace {
49 
50 // Print an assertion message describing whether the given 'testName' has passed
51 // or failed
52 void printAssertionTestStatusMessage(
53  bool ok, const char* file, uint16_t line,
54  const char* testName, const __FlashStringHelper* statusMessage) {
55  // Many of the following strings are duplicated in Assertion.cpp and
56  // the compiler/linker will dedupe them.
57  Print* printer = Printer::getPrinter();
58  printer->print(file);
59  printer->print(':');
60  printer->print(line);
61  printer->print(": Assertion ");
62  printer->print(ok ? "passed" : "failed");
63  printer->print(F(": Test "));
64  printer->print(testName);
65  printer->print(" is ");
66  printer->print(statusMessage);
67  printer->println('.');
68 }
69 
70 }
71 
72 bool MetaAssertion::assertionTestStatus(const char* file, uint16_t line,
73  const char* testName, const __FlashStringHelper* statusMessage, bool ok) {
74  if (isDone()) return false;
75  if (isOutputEnabled(ok)) {
76  printAssertionTestStatusMessage(ok, file, line, testName, statusMessage);
77  }
78  setPassOrFail(ok);
79  return ok;
80 }
81 
82 namespace {
83 
84 // Print message for failNow() macro.
85 // "{file}:{line}: Status failed."
86 void printStatusNowMessage(const char* file, uint16_t line,
87  const __FlashStringHelper* statusString) {
88  // Many of these strings are duplicated in Assertion.cpp and will be deduped
89  // by the compiler/linker.
90  Print* printer = Printer::getPrinter();
91  printer->print(file);
92  printer->print(':');
93  printer->print(line);
94  printer->print(F(": Status "));
95  printer->print(statusString);
96  printer->println('.');
97 }
98 
99 }
100 
101 bool MetaAssertion::isOutputEnabledForStatus(uint8_t status) const {
102  return (status == kStatusFailed && isVerbosity(Verbosity::kTestFailed))
106 }
107 
108 void MetaAssertion::setStatusNow(const char* file, uint16_t line,
109  uint8_t status, const __FlashStringHelper* statusString) {
110  if (isDone()) return;
111  if (isOutputEnabledForStatus(status)) {
112  printStatusNowMessage(file, line, statusString);
113  }
114  setStatus(status);
115 }
116 
117 }
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...
bool isOutputEnabled(bool ok) const
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:254
bool assertionTestStatus(const char *file, uint16_t line, const char *testName, const __FlashStringHelper *statusMessage, bool ok)
Set the status of the current test using the 'ok' status from another test, and print the assertion m...
void setStatusNow(const char *file, uint16_t line, uint8_t status, const __FlashStringHelper *statusString)
Set the status of the current test to 'status' and print a message.
bool isOutputEnabledForStatus(uint8_t status) const
Return true if setting of status should print a message.
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:48
bool isDone() const
Return true if test has been asserted.
Definition: Test.h:196
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 setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
Definition: Test.cpp:50
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