Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/examples/110-Fix-ClassFixture.cpp
diff options
context:
space:
mode:
authorMartin Moene2017-10-13 09:43:50 +0200
committerMartin Hořeňovský2017-11-15 15:37:39 +0100
commit4ecb2e112e0565451c14fc42d63f438cb9ecb7dd (patch)
treea096a4aa62856f6b71c4358c697630f346434e7d /examples/110-Fix-ClassFixture.cpp
parent97a8640cbf10a8ff3d6f06f0293f5c37b263144a (diff)
downloadcatch3-4ecb2e112e0565451c14fc42d63f438cb9ecb7dd.tar.gz
Add examples folder with initial examples
Diffstat (limited to 'examples/110-Fix-ClassFixture.cpp')
-rw-r--r--examples/110-Fix-ClassFixture.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/110-Fix-ClassFixture.cpp b/examples/110-Fix-ClassFixture.cpp
new file mode 100644
index 00000000..06c2cf32
--- /dev/null
+++ b/examples/110-Fix-ClassFixture.cpp
@@ -0,0 +1,63 @@
+// 110-Fix-ClassFixture.cpp
+
+// Catch has two ways to express fixtures:
+// - Sections
+// - Traditional class-based fixtures (this file)
+
+// main() provided in 000-CatchMain.cpp
+
+#include "catch.hpp"
+
+class DBConnection
+{
+public:
+ static DBConnection createConnection( std::string const & /*dbName*/ ) {
+ return DBConnection();
+ }
+
+ bool executeSQL( std::string const & /*query*/, int const /*id*/, std::string const & arg ) {
+ if ( arg.length() == 0 ) {
+ throw std::logic_error("empty SQL query argument");
+ }
+ return true; // ok
+ }
+};
+
+class UniqueTestsFixture
+{
+protected:
+ UniqueTestsFixture()
+ : conn( DBConnection::createConnection( "myDB" ) )
+ {}
+
+ int getID() {
+ return ++uniqueID;
+ }
+
+protected:
+ DBConnection conn;
+
+private:
+ static int uniqueID;
+};
+
+int UniqueTestsFixture::uniqueID = 0;
+
+TEST_CASE_METHOD( UniqueTestsFixture, "Create Employee/No Name", "[create]" ) {
+ REQUIRE_THROWS( conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "") );
+}
+
+TEST_CASE_METHOD( UniqueTestsFixture, "Create Employee/Normal", "[create]" ) {
+ REQUIRE( conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs" ) );
+}
+
+// Compile & run:
+// - g++ -std=c++11 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 110-Fix-ClassFixture 110-Fix-ClassFixture.cpp 000-CatchMain.o && 110-Fix-ClassFixture --success
+// - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 110-Fix-ClassFixture.cpp 000-CatchMain.obj && 110-Fix-ClassFixture --success
+
+// Expected compact output (all assertions):
+//
+// prompt> 110-Fix-ClassFixture.exe --reporter compact --success
+// 110-Fix-ClassFixture.cpp:47: passed: conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "")
+// 110-Fix-ClassFixture.cpp:51: passed: conn.executeSQL( "INSERT INTO employee (id, name) VALUES (?, ?)", getID(), "Joe Bloggs" ) for: true
+// Passed both 2 test cases with 2 assertions.