AUnit  0.4.0
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 #ifdef ESP8266
26 #include <pgmspace.h>
27 #else
28 #include <avr/pgmspace.h>
29 #endif
30 
31 #include <Arduino.h> // definition of Print
32 #include "Printer.h"
33 #include "Verbosity.h"
34 #include "Compare.h"
35 #include "TestRunner.h"
36 #include "MetaAssertion.h"
37 
38 namespace aunit {
39 
40 // Moving these strings into PROGMEM saves 162 bytes of flash memory (from
41 // elimination of a function) and 130 bytes of static memory,
42 const char MetaAssertion::kMessageDone[] PROGMEM = "done";
43 const char MetaAssertion::kMessageNotDone[] PROGMEM = "not done";
44 const char MetaAssertion::kMessagePassed[] PROGMEM = "passed";
45 const char MetaAssertion::kMessageNotPassed[] PROGMEM = "not passed";
46 const char MetaAssertion::kMessageFailed[] PROGMEM = "failed";
47 const char MetaAssertion::kMessageNotFailed[] PROGMEM = "not failed";
48 const char MetaAssertion::kMessageSkipped[] PROGMEM = "skipped";
49 const char MetaAssertion::kMessageNotSkipped[] PROGMEM = "not skipped";
50 const char MetaAssertion::kMessageExpired[] PROGMEM = "timed out";
51 const char MetaAssertion::kMessageNotExpired[] PROGMEM = "not timed out";
52 
54  bool ok, const char* file, uint16_t line,
55  const char* testName, const __FlashStringHelper* statusMessage) {
56  // Trying to move these strings into PROGMEM actually makes the flash memory
57  // consumption bigger. The compile/linker will dedupe these c-strings.
58  Print* printer = Printer::getPrinter();
59  printer->print("Assertion ");
60  printer->print(ok ? "passed" : "failed");
61  printer->print(": Test ");
62  printer->print(testName);
63  printer->print(" is ");
64  printer->print(statusMessage);
65  printer->print(", file ");
66  printer->print(file);
67  printer->print(", line ");
68  printer->print(line);
69  printer->println('.');
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 }
void printAssertionTestStatusMessage(bool ok, const char *file, uint16_t line, const char *testName, const __FlashStringHelper *statusMessage)
Print the meta assertion passed or failed message.
Various assertTestXxx() and checkTestXxx() macros are defined in this header.
void setPassOrFail(bool ok)
Set the status to Passed or Failed depending on ok.
Definition: Test.cpp:57
bool assertionTestStatus(const char *file, uint16_t line, const char *testName, const __FlashStringHelper *statusMessage, bool ok)
Set the status of the current test based on &#39;ok, and print assertion message if requested.
bool isOutputEnabled(bool ok)
Returns true if an assertion message should be printed.
Definition: Assertion.cpp:67
bool isDone()
Return true if test is done (passed, failed, skipped, expired).
Definition: Test.h:118
static Print * getPrinter()
Get the output printer used by the various assertion() methods and the TestRunner.
Definition: Printer.h:50