Boost logo

Boost Users :

From: Bill Lear (rael_at_[hidden])
Date: 2005-01-28 17:23:57


I am having trouble working around a problem with inheriting unit
test cases.

Suppose I have a base class Foo and several derived classes. Suppose
I have a corresponding base test case and I would like to reuse that
base test case with test cases for the derived classes.

What I would like to do is this, in highly stylized form:

class BaseFooTestCase {
public:
    virtual Foo* create() = 0;

    void test_base() {
        Foo* foo = create();
        // test foo ...
    }

    void add(test_suite* ts) {
        ts->add(BOOST_CLASS_TEST_CASE(&BaseFooTestCase::test_base, this);
    }
};

class DerivedFooTestCase {
public:
    virtual Foo* create() { return new Derived(); }

    void test_derived() {
        Foo* foo = create();
        // test foo
    }

    void add(test_suite* ts) {
        BaseFooTestCase::add(ts);
        ts->add(BOOST_CLASS_TEST_CASE(&DerivedFooTestCase::test_derived, this);
    }
};

Then, something like this:

test_suite* init_unit_test_suite(int, char* []) {
    auto_ptr<test_suite> ts(new test_suite("test"));

    DerivedFooTestCase* tc = new DeriveFooTestCase();

    tc->add(ts);

    return ts;
}

However, I can't get the add() methods in the test case classes worked
out. The BOOST_CLASS_TEST_CASE macro seems to want a shared_ptr,
and passing "this" to it does not seem to work.

Could anyone please provide an example of how to do this?

Every time I try to work this out, I end up with a mess --- static
allocator methods, weak_ptr wrapped by shared_ptr, and other
workarounds --- all of which works, but is really quite a lot of
effort for what I want and is overly complex for me to recommend to my
team as a solution.

Any help appreciated.

Thanks.

Bill


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