diff --git a/src/libraries/turtle/function.hpp b/src/libraries/turtle/function.hpp index c2177e0..e61c3b6 100644 --- a/src/libraries/turtle/function.hpp +++ b/src/libraries/turtle/function.hpp @@ -120,11 +120,9 @@ namespace mock public: function_impl() : name_( "?" ) - , parent_( &root() ) + , parent_( 0 ) , valid_( true ) - { - parent_->add( *this ); - } + {} virtual ~function_impl() { if( parent_ ) @@ -177,13 +175,14 @@ namespace mock expectation_type& expect( const std::string& file, int line ) { - expectations_.push_back( expectation_type() ); - expectations_.back().set_location( file, line ); - valid_ = true; - return expectations_.back(); + expectation_type& e = expect(); + e.set_location( file, line ); + return e; } expectation_type& expect() { + if( ! parent_ ) + set_parent( root ); expectations_.push_back( expectation_type() ); valid_ = true; return expectations_.back(); diff --git a/src/libraries/turtle/node.hpp b/src/libraries/turtle/node.hpp index cdc099b..15c0e4b 100644 --- a/src/libraries/turtle/node.hpp +++ b/src/libraries/turtle/node.hpp @@ -58,10 +58,6 @@ namespace mock std::mem_fun( &verifiable::reset ) ); } - protected: - virtual void untie() - {} - private: typedef std::vector< verifiable* > verifiables_type; typedef verifiables_type::const_iterator verifiables_cit; diff --git a/src/libraries/turtle/object.hpp b/src/libraries/turtle/object.hpp index 872890f..37294d8 100644 --- a/src/libraries/turtle/object.hpp +++ b/src/libraries/turtle/object.hpp @@ -27,7 +27,7 @@ namespace mock template< typename T > void set_child( T& t ) const { - t.set_parent( *impl_ ); + impl_->set_child( t ); } void tag( const std::string& name ) const { @@ -48,13 +48,32 @@ namespace mock { public: object_impl() - { - root().add( *this ); - } + : parent_( 0 ) + {} virtual ~object_impl() { - root().remove( *this ); + if( parent_ ) + parent_->remove( *this ); } + template< typename T > + void set_child( T& t ) + { + if( ! parent_ ) + { + root.add( *this ); + parent_ = &root; + } + t.set_parent( *this ); + } + + protected: + virtual void untie() + { + parent_ = 0; + } + + private: + node* parent_; }; boost::shared_ptr< object_impl > impl_; diff --git a/src/libraries/turtle/root.hpp b/src/libraries/turtle/root.hpp index ba692d2..05fc277 100644 --- a/src/libraries/turtle/root.hpp +++ b/src/libraries/turtle/root.hpp @@ -11,22 +11,27 @@ #include "config.hpp" #include "node.hpp" +#include namespace mock { - inline node& root() + class root_t : public boost::unit_test::singleton< root_t >, public node { - static node r; - return r; - } + protected: + virtual void untie() + {} + private: + BOOST_TEST_SINGLETON_CONS( root_t ); + }; + BOOST_TEST_SINGLETON_INST( root ) inline bool verify() { - return root().verify(); + return root.verify(); } inline void reset() { - root().reset(); + root.reset(); } } diff --git a/src/tests/turtle_test/object_test.cpp b/src/tests/turtle_test/object_test.cpp index 0d4fcc2..2d4daae 100644 --- a/src/tests/turtle_test/object_test.cpp +++ b/src/tests/turtle_test/object_test.cpp @@ -16,6 +16,11 @@ #include #include +namespace +{ + mock::object static_o; +} + BOOST_AUTO_TEST_CASE( verifying_an_empty_object_succeeds ) { mock::object o;