[boost] Exception catching in execution_monitor.cpp

2003-02-14 Thread r . lichtenberger
I've encountered a problem with exceptions in boost/test:
Our project uses a base class for all our exceptions which cannot derive 
from std::exception.

If such an exception is thrown, the message:
Exception in "...": unknown type
is printed out, which is not very helpful in locating the problem.

Is there a way to specify an "exception handler", which can catch "our" 
exceptions and print something more useful?.

We are working with the unit_test_monitor if that makes any difference.

Thanks,
Robert
___
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost



[boost] Request for const fix in unit_test_suite.hpp

2003-02-12 Thread r . lichtenberger
Hello boosters,

I've justed stumbled across a problem when using the BOOST_CLASS_TEST_CASE 

macro in a special way.
I am using class based test cases and want to run every testcase from a 
clean environment, i.e. I need to create a new instance of my test class 
for every test case.
To keep the code simple and clean I wrote myself a function that returns 
the required shared_ptr on a test instance:

--- SNIP ---
boost::shared_ptr testInstance() 
{
return boost::shared_ptr(new MyTest());
}

test_suite* init_unit_test_suite( int argc, char * argv[] ) {
test_suite* test= BOOST_TEST_SUITE( "My Unit Test" );

test->add(BOOST_CLASS_TEST_CASE(&MyTest::test1, testInstance()));
test->add(BOOST_CLASS_TEST_CASE(&MyTest::test2, testInstance()));
test->add(BOOST_CLASS_TEST_CASE(&MyTest::test3, testInstance()));

return test; 
}
--- SNIP ---

This works perfectly well under Windows using the Microsoft compiler. On 
Linux, using gcc (tried 2.96 and 3.2) it fails. The reason it fails is, 
because the return value of testInstance() is put into a temporary (is 
that the correct term?) variable and the test framework needs a 
shared_ptr&. This reference ist not const at the moment, thus the (gcc) 
compiler refuses to compile.
The problem can be illustrated this:

--- SNIP ---
class Object {
};

void test(Object& obj) {
}

Object makeObject() {
return Object();
}

void foo() {
test(makeObject()); // fails
Object obj = makeObject();
test(obj);  // works
}
--- SNIP ---

Although test(makeObject()) should IMHO be just the same as the test(obj) 
stuff, the former fails to compile.

I've tried to make the shared_ptr const in 
unit_test_suite.hpp and succeeded (needed to add "const" in two 
places...).

May I request that this fix is integrated into the base source tree?
___
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost



[boost] Test Tool Proposal (test_tools.hpp)

2003-02-03 Thread r . lichtenberger
I suggest adding the following (or a similar) macro to test_tools.hpp:

#define BOOST_CHECK_EQUAL_MESSAGE(left_, right_, message_) \
boost::test_toolbox::detail::equal_and_continue_impl((left_), 
(right_), \
boost::test_toolbox::detail::wrapstrstream() << #left_ " == " 
#right_ << " (" << message_ << ")" , __FILE__, __LINE__)

Sometimes in a unit test one has got a function/method to check certain 
properties of an object in order to avoid duplicating lots of code

--- SNIP Example ---
MyObject* my1 = new MyObject(1, 2, 3);
BOOST_CHECK_EQUAL(my1->getA(), 1);
BOOST_CHECK_EQUAL(my1->getB(), 2);
BOOST_CHECK_EQUAL(my1->getC(), 3);

MyObject* my2 = new MyObject(4, 5, 6);
BOOST_CHECK_EQUAL(my2->getA(), 4);
BOOST_CHECK_EQUAL(my2->getB(), 5);
BOOST_CHECK_EQUAL(my2->getC(), 6);
--- SNIP ---

can be refactored into:

--- SNIP Example refactored ---
void checkMyObject(MyObject* my, int a, int b, int c) 
{
BOOST_CHECK_EQUAL(my->getA(), a);
BOOST_CHECK_EQUAL(my->getB(), b);
BOOST_CHECK_EQUAL(my->getC(), c);
}

MyObject* my1 = new MyObject(1, 2, 3);
checkMyObject(my1, 1, 2, 3);
MyObject* my2 = new MyObject(4, 5, 6);
checkMyObject(my2, 4, 5, 6);
--- SNIP ---

The problem with the second version is that if the check for equality 
fails, the only output is the line number within checkMyObject() that 
failed. No hint about the calling line is present. Ideally all macros 
should print out a stack trace (as done in e.g. Java), but from my limited 
knowledge of C++ I guess that this is not (easily) possible, so 
introducing a BOOST_CHECK_EQUAL_MESSAGE macro is the second best solution 
IMHO:

--- SNIP Example with new macro ---
void checkMyObject(MyObject* my, int context, int a, int b, int c) 
{
BOOST_CHECK_EQUAL_MESSAGE(my->getA(), a, context);
BOOST_CHECK_EQUAL_MESSAGE(my->getB(), b, context);
BOOST_CHECK_EQUAL_MESSAGE(my->getC(), c, context);
}

MyObject* my1 = new MyObject(1, 2, 3);
checkMyObject(my1, __LINE__, 1, 2, 3);
MyObject* my2 = new MyObject(4, 5, 6);
checkMyObject(my2, __LINE__, 4, 5, 6);
--- SNIP ---

Note that instead of using an int context we could also have a std::string 
or any other streamable object. Using an int and the __LINE__ macro is 
just convenient here.

Best regards, 
Robert

___
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost