AUnit  0.5.2
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("Assertion ");
59  printer->print(ok ? "passed" : "failed");
60  printer->print(F(": Test "));
61  printer->print(testName);
62  printer->print(" is ");
63  printer->print(statusMessage);
64  printer->print(", file ");
65  printer->print(file);
66  printer->print(", line ");
67  printer->print(line);
68  printer->println('.');
69 }
70 
71 }
72 
73 bool MetaAssertion::assertionTestStatus(const char* file, uint16_t line,
74  const char* testName, const __FlashStringHelper* statusMessage, bool ok) {
75  if (isDone()) return false;
76  if (isOutputEnabled(ok)) {
77  printAssertionTestStatusMessage(ok, file, line, testName, statusMessage);
78  }
79  setPassOrFail(ok);
80  return ok;
81 }
82 
83 namespace {
84 
85 // Print message for failNow() macro.
86 // "Status failed, file xxx, line yyy."
87 void printStatusNowMessage(const char* file, uint16_t line,
88  const __FlashStringHelper* statusString) {
89  // Many of these strings are duplicated in Assertion.cpp and will be deduped
90  // by the compiler/linker.
91  Print* printer = Printer::getPrinter();
92  printer->print(F("Status "));
93  printer->print(statusString);
94  printer->print(", file ");
95  printer->print(file);
96  printer->print(", line ");
97  printer->print(line);
98  printer->println('.');
99 }
100 
101 }
102 
104  return (status == kStatusFailed && isVerbosity(Verbosity::kTestFailed))
108 }
109 
110 void MetaAssertion::setStatusNow(const char* file, uint16_t line,
111  uint8_t status, const __FlashStringHelper* statusString) {
112  if (isDone()) return;
113  if (isOutputEnabledForStatus(status)) {
114  printStatusNowMessage(file, line, statusString);
115  }
116  setStatus(status);
117 }
118 
119 }
static const uint8_t kTestExpired
Print test timed out message.
Definition: Verbosity.h:55
static const uint8_t kStatusFailed
Test has failed, or fail() was called.
Definition: Test.h:102
void setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
Definition: Test.cpp:50
static const uint8_t kTestPassed
Print test passed message.
Definition: Verbosity.h:46
static const uint8_t kStatusPassed
Test has passed, or pass() was called.
Definition: Test.h:99
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 &#39;ok&#39; status from another test, and print the assertion m...
static const uint8_t kStatusSkipped
Test is skipped through the exclude() method or skip() was called.
Definition: Test.h:105
This file provides overloaded compareXxx(a, b) functions which are used by the various assertXxx(a...
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:117
void setStatusNow(const char *file, uint16_t line, uint8_t status, const __FlashStringHelper *statusString)
Set the status of the current test to &#39;status&#39; and print a message.
bool isOutputEnabledForStatus(uint8_t status)
Return true if setting of status should print a message.
bool isVerbosity(uint8_t verbosity)
Determine if any of the given verbosity is enabled.
Definition: Test.h:275
bool isDone()
Return true if test has been asserted.
Definition: Test.h:196
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:52
static const uint8_t kStatusExpired
Test has timed out, or expire() called.
Definition: Test.h:108
static const uint8_t kTestSkipped
Print test skipped message.
Definition: Verbosity.h:52
Various macros to smooth over the differences among the various platforms with regards to their suppo...
static const uint8_t kTestFailed
Print test failed message.
Definition: Verbosity.h:49
void setStatus(uint8_t status)
Set the status of the test.
Definition: Test.h:173