|
Boost : |
From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2001-12-21 13:54:56
----- Original Message -----
From: rogeeff <rogeeff_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, December 21, 2001 3:42 PM
Subject: [boost] Re: Unit Test Framework: Class Test Cases
> --- In boost_at_y..., "Fernando Cacciola" <fcacciola_at_g...> wrote:
> >
> [...]
>
> > > Though the problem seems to be pretty generic and worth adding to
> the
> > > base implementation. I could add an ability for the user to
> specify a
> > > ninstance of user test case as an optional argument to
> > > BOOST_TEST_CASE like this:
> > >
> > > BOOST_TEST_CASE(( &A::foo )); // here framework will create an
> > > instance
> > >
> > > A* a = new A(...)
> > > BOOST_TEST_CASE(( &A::foo, a )); // here framework will use the
> > > instance provided and DESTROY it at the end.
> > >
> > > How about this?
> > >
> > Yes, this is exactly what I would like to have.
> >
> > BTW, I already add that to my copy.
> >
> > In order to allow A not to need a default constructor that is
> probably never
>
> [...]
>
> No need to introduce any new classes. Just add UserTestCase* argument
> to user_test_case_holder and class_test_case constructors and that is
> it. Plus change a bit logic in user_test_case_holder::init(). I will
> do it soon.
>
This is just what I initially did.
But it requires UserTestCase to have a default constructor, because the
init() logic has to be able to create a UserTestCase() if the current one is
NULL.
I really don't want to impose the requirement that user classes must have a
default constructor, so I added the new classes just to avoid this.
> [...]
>
> > But the 'closure' class might be tricky to get right, so if
> >
> > > BOOST_TEST_CASE(( &A::foo, a )); // here framework will use the
> >
> > can be parsed correctly it is probably better.
>
> I will use closure internally. I want to provide a simplies interface
> possible.
>
If you are using a closure internally, here's the one I'm using now:
//
**************************************************************************
//
// ************** closure
************** //
//
**************************************************************************
//
// This class is used to tie a member fucntion to a particular class
instance.
// It is used to build a test case with a specific method of a specific
object.
//
// NOTE: Your are not supposed to instantiate closure<> directly, use
make_closure();
//
template<class class_type, class func_type>
struct closure
{
typedef class_type class_type ;
typedef func_type func_type ;
closure ( boost::shared_ptr<class_type> a_obj, func_type a_func )
: m_obj(a_obj), m_func(a_func) {}
boost::shared_ptr<class_type> m_obj ;
func_type m_func ;
} ;
// This template function creates a closure binding an object to a member
fucntion.
//
// USAGE: MyClass my_instance ;
make_closure(my_instance,&MyClass::MyMethod);
//
template<class class_type, class func_type>
inline
closure<class_type,func_type> make_closure ( boost::shared_ptr<class_type>
a_obj,
func_type a_func
)
{ return closure<class_type,func_type>(a_obj,a_func) ; }
Also notice that this uses shared_ptr<>, the additional fixed_xxx classes
also use a shared_ptr<> to hold the UserTestCase so there are no lifetime
issues to solve.
(this was not so in the code I pasted before but it is so in the code I'm
running now...)
The user would have to do this:
boost::shared_ptr<test_bed> bed( new test_bed(...) ) ;
test->add( BOOST_TEST_CASE( (test_bed::proceed,bed) ) );
Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk