From 6daff2167cff4909e270aac74b7f3392801efea9 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Sun, 28 May 2017 08:25:37 +0200 Subject: [PATCH] Added Catch integration --- doc/acknowledgements.qbk | 2 +- doc/changelog.qbk | 1 + doc/customization.qbk | 2 ++ doc/example/customization.cpp | 9 ++++--- include/turtle/catch.hpp | 47 +++++++++++++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 include/turtle/catch.hpp diff --git a/doc/acknowledgements.qbk b/doc/acknowledgements.qbk index b91f13e..9a2f5d2 100644 --- a/doc/acknowledgements.qbk +++ b/doc/acknowledgements.qbk @@ -7,6 +7,6 @@ [section Acknowledgements] -Many thanks to Adrien Gervaise, Silvin Lubecki and Takatoshi Kondo ! +Many thanks to Adrien Gervaise, Silvin Lubecki, Takatoshi Kondo and Ovanes Markarian ! [endsect] diff --git a/doc/changelog.qbk b/doc/changelog.qbk index 1fb51b5..64aba81 100644 --- a/doc/changelog.qbk +++ b/doc/changelog.qbk @@ -12,6 +12,7 @@ Not yet released * Fixed mocking of a function returning a reference for gcc 4.1 * Added MOCK_NO_AUTO_PTR to deactivate std::auto_ptr support +* Added [@https://github.com/philsquared/Catch Catch] integration [endsect] diff --git a/doc/customization.qbk b/doc/customization.qbk index d8f2dde..577ed88 100644 --- a/doc/customization.qbk +++ b/doc/customization.qbk @@ -148,6 +148,8 @@ The policy can then be activated by defining MOCK_ERROR_POLICY prior to includin [define_custom_policy] +A custom policy for [@https://github.com/philsquared/Catch Catch] is provided and can be enabled simply by including catch.hpp instead of turtle.hpp. + [endsect] [section Thread safety] diff --git a/doc/example/customization.cpp b/doc/example/customization.cpp index d679ac8..30f69c3 100644 --- a/doc/example/customization.cpp +++ b/doc/example/customization.cpp @@ -147,21 +147,22 @@ struct custom_policy { static Result abort() { - // ... + // Notify the test framework that an error occurs which makes it impossible to continue the test. + // This should most likely throw an exception of some kind. } template< typename Context > static void fail( const char* message, const Context& context, const char* file = "unknown location", int line = 0 ) { - // ... + // Notify the test framework that an unexpected call has occurred. } template< typename Context > static void call( const Context& context, const char* file, int line ) { - // ... + // Notify the test framework that an expectation has been fulfilled. } static void pass( const char* file, int line ) { - // ... + // Notify the test framework that the test execution merely passed the given code location. } }; //] diff --git a/include/turtle/catch.hpp b/include/turtle/catch.hpp new file mode 100644 index 0000000..1736dde --- /dev/null +++ b/include/turtle/catch.hpp @@ -0,0 +1,47 @@ +// http://turtle.sourceforge.net +// +// Copyright Mathieu Champlon and Ovanes Markarian 2017 +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef MOCK_CATCH_HPP_INCLUDED +#define MOCK_CATCH_HPP_INCLUDED + +#include + +template< typename Result > +struct catch_mock_error_policy +{ + static Result abort() + { + FAIL( "Aborted" ); + throw std::runtime_error( "unreachable" ); + } + + template< typename Context > + static void fail( const char* message, const Context& context, + const char* file = "file://unknown-location", line = 0 ) + { + CAPTURE( context ); + FAIL_CHECK( message << " in: " << file << ":" << line ); + } + + template< typename Context > + static void call( const Context& context, const char* file, int line ) + { + CAPTURE( context ); + INFO( file << ":" << line ); + } + + static void pass( const char* file, int line ) + { + INFO( file << ":" << line ); + } +}; + +#define MOCK_ERROR_POLICY catch_mock_error_policy +#include "mock.hpp" + +#endif // MOCK_CATCH_HPP_INCLUDED