Skip to content

Commit

Permalink
Add AUnitVerbose.h, a verbose variant of AUnit.h, fix for #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bxparks committed Apr 11, 2018
1 parent fba9ae6 commit 806dd2e
Show file tree
Hide file tree
Showing 14 changed files with 672 additions and 29 deletions.
32 changes: 32 additions & 0 deletions examples/basic_verbose/basic_verbose.ino
@@ -0,0 +1,32 @@
#line 2 "basic_verbose.ino"

// Same as ../basic/basic.ino except this uses <AUnitVerbose.h> instead of
// <AUnit.h> to get the more verbose assertion messages containing the string
// fragment of the actual arguments in the assertXxx() macros. The cost is
// ~20-25% increase in flash memory to store those strings for moderate to
// large unit tests. But the verbose version may be helpful for debugging.

#include <AUnitVerbose.h>

test(correct) {
int x = 1;
assertEqual(x, 1);
}

test(incorrect) {
int x = 1;
assertNotEqual(x, 1);
}

void setup() {
delay(1000); // wait for stability on some boards to prevent garbage Serial
Serial.begin(115200); // ESP8266 default of 74880 not supported on Linux
while(!Serial); // for the Arduino Leonardo/Micro only
}

void loop() {
// Should get:
// TestRunner summary:
// 1 passed, 1 failed, 0 skipped, 0 timed out, out of 2 test(s).
aunit::TestRunner::run();
}
2 changes: 1 addition & 1 deletion src/AUnit.h
Expand Up @@ -41,7 +41,7 @@ SOFTWARE.
#include "aunit/TestOnce.h"
#include "aunit/TestAgain.h"
#include "aunit/TestRunner.h"
#include "aunit/AssertMacros.h"
#include "aunit/AssertMacros.h" // terse assertXxx() macros
#include "aunit/MetaAssertMacros.h"
#include "aunit/TestMacros.h"

Expand Down
54 changes: 54 additions & 0 deletions src/AUnitVerbose.h
@@ -0,0 +1,54 @@
/*
MIT License
Copyright (c) 2018 Brian T. Park
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

/**
* @file AUnitVerbose.h
*
* Same as AUnit.h except that the verbose versions of teh various assertXxx()
* macros are provided. These capture the strings of the actual arguments in
* the assert macros and print more verbose and helpful messages in the same
* format used by ArduinoUnit. The cost is 20-25% increase in flash memory to
* hold those strings for medium to large unit tests.
*/

#ifndef AUNIT_AUNIT_VERBOSE_H
#define AUNIT_AUNIT_VERBOSE_H

#include "aunit/Verbosity.h"
#include "aunit/Compare.h"
#include "aunit/Printer.h"
#include "aunit/Test.h"
#include "aunit/Assertion.h"
#include "aunit/MetaAssertion.h"
#include "aunit/TestOnce.h"
#include "aunit/TestAgain.h"
#include "aunit/TestRunner.h"
#include "aunit/AssertVerboseMacros.h" // verbose assertXxx() macros
#include "aunit/MetaAssertMacros.h"
#include "aunit/TestMacros.h"

// Version format: xxyyzz == "xx.yy.zz"
#define AUNIT_VERSION 000402

#endif
82 changes: 82 additions & 0 deletions src/aunit/AssertVerboseMacros.h
@@ -0,0 +1,82 @@
/*
MIT License
Copyright (c) 2018 Brian T. Park
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

// Significant portions of the design and implementation of this file came from
// https://github.com/mmurdoch/arduinounit/blob/master/src/ArduinoUnit.h

/**
* @file AssertVerboseMacros.h
*
* Verbose versions of the macros in AssertMacros.h. These capture the string
* of the actual arguments and pass them to the respective assertionVerbose()
* methods so that verbose messages can be printed.
*/

#ifndef AUNIT_ASSERT_VERBOSE_MACROS_H
#define AUNIT_ASSERT_VERBOSE_MACROS_H

/** Assert that arg1 is equal to arg2. */
#define assertEqual(arg1,arg2) \
assertOpVerboseInternal(arg1,aunit::compareEqual,"==",arg2)

/** Assert that arg1 is not equal to arg2. */
#define assertNotEqual(arg1,arg2) \
assertOpVerboseInternal(arg1,aunit::compareNotEqual,"!=",arg2)

/** Assert that arg1 is less than arg2. */
#define assertLess(arg1,arg2) \
assertOpVerboseInternal(arg1,aunit::compareLess,"<",arg2)

/** Assert that arg1 is more than arg2. */
#define assertMore(arg1,arg2) \
assertOpVerboseInternal(arg1,aunit::compareMore,">",arg2)

/** Assert that arg1 is less than or equal to arg2. */
#define assertLessOrEqual(arg1,arg2) \
assertOpVerboseInternal(arg1,aunit::compareLessOrEqual,"<=",arg2)

/** Assert that arg1 is more than or equal to arg2. */
#define assertMoreOrEqual(arg1,arg2) \
assertOpVerboseInternal(arg1,aunit::compareMoreOrEqual,">=",arg2)

/** Assert that arg is true. */
#define assertTrue(arg) assertBoolVerboseInternal(arg,true)

/** Assert that arg is false. */
#define assertFalse(arg) assertBoolVerboseInternal(arg,false)

/** Internal helper macro, shouldn't be called directly by users. */
#define assertOpVerboseInternal(arg1,op,opName,arg2) do {\
if (!assertionVerbose(__FILE__,__LINE__,\
(arg1),AUNIT_F(#arg1),opName,op,(arg2),AUNIT_F(#arg2)))\
return;\
} while (false)

/** Internal helper macro, shouldn't be called directly by users. */
#define assertBoolVerboseInternal(arg,value) do {\
if (!assertionBoolVerbose(__FILE__,__LINE__,(arg),AUNIT_F(#arg),(value)))\
return;\
} while (false)

#endif

0 comments on commit 806dd2e

Please sign in to comment.