Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Added a handful of "built-in" matchers

Commit
a6a40b3ba9fad2977f10a0b77f2aa0d6e74b5852
Author
Phil Nash ·
Parents
eca5637c5823

Files · Download .tar.gz

Changes

 include/catch.hpp                   |  1 +
 include/internal/catch_matchers.hpp | 86 +++++++++++++++++++++++++++++++++++++
 projects/SelfTest/MiscTests.cpp     | 46 ++++++++++----------
 projects/SelfTest/TestMain.cpp      |  4 +-
 4 files changed, 112 insertions(+), 25 deletions(-)

diff --git a/include/catch.hpp b/include/catch.hpp
index 8b0e688d..330372ac 100644
--- a/include/catch.hpp
+++ b/include/catch.hpp
@@ -29,6 +29,7 @@
 #include "internal/catch_interfaces_exception.h"
 #include "internal/catch_approx.hpp"
 #include "internal/catch_test_case_info.hpp"
+#include "internal/catch_matchers.hpp"
 
 #ifdef __OBJC__
 #include "internal/catch_objc.hpp"
diff --git a/include/internal/catch_matchers.hpp b/include/internal/catch_matchers.hpp
new file mode 100644
index 00000000..7a3ab889
--- /dev/null
+++ b/include/internal/catch_matchers.hpp
@@ -0,0 +1,86 @@
+//
+//  catch_matchers.hpp
+//  CatchSelfTest
+//
+//  Created by Phil Nash on 04/03/2012.
+//  Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+//
+
+#ifndef TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
+#define TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
+
+namespace Catch
+{
+namespace Matchers
+{
+    namespace Impl
+    {
+    namespace StdString
+    {
+        struct Contains
+        {
+            Contains( const std::string& substr ) : m_substr( substr ){}
+            
+            bool operator()( const std::string& str ) const
+            {
+                return str.find( m_substr ) != std::string::npos;
+            }
+            
+            friend std::ostream& operator<<( std::ostream& os, const Contains& matcher )
+            {
+                os << "contains: \"" << matcher.m_substr << "\"";
+                return os;
+            }
+            std::string m_substr;
+        };
+        
+        struct StartsWith
+        {
+            StartsWith( const std::string& substr ) : m_substr( substr ){}
+            
+            bool operator()( const std::string& str ) const
+            {
+                return str.find( m_substr ) == 0;
+            }
+            
+            friend std::ostream& operator<<( std::ostream& os, const StartsWith& matcher )
+            {
+                os << "starts with: \"" << matcher.m_substr << "\"";
+                return os;
+            }
+            std::string m_substr;
+        };
+        
+        struct EndsWith
+        {
+            EndsWith( const std::string& substr ) : m_substr( substr ){}
+            
+            bool operator()( const std::string& str ) const
+            {
+                return str.find( m_substr ) == str.size() - m_substr.size();
+            }
+            
+            friend std::ostream& operator<<( std::ostream& os, const EndsWith& matcher )
+            {
+                os << "ends with: \"" << matcher.m_substr << "\"";
+                return os;
+            }
+            std::string m_substr;
+        };
+    } // namespace StdString
+    } // namespace Impl
+    
+    inline Impl::StdString::Contains    Contains( const std::string& substr ){ return Impl::StdString::Contains( substr ); }
+    inline Impl::StdString::StartsWith  StartsWith( const std::string& substr ){ return Impl::StdString::StartsWith( substr ); }
+    inline Impl::StdString::EndsWith    EndsWith( const std::string& substr ){ return Impl::StdString::EndsWith( substr ); }
+    
+} // namespace Matchers
+    
+using namespace Matchers;
+    
+} // namespace Catch
+
+#endif // TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp
index 33e41cf5..b73b1aaf 100644
--- a/projects/SelfTest/MiscTests.cpp
+++ b/projects/SelfTest/MiscTests.cpp
@@ -186,33 +186,33 @@ TEST_CASE("./succeeding/atomic if", "")
         REQUIRE(x == 0);
 }
 
-namespace Matchers
+inline const char* testStringForMatching()
+{
+    return "this string contains 'abc' as a substring";
+}
+
+using namespace Catch::Matchers;
+
+TEST_CASE("./succeeding/matchers", "") 
 {    
-    struct ContainsStdString
-    {
-        ContainsStdString( const std::string& substr ) : m_substr( substr ){}
-        
-        bool operator()( const std::string& str ) const
-        {
-            return str.find( m_substr ) != std::string::npos;        
-        }
-        
-        friend std::ostream& operator<<( std::ostream& os, const ContainsStdString& matcher )
-        {
-            os << "contains: \"" << matcher.m_substr << "\"";
-            return os;
-        }
-        std::string m_substr;
-    };
+    REQUIRE_THAT( testStringForMatching(), Contains( "string" ) );    
+    CHECK_THAT( testStringForMatching(), Contains( "abc" ) );
+
+    CHECK_THAT( testStringForMatching(), StartsWith( "this" ) );
+    CHECK_THAT( testStringForMatching(), EndsWith( "substring" ) );
 }
 
-inline Matchers::ContainsStdString Contains( const std::string& substr ){ return Matchers::ContainsStdString( substr ); }
+TEST_CASE("./failing/matchers/Contains", "") 
+{
+    CHECK_THAT( testStringForMatching(), Contains( "not there" ) );
+}
 
+TEST_CASE("./failing/matchers/StartsWith", "") 
+{
+    CHECK_THAT( testStringForMatching(), StartsWith( "string" ) );
+}
 
-TEST_CASE("./succeeding/matcher", "") 
+TEST_CASE("./failing/matchers/EndsWith", "") 
 {
-    const char* actualStr = "this string contains 'abc' as a substring";
-    REQUIRE_THAT( actualStr, Contains( "string" ) );
-    CHECK_THAT( actualStr, Contains( "not there" ) );
-    CHECK_THAT( actualStr, Contains( "a2bc" ) );
+    CHECK_THAT( testStringForMatching(), EndsWith( "this" ) );
 }
diff --git a/projects/SelfTest/TestMain.cpp b/projects/SelfTest/TestMain.cpp
index c9a1e458..06956a58 100644
--- a/projects/SelfTest/TestMain.cpp
+++ b/projects/SelfTest/TestMain.cpp
@@ -43,7 +43,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results"
                     "Number of 'succeeding' tests is fixed" )
         {
             runner.runMatching( "./succeeding/*" );
-            CHECK( runner.getTotals().assertions.passed == 268 );
+            CHECK( runner.getTotals().assertions.passed == 272 );
             CHECK( runner.getTotals().assertions.failed == 0 );
         }
 
@@ -52,7 +52,7 @@ TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results"
         {
             runner.runMatching( "./failing/*" );        
             CHECK( runner.getTotals().assertions.passed == 0 );
-            CHECK( runner.getTotals().assertions.failed == 68 );
+            CHECK( runner.getTotals().assertions.failed == 71 );
         }
     }
 }