Re: [Cppunit-devel] equality of const objects
Brought to you by:
blep
|
From: Baptiste L. <gai...@fr...> - 2001-09-25 15:13:38
|
Quoting "Steve M. Robbins" <ste...@vi...>:
> On Tue, Sep 25, 2001 at 02:56:55PM +0200, Baptiste Lepilleur wrote:
>
> > I'm not saying to change CppUnit as a whole. The only thing I want
> > to change is those part of CppUnit that would impose on the
> > "user" policy:
> >
> > struct assertion_traits
> > {
> > static bool equal( const T& x, const T& y )
> > => change to
> > static bool equal( T& x, T& y )
> >
> > and
> > template <class T>
> > void assertEquals( const T& expected,
> > const T& actual,
> > => change to:
> > template <class T>
> > void assertEquals( T& expected,
> > T& actual,
> >
> > The change is invisible for user that apply the "const" policy, and
> allow
> > user who don't to use the assertEquals without having to do
> const_cast.
>
> Sorry Baptiste, I do not understand. Why do you say that using const
> references makes writing equal() black magic? Can you give us a short
> example of a class that is problematic for equal(const&,const&)?
Example of assertion_traits for FileDependency:
static bool equal( const FileDependency& x, const FileDependency& y )
{
return const_cast<FileDependency&>(x).equalsTo(
const_cast<FileDependency *>( &y ) );
}
static std::string toString( const FileDependency& x )
{
return const_cast<FileDependency&>( x ).toString();
}
The const_cast are the things I call "black magic". The code would be
much cleaner if:
static bool equal( FileDependency& x, FileDependency& y )
{
return x.equalsTo( &y );
}
static std::string toString( FileDependency& x )
{
return x.toString();
}
But I just realized, that assertion_traits is not the problem. When
specializing a template, you can change the function signature anyway you want.
So I could actually write the template the right way (having non-const
reference).
So that lead us to... (below)
>
> I'm worried about making the change you propose. I use const a lot,
> and I'm sure that I have CppUnit tests in functions that are
> passed const references:
>
> foo( const T& x )
> {
> T y(blahblah);
> CPPUNIT_ASSERT_EQUAL(x,y);
> }
>
> I think your proposed change would break my code, wouldn't it?
Yes, I'm a dummy. I inverted the implicit cast. So the change can't be done
the way I proposed (We should add some unit test for the traits I think).
How about changing assertEquals() which is the method that actually force the
use of const reference ?
template <class T>
void assertEquals( const T& expected,
const T& actual, ... )
=> change to :
template <class T>
void assertEquals( T expected,
T actual, ... )
This means that pass-by-value would be use instead of const reference.
Means that tested object should either have a copy constructor or be pointer.
Baptiste.
>
>
> Thanks,
> -Steve
---
Baptiste Lepilleur <gai...@fr...>
http://gaiacrtn.free.fr/index.html
Language: English, French
|