tags:

views:

10

answers:

0

I'm trying to use CppUnit test, and this is the simple test code borrowed from CppUnit Cookbook. The system is Mac OS X with g++ 4.2.1.

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include <iostream>

class Board
{
    int m_x, m_y;
public:
    Board(int x, int y) : m_x(x), m_y(y) {}  
    bool operator==(const Board& other) const
    {
        return  (m_x == other.m_x && m_y == other.m_y);
    } 
    int getX() {return m_x;}
    int getY() {return m_y;}
};

inline std::ostream &operator <<( std::ostream &os, Board &value )
{
    return os << "Board <x =" << value.getX() << "; y = " << value.getY() << "> \n";
}

class BoardTest : public CppUnit::TestFixture {
    Board *m_10_20, *m_10_20p, *m_10_30;
public:
    BoardTest() {}
    void setUp() 
    {
        m_10_20 = new Board(10,20);
        m_10_20p = new Board(10,20);
        m_10_30 = new Board(10,30);
    }
    void tearDown()
    {
        delete m_10_20; delete m_10_20p; delete m_10_30;
    }
    void testX()
    {
        CPPUNIT_ASSERT(*m_10_20 == *m_10_20p);
    }
    void testY()
    {
        CPPUNIT_ASSERT(!(*m_10_20 == *m_10_30));
    }
    static CppUnit::Test *suite()
    {
        CppUnit::TestSuite *testsuite = new CppUnit::TestSuite("Board test");
        suite->addTest(new CppUnit::TestCaller<BoardTest>("testX", &BoardTest::testX));
        suite->addTest(new CppUnit::TestCaller<BoardTest>("testY", &BoardTest::testY));        
        return testsuite;
    }
};

int main()
{

    CppUnit::TextUi::TestRunner runner;

    runner.addTest(BoardTest::suite() );

    bool retcode = runner.run();
    return retcode;
}

When I run this, I get the following error.

simple4.cpp:48: error: request for member 'addTest' in 'BoardTest::suite', which is of non-class type 'CppUnit::Test* ()()'
simple4.cpp:49: error: request for member 'addTest' in 'BoardTest::suite', which is of non-class type 'CppUnit::Test* ()()'

And the following equivalent code just works. What might be wrong? Is the first (previous) example is kind of obsolete?

#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/ui/text/TestRunner.h>
#include <iostream>

class Board
{
    int m_x, m_y;
public:
    Board(int x, int y) : m_x(x), m_y(y) {}  
    bool operator==(const Board& other) const
    {
        return  (m_x == other.m_x && m_y == other.m_y);
    } 
    int getX() {return m_x;}
    int getY() {return m_y;}
};

inline std::ostream &operator <<( std::ostream &os, Board &value )
{
    return os << "Board <x =" << value.getX() << "; y = " << value.getY() << "> \n";
}

class BoardTest : public CppUnit::TestFixture {
    CPPUNIT_TEST_SUITE(BoardTest);
        CPPUNIT_TEST(testX);
        CPPUNIT_TEST(testY);
    CPPUNIT_TEST_SUITE_END();
    Board *m_10_20, *m_10_20p, *m_10_30;
public:
    BoardTest() {}
    void setUp() 
    {
        m_10_20 = new Board(10,20);
        m_10_20p = new Board(10,20);
        m_10_30 = new Board(10,30);
    }
    void tearDown()
    {
        delete m_10_20; delete m_10_20p; delete m_10_30;
    }
    void testX()
    {
        CPPUNIT_ASSERT(*m_10_20 == *m_10_20p);
    }
    void testY()
    {
        CPPUNIT_ASSERT(!(*m_10_20 == *m_10_30));
    }
};

int main()
{
    CppUnit::TestSuite *suite = BoardTest::suite();
    CppUnit::TextUi::TestRunner runner;

    runner.addTest(suite);

    bool retcode = runner.run();
    return retcode;
}