Matcher usage?

108 views
Skip to first unread message

Martin Moene

unread,
Aug 20, 2013, 2:56:02 PM8/20/13
to catch...@googlegroups.com
Can someone give some examples of Catch matcher usage?

I couldn't find anything such.

Thanks,
Martin

Martin Moene

unread,
Aug 20, 2013, 4:55:55 PM8/20/13
to catch...@googlegroups.com
Ok, I see that SelfTest/MiscTests.cpp contains some examples

Martin Moene

unread,
Aug 20, 2013, 5:25:55 PM8/20/13
to catch...@googlegroups.com
For example:

#define CATCH_CONFIG_MAIN
#include "catch.hpp"

using namespace Catch;

TEST_CASE( "show current matchers", "" )
{
    REQUIRE_THAT( "abc",
        AllOf(
            StartsWith("a")
            , Contains("b")
            , Equals("abc")
            // , EndsWith("c" ) // up to 3 arguments
    ) );

    REQUIRE_THAT( "abc",
        AnyOf(
            StartsWith("c")
            , Contains("b")
            , Equals("cba")
            // , EndsWith("a" ) // up to 3 arguments
    ) );

}

// cl -nologo -EHsc -I$(CATCH_HOME) match.cpp && match.exe
// g++ -I$(CATCH_HOME) -o match.exe match.cpp && match.exe

Gives:
match.exe is a Catch v1.0 b8 host application.
Run with -? for options

-------------------------------------------------------------------------------
show current matchers
-------------------------------------------------------------------------------
match.cpp:7
...............................................................................

match.cpp:15:
PASSED:
  REQUIRE_THAT( "abc" AllOf( StartsWith("a") , Contains("b") , Equals("abc") ) )

with expansion:
  "abc" ( starts with: "a" and contains: "b" and equals: "abc" )

match.cpp:23:
PASSED:
  REQUIRE_THAT( "abc" AnyOf( StartsWith("c") , Contains("b") , Equals("cba") ) )

with expansion:
  "abc" ( starts with: "c" or contains: "b" or equals: "cba" )

===============================================================================
All tests passed (2 assertions in 1 test case)

Phil Nash

unread,
Aug 20, 2013, 6:06:28 PM8/20/13
to catch...@googlegroups.com
Yeah support is pretty embryonic at the moment. It needs a lot more to be useful - although I do use those string matchers quite a lot - mostly for verifying error messages without pinning down precise strings.

While more string matchers are needed, I think it will become a lot more powerful once we have a few container-based matchers (e.g. to verify that a set contains a list of specific items without regard to ordering etc). The trick will be getting that to take full advantage of C++11 features such as initialiser lists while still working and being as usable as possible with C++03.

Where you looking for anything in particular, or just exploring?

Martin Moene

unread,
Aug 21, 2013, 3:06:23 AM8/21/13
to catch...@googlegroups.com
I was just wondering what Catch provides and how to use it.

Came here via watching James Grenning's Embedded Development, What's Changed in 30 Years?: mocking, hamcrest matchers.

Reply all
Reply to author
Forward
0 new messages