Refactoring

git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@138 860be788-9bd5-4423-9f1e-828f051e677b
This commit is contained in:
mat007 2010-03-02 23:42:15 +00:00
parent 1a7fb3dd65
commit a366578b9e
8 changed files with 136 additions and 114 deletions

View file

@ -169,7 +169,7 @@
>
</File>
<File
RelativePath="..\..\src\libraries\turtle\constraint.hpp"
RelativePath="..\..\src\libraries\turtle\constraints.hpp"
>
</File>
<File
@ -212,6 +212,10 @@
RelativePath="..\..\src\libraries\turtle\object.hpp"
>
</File>
<File
RelativePath="..\..\src\libraries\turtle\operators.hpp"
>
</File>
<File
RelativePath="..\..\src\libraries\turtle\placeholder.hpp"
>

View file

@ -191,7 +191,7 @@
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx;h;hpp;hxx;hm;inl;inc;xsd"
>
<File
RelativePath="..\..\src\tests\turtle_test\constraint_test.cpp"
RelativePath="..\..\src\tests\turtle_test\constraints_test.cpp"
>
</File>
<File

View file

@ -10,8 +10,8 @@
#define MOCK_CHECK_HPP_INCLUDED
#include "placeholder.hpp"
#include "constraint.hpp"
#include "is_functor.hpp"
#include "constraints.hpp"
#include "format.hpp"
#include <boost/function.hpp>
#include <boost/utility/enable_if.hpp>

View file

@ -6,38 +6,14 @@
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MOCK_CONSTRAINT_HPP_INCLUDED
#define MOCK_CONSTRAINT_HPP_INCLUDED
#ifndef MOCK_CONSTRAINTS_HPP_INCLUDED
#define MOCK_CONSTRAINTS_HPP_INCLUDED
#include "placeholder.hpp"
#include "functional.hpp"
#include "format.hpp"
#include <sstream>
#include "operators.hpp"
namespace mock
{
template< typename Functor, typename Description >
const detail::placeholder< Functor > constraint( const Functor& f,
const Description& desc )
{
std::stringstream s;
s << std::boolalpha << desc;
return detail::placeholder< Functor >( f, s.str() );
}
template< typename Functor >
const detail::placeholder< Functor > constraint( const Functor& f )
{
return detail::placeholder< Functor >( f, "?" );
}
template< typename Functor, typename T >
const detail::placeholder< Functor > constraint( const Functor& f,
const std::string& name,
const T& t )
{
return detail::placeholder< Functor >( f,
name + "( " + format( t ) + " )" );
}
namespace detail
{
template<>
@ -135,4 +111,4 @@ namespace detail
}
}
#endif // #ifndef MOCK_CONSTRAINT_HPP_INCLUDED
#endif // #ifndef MOCK_CONSTRAINTS_HPP_INCLUDED

View file

@ -14,7 +14,7 @@
#include "action.hpp"
#include "sequence.hpp"
#include "check.hpp"
#include "constraint.hpp"
#include "constraints.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/preprocessor/repetition/repeat_from_to.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>

View file

@ -0,0 +1,100 @@
//
// Copyright Mathieu Champlon 2010
//
// 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_OPERATORS_HPP_INCLUDED
#define MOCK_OPERATORS_HPP_INCLUDED
#include "placeholder.hpp"
namespace mock
{
namespace detail
{
template< typename Functor1, typename Functor2 >
class and_
{
public:
and_( const Functor1& f1, const Functor2& f2 )
: f1_( f1 )
, f2_( f2 )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return f1_( actual ) && f2_( actual );
}
private:
Functor1 f1_;
Functor2 f2_;
};
template< typename Functor1, typename Functor2 >
class or_
{
public:
or_( const Functor1& f1, const Functor2& f2 )
: f1_( f1 )
, f2_( f2 )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return f1_( actual ) || f2_( actual );
}
private:
Functor1 f1_;
Functor2 f2_;
};
template< typename Functor >
class not_
{
public:
explicit not_( const Functor& f )
: f_( f )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return ! f_( actual );
}
private:
Functor f_;
};
template< typename Functor1, typename Functor2 >
const placeholder< or_< Functor1, Functor2 > >
operator||( const placeholder< Functor1 >& lhs,
const placeholder< Functor2 >& rhs )
{
return constraint(
or_< Functor1, Functor2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " || " + rhs.desc_ + ")" );
}
template< typename Functor1, typename Functor2 >
const placeholder< and_< Functor1, Functor2 > >
operator&&( const placeholder< Functor1 >& lhs,
const placeholder< Functor2 >& rhs )
{
return constraint(
and_< Functor1, Functor2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " && " + rhs.desc_ + ")" );
}
template< typename Functor >
const placeholder< not_< Functor > >
operator!( const placeholder< Functor >& ph )
{
return constraint(
not_< Functor >( ph.f_ ), "! " + ph.desc_ );
}
}
}
#endif // #ifndef MOCK_OPERATORS_HPP_INCLUDED

View file

@ -9,7 +9,7 @@
#ifndef MOCK_PLACEHOLDER_HPP_INCLUDED
#define MOCK_PLACEHOLDER_HPP_INCLUDED
#include <string>
#include "format.hpp"
namespace mock
{
@ -25,87 +25,29 @@ namespace detail
Functor f_;
std::string desc_;
};
template< typename Functor1, typename Functor2 >
class and_
{
public:
and_( const Functor1& f1, const Functor2& f2 )
: f1_( f1 )
, f2_( f2 )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return f1_( actual ) && f2_( actual );
}
private:
Functor1 f1_;
Functor2 f2_;
};
template< typename Functor1, typename Functor2 >
class or_
{
public:
or_( const Functor1& f1, const Functor2& f2 )
: f1_( f1 )
, f2_( f2 )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return f1_( actual ) || f2_( actual );
}
private:
Functor1 f1_;
Functor2 f2_;
};
template< typename Functor >
class not_
{
public:
explicit not_( const Functor& f )
: f_( f )
{}
template< typename Actual >
bool operator()( const Actual& actual ) const
{
return ! f_( actual );
}
private:
Functor f_;
};
template< typename F1, typename F2 >
const placeholder< or_< F1, F2 > >
operator||( const placeholder< F1 >& lhs,
const placeholder< F2 >& rhs )
{
return placeholder< or_< F1, F2 > >(
or_< F1, F2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " || " + rhs.desc_ + ")" );
}
template< typename F1, typename F2 >
const placeholder< and_< F1, F2 > >
operator&&( const placeholder< F1 >& lhs,
const placeholder< F2 >& rhs )
{
return placeholder< and_< F1, F2 > >(
and_< F1, F2 >( lhs.f_, rhs.f_ ),
"(" + lhs.desc_ + " && " + rhs.desc_ + ")" );
}
template< typename F >
const placeholder< not_< F > >
operator!( const placeholder< F >& ph )
{
return placeholder< not_< F > >(
not_< F >( ph.f_ ), "! " + ph.desc_ );
}
}
template< typename Functor, typename Description >
const detail::placeholder< Functor > constraint( const Functor& f,
const Description& desc )
{
std::stringstream s;
s << std::boolalpha << desc;
return detail::placeholder< Functor >( f, s.str() );
}
template< typename Functor >
const detail::placeholder< Functor > constraint( const Functor& f )
{
return detail::placeholder< Functor >( f, "?" );
}
template< typename Functor, typename T >
const detail::placeholder< Functor > constraint( const Functor& f,
const std::string& name,
const T& t )
{
return detail::placeholder< Functor >( f,
name + "( " + format( t ) + " )" );
}
}
#endif // #ifndef MOCK_PLACEHOLDER_HPP_INCLUDED

View file

@ -6,7 +6,7 @@
// http://www.boost.org/LICENSE_1_0.txt)
//
#include <turtle/constraint.hpp>
#include <turtle/constraints.hpp>
#include <boost/test/auto_unit_test.hpp>
#define BOOST_LIB_NAME boost_unit_test_framework