Blame examples/110-Fix-ClassFixture.cpp

rpm-build a7f80b
// 110-Fix-ClassFixture.cpp
rpm-build a7f80b
rpm-build a7f80b
// Catch has two ways to express fixtures:
rpm-build a7f80b
// - Sections
rpm-build a7f80b
// - Traditional class-based fixtures (this file)
rpm-build a7f80b
rpm-build a7f80b
// main() provided in 000-CatchMain.cpp
rpm-build a7f80b
rpm-build a7f80b
#include "catch.hpp"
rpm-build a7f80b
rpm-build a7f80b
class DBConnection
rpm-build a7f80b
{
rpm-build a7f80b
public:
rpm-build a7f80b
    static DBConnection createConnection( std::string const & /*dbName*/ ) {
rpm-build a7f80b
        return DBConnection();
rpm-build a7f80b
    }
rpm-build a7f80b
rpm-build a7f80b
    bool executeSQL( std::string const & /*query*/, int const /*id*/, std::string const & arg ) {
rpm-build a7f80b
        if ( arg.length() == 0 ) {
rpm-build a7f80b
            throw std::logic_error("empty SQL query argument");
rpm-build a7f80b
        }
rpm-build a7f80b
        return true; // ok
rpm-build a7f80b
    }
rpm-build a7f80b
};
rpm-build a7f80b
rpm-build a7f80b
class UniqueTestsFixture
rpm-build a7f80b
{
rpm-build a7f80b
protected:
rpm-build a7f80b
    UniqueTestsFixture()
rpm-build a7f80b
    : conn( DBConnection::createConnection( "myDB" ) )
rpm-build a7f80b
    {}
rpm-build a7f80b
rpm-build a7f80b
    int getID() {
rpm-build a7f80b
        return ++uniqueID;
rpm-build a7f80b
    }
rpm-build a7f80b
rpm-build a7f80b
protected:
rpm-build a7f80b
    DBConnection conn;
rpm-build a7f80b
rpm-build a7f80b
private:
rpm-build a7f80b
    static int uniqueID;
rpm-build a7f80b
};
rpm-build a7f80b
rpm-build a7f80b
int UniqueTestsFixture::uniqueID = 0;
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE_METHOD( UniqueTestsFixture, "Create Employee/No Name", "[create]" ) {
rpm-build a7f80b
    REQUIRE_THROWS( conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "") );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
TEST_CASE_METHOD( UniqueTestsFixture, "Create Employee/Normal", "[create]" ) {
rpm-build a7f80b
    REQUIRE( conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs" ) );
rpm-build a7f80b
}
rpm-build a7f80b
rpm-build a7f80b
// Compile & run:
rpm-build a7f80b
// - g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 110-Fix-ClassFixture 110-Fix-ClassFixture.cpp 000-CatchMain.o && 110-Fix-ClassFixture --success
rpm-build a7f80b
// - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 110-Fix-ClassFixture.cpp 000-CatchMain.obj && 110-Fix-ClassFixture --success
rpm-build a7f80b
rpm-build a7f80b
// Expected compact output (all assertions):
rpm-build a7f80b
//
rpm-build a7f80b
// prompt> 110-Fix-ClassFixture.exe --reporter compact --success
rpm-build a7f80b
// 110-Fix-ClassFixture.cpp:47: passed: conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "")
rpm-build a7f80b
// 110-Fix-ClassFixture.cpp:51: passed: conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs" ) for: true
rpm-build a7f80b
// Passed both 2 test cases with 2 assertions.