From e41b75e525ff225c94071f3a502c8d9efdb6ef68 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Sun, 4 Oct 2015 11:10:15 +0200 Subject: [PATCH 1/4] Fixed error management --- build/build.bat | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/build/build.bat b/build/build.bat index e747db4..f88b5a0 100644 --- a/build/build.bat +++ b/build/build.bat @@ -6,13 +6,6 @@ rem Distributed under the Boost Software License, Version 1.0. rem (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) setlocal -goto Start - -:Error -color 00 -goto :eof - -:Start rem error if BOOST_ROOT not set set BOOST=%BOOST_ROOT% @@ -20,7 +13,7 @@ set BOOST=%BOOST_ROOT% pushd ..\test %BOOST%\b2 -q %* popd -if errorlevel 1 goto Error +if errorlevel 1 exit /b %ERRORLEVEL% set BOOSTBOOK_DIR=..\bin\turtle\boostbook xcopy /Y /S /Q /I %BOOST%\tools\boostbook\xsl %BOOSTBOOK_DIR%\xsl @@ -29,9 +22,8 @@ xcopy /Y /S /Q /I boostbook %BOOSTBOOK_DIR% xcopy /Y /S /Q /I %BOOST%\doc\src\boostbook.css ..\doc\html xcopy /Y /S /Q /I %BOOST%\doc\src\images\*.png ..\doc\html\images xcopy /Y /S /Q /I %BOOST%\doc\src\images\callouts\*.png ..\doc\html\images\callouts -if errorlevel 1 goto Error +if errorlevel 1 exit /b %ERRORLEVEL% pushd ..\doc %BOOST%\b2 -q %* popd - -:End \ No newline at end of file +if errorlevel 1 exit /b %ERRORLEVEL% From 4f0431842f2156c23f4649b94762f091178130f7 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Fri, 9 Oct 2015 22:38:37 +0200 Subject: [PATCH 2/4] Documented how to manage static objects --- .gitignore | 1 + build/vc100/turtle.vcxproj | 2 +- build/vc100/turtle.vcxproj.filters | 6 +-- doc/example/patterns_static_objects.cpp | 67 +++++++++++++++++++++++++ doc/patterns.qbk | 17 +++++++ include/turtle/{detail => }/cleanup.hpp | 17 +++---- include/turtle/mock.hpp | 2 +- 7 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 doc/example/patterns_static_objects.cpp rename include/turtle/{detail => }/cleanup.hpp (72%) diff --git a/.gitignore b/.gitignore index 132530c..17f91c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin out +build/xsl diff --git a/build/vc100/turtle.vcxproj b/build/vc100/turtle.vcxproj index 2fc8585..cf3d8da 100644 --- a/build/vc100/turtle.vcxproj +++ b/build/vc100/turtle.vcxproj @@ -19,13 +19,13 @@ + - diff --git a/build/vc100/turtle.vcxproj.filters b/build/vc100/turtle.vcxproj.filters index db9e20b..f3f1f04 100644 --- a/build/vc100/turtle.vcxproj.filters +++ b/build/vc100/turtle.vcxproj.filters @@ -115,9 +115,6 @@ Source Files\detail - - Source Files\detail - Source Files\detail @@ -127,5 +124,8 @@ Source Files\detail + + Source Files + \ No newline at end of file diff --git a/doc/example/patterns_static_objects.cpp b/doc/example/patterns_static_objects.cpp new file mode 100644 index 0000000..c385fbc --- /dev/null +++ b/doc/example/patterns_static_objects.cpp @@ -0,0 +1,67 @@ +// http://turtle.sourceforge.net +// +// Copyright Mathieu Champlon 2015 +// +// 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) + +//[ static_objects_problem +#define BOOST_AUTO_TEST_MAIN +#include +#include +#include + +namespace +{ + struct my_class + { + my_class( int i ) + : i_( i ) + {} + + int i_; + }; + + std::ostream& operator<<( std::ostream& os, const my_class* c ) + { + return os << "my_class " << c->i_; // the 'c' pointer must be valid when logging + } + + MOCK_FUNCTION( f, 1, void( my_class* ) ) // being static 'f' outlive the test case +} + +BOOST_AUTO_TEST_CASE( static_objects_problem ) +{ + my_class c( 42 ); + MOCK_EXPECT( f ).once().with( &c ); // the set expectation will also outlive the test case and leak into other test cases using 'f' +} // the 'c' instance goes out of scope and the '&c' pointer becomes dangling +//] + +//[ static_objects_partial_solution +struct fixture +{ + ~fixture() + { + mock::reset(); // the use of a fixture ensures the reset will prevent the expectations from leaking into other test cases + } +}; + +BOOST_FIXTURE_TEST_CASE( static_object_partial_solution, fixture ) +{ + my_class c( 42 ); + MOCK_EXPECT( f ).once().with( &c ); + f( &c ); + mock::verify(); // verify the expectations before local objects are destroyed and before the fixture resets them +} +//] + +//[ static_objects_solution +BOOST_FIXTURE_TEST_CASE( static_objects_solution, mock::cleanup ) // actually the library includes a ready to use fixture just like the one described +{ + my_class c( 42 ); + MOCK_EXPECT( f ).once().with( &c ); + f( &c ); + mock::verify(); +} +//] diff --git a/doc/patterns.qbk b/doc/patterns.qbk index 9322966..7cdfd84 100644 --- a/doc/patterns.qbk +++ b/doc/patterns.qbk @@ -6,6 +6,7 @@ /] [section Patterns] +[import example/patterns_static_objects.cpp] [import example/patterns_async_call.cpp] [import example/patterns_retrieve_cref.cpp] [import example/patterns_invoke_functor.cpp] @@ -13,6 +14,22 @@ This section highlights not-so-obvious features of the library gathered from real use cases. +[section Managing static mock objects] + +Problem : + +[static_objects_problem] + +Partial solution : + +[static_objects_partial_solution] + +Solution : + +[static_objects_solution] + +[endsect] + [section Waiting for an asynchronous call] Problem : diff --git a/include/turtle/detail/cleanup.hpp b/include/turtle/cleanup.hpp similarity index 72% rename from include/turtle/detail/cleanup.hpp rename to include/turtle/cleanup.hpp index 87cc5fb..47f8038 100644 --- a/include/turtle/detail/cleanup.hpp +++ b/include/turtle/cleanup.hpp @@ -9,29 +9,28 @@ #ifndef MOCK_CLEANUP_HPP_INCLUDED #define MOCK_CLEANUP_HPP_INCLUDED -#include "../config.hpp" +#include "config.hpp" +#include "verify.hpp" +#include "reset.hpp" #ifdef MOCK_USE_BOOST_TEST -#include "../verify.hpp" -#include "../reset.hpp" #include +#endif namespace mock -{ -namespace detail { struct cleanup { ~cleanup() { - // see https://svn.boost.org/trac/boost/ticket/5563 - //mock::verify(); mock::reset(); } }; + +#ifdef MOCK_USE_BOOST_TEST BOOST_GLOBAL_FIXTURE( cleanup ); -} +#endif + } // mock -#endif // MOCK_USE_BOOST_TEST #endif // MOCK_CLEANUP_HPP_INCLUDED diff --git a/include/turtle/mock.hpp b/include/turtle/mock.hpp index b7c9f65..a560800 100644 --- a/include/turtle/mock.hpp +++ b/include/turtle/mock.hpp @@ -13,12 +13,12 @@ #include "object.hpp" #include "reset.hpp" #include "verify.hpp" +#include "cleanup.hpp" #include "detail/functor.hpp" #include "detail/function.hpp" #include "detail/type_name.hpp" #include "detail/signature.hpp" #include "detail/parameter.hpp" -#include "detail/cleanup.hpp" #include #include #include From 09214ae3f8433abb15ce8d3888b7f7410f56f3b0 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Sat, 10 Oct 2015 10:04:20 +0200 Subject: [PATCH 3/4] Used local docbook packages --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index d8f0cfe..5064883 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,8 +28,13 @@ addons: apt: packages: - xsltproc + - docbook-xsl + - docbook-xml before_install: + - DOCBOOK_XSL_DIR=/usr/share/xml/docbook/stylesheet/docbook-xsl + - DOCBOOK_DTD_DIR=/usr/share/xml/docbook/schema/dtd/4.2 + # Set this to the name of your Boost library # Autodetect library name by using the following code: - PROJECT_TO_TEST=$(basename $(pwd)) - PROJECT_TO_TEST=$(basename $(pwd)) From ad2325584022a0855673bcac18e0d964988a67cc Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Sat, 10 Oct 2015 11:47:12 +0200 Subject: [PATCH 4/4] Added links to the pattern section --- doc/reference.qbk | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/reference.qbk b/doc/reference.qbk index 277cee4..b4797ef 100644 --- a/doc/reference.qbk +++ b/doc/reference.qbk @@ -165,7 +165,7 @@ Synopsis : MOCK_STATIC_METHOD_TPL( [calling convention] name, arity, signature[, identifier] ) // must be used if the signature uses a template parameter of the class, if 'identifier' is omitted it will default to 'name' -[note A static object is used behind the scene in order to keep track of the expectations of a mock static method, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the static method at the end of each test.] +[note A static object is used behind the scene in order to keep track of the expectations of a mock static method, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the static method at the end of each test, see the related [link turtle.patterns.managing_static_mock_objects pattern section].] [note In case of a calling convention specified, all four parameters must be provided.] @@ -275,7 +275,7 @@ Synopsis : MOCK_FUNCTION( [calling convention] name, arity, signature[, identifier] ) // if 'identifier' is omitted it will default to 'name' -[note A static object is used behind the scene in order to keep track of the expectations of a mock function, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the mock function at the end of each test.] +[note A static object is used behind the scene in order to keep track of the expectations of a mock function, therefore to ensure all tests run in isolation it is strongly suggested to manually [link turtle.reference.verification verify] and [link turtle.reference.reset reset] the mock function at the end of each test, see the related [link turtle.patterns.managing_static_mock_objects pattern section].] [note In case of a calling convention specified, all four parameters must be provided.]