|
Boost Users : |
From: Carlo Wood (carlo_at_[hidden])
Date: 2006-05-04 22:05:55
On Wed, May 03, 2006 at 03:03:30PM -0700, Ed Johnson wrote:
> > private:
> > int foo(int a);
> >
> > #ifdef TESTING
> > public:
> > int foo_test(int a)
> > {
> > return foo(a);
> > }
> > #endif
> >
> > I'm not sure if this is a preferable alternative to your idea of
> > separating members into their own test suites, but it certainly seems
> > simpler.
> >
> > Paul
>
> I like that better than my previous approach, too. It allows finer
> grained control than my strategy. However, I think Gennadiy's solution
> that he posted in this thread is even less hacky, and gets the job done.
Obviously, you can't use 'TESTING', it is way too general.
Some macro name will have to be used that will stay constant
forever and will never collide with anyone elses code.
Also, the above is slightly dangerous, as the 'public' would
'leak' beyond the #ifdef when TESTING is defined. You'd have to make
sure that the test block at the end of the class.
Personally, I like the #define private public best, because it doesn't
polute the real code.
You could do the following without running into the danger of writing
test code that accesses something that you want to be private.
Unaltered foo.h:
class Foo {
private:
int x;
int f_priv(int a); // Private function that needs testing.
public:
int f();
};
--- test code: #define private protected #include "foo.h" #undef private namespace PrivateAccess { class Foo : public ::Foo { public: ::Foo::f_priv; // Change access from protected to public. }; } namespace Testsuite { using PrivateAccess::Foo; // Test code with access to Foo::f_priv, but nothing else: Foo foo; void test_func() { foo.f_priv(3); // Access private function of Foo int y = foo.x; // Compile error. } } -- Carlo Wood <carlo_at_[hidden]>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net