| 
									
										
										
										
											2017-10-13 09:43:50 +02:00
										 |  |  | // 110-Fix-ClassFixture.cpp
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Catch has two ways to express fixtures:
 | 
					
						
							|  |  |  | // - Sections
 | 
					
						
							|  |  |  | // - Traditional class-based fixtures (this file)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 22:36:47 +00:00
										 |  |  | // main() provided by linkage to Catch2WithMain
 | 
					
						
							| 
									
										
										
										
											2017-10-13 09:43:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 14:46:07 +01:00
										 |  |  | #include <catch2/catch_test_macros.hpp>
 | 
					
						
							| 
									
										
										
										
											2017-10-13 09:43:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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:
 | 
					
						
							| 
									
										
										
										
											2022-05-24 10:07:17 +02:00
										 |  |  | // - g++ -std=c++14 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 110-Fix-ClassFixture 110-Fix-ClassFixture.cpp && 110-Fix-ClassFixture --success
 | 
					
						
							| 
									
										
										
										
											2022-02-18 22:36:47 +00:00
										 |  |  | // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 110-Fix-ClassFixture.cpp && 110-Fix-ClassFixture --success
 | 
					
						
							| 
									
										
										
										
											2022-05-24 10:07:17 +02:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Compile with pkg-config:
 | 
					
						
							|  |  |  | // - g++ -std=c++14 -Wall $(pkg-config catch2-with-main --cflags)  -o 110-Fix-ClassFixture 110-Fix-ClassFixture.cpp $(pkg-config catch2-with-main --libs)
 | 
					
						
							| 
									
										
										
										
											2017-10-13 09:43:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // 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.
 |