Documented how to manage static objects

This commit is contained in:
Mathieu Champlon 2015-10-09 22:38:37 +02:00
parent e41b75e525
commit 4f0431842f
7 changed files with 98 additions and 14 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
bin
out
build/xsl

View file

@ -19,13 +19,13 @@
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\turtle\cleanup.hpp" />
<ClInclude Include="..\..\include\turtle\config.hpp" />
<ClInclude Include="..\..\include\turtle\constraint.hpp" />
<ClInclude Include="..\..\include\turtle\constraints.hpp" />
<ClInclude Include="..\..\include\turtle\detail\action.hpp" />
<ClInclude Include="..\..\include\turtle\detail\addressof.hpp" />
<ClInclude Include="..\..\include\turtle\detail\child.hpp" />
<ClInclude Include="..\..\include\turtle\detail\cleanup.hpp" />
<ClInclude Include="..\..\include\turtle\detail\context.hpp" />
<ClInclude Include="..\..\include\turtle\detail\expectation_template.hpp" />
<ClInclude Include="..\..\include\turtle\detail\formatter.hpp" />

View file

@ -115,9 +115,6 @@
<ClInclude Include="..\..\include\turtle\detail\child.hpp">
<Filter>Source Files\detail</Filter>
</ClInclude>
<ClInclude Include="..\..\include\turtle\detail\cleanup.hpp">
<Filter>Source Files\detail</Filter>
</ClInclude>
<ClInclude Include="..\..\include\turtle\detail\context.hpp">
<Filter>Source Files\detail</Filter>
</ClInclude>
@ -127,5 +124,8 @@
<ClInclude Include="..\..\include\turtle\detail\formatter.hpp">
<Filter>Source Files\detail</Filter>
</ClInclude>
<ClInclude Include="..\..\include\turtle\cleanup.hpp">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View file

@ -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 <boost/test/auto_unit_test.hpp>
#include <turtle/mock.hpp>
#include <ostream>
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();
}
//]

View file

@ -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 :

View file

@ -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 <boost/test/unit_test_suite.hpp>
#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

View file

@ -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 <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/utility/identity_type.hpp>